reddit / emacs

Swap Ctrl and alt just in Emacs on windows

aimebertrand · 2022-02-23 18:59
I need to use Windows at work :(, but cannot not use Emacs still. However, I would like to swap ctrl and alt (meta) just in Emacs not in the whole OS. I usually trust my google-fu. However the last few days it did fail me. Help, please help. ​ EDIT: The Link by u/mmarshall540 was exactly the solution. More details: in Windows I already had swaped Crtl and alt because of mymuscle memory on macOS (I use cmd for stuff like copy&paste and more). I wanted to keep the position of the keys. However. Since crtl is now in the position of what is supposed to be meta (cmd on macOS) I needed to swap back ctrl and alt. But just in Emacs. Convoluted, I know. It work for me just fine now and I am a happy camper. Thanx again u/mmarshall540.
HM0880 · 2022-02-23 19:54
Try `key-translation-map`. Example: `(define-key key-translation-map (kbd "<print>") (kbd "<menu>"))` Lots of info from Xah: http://xahlee.info/emacs/emacs/emacs_menu_app_keys.html
aimebertrand · 2022-02-23 20:09
Thanx for the hint. I naively tried `(define-key key-translation-map (kbd "<ctrl>") (kbd "<alt>"))`, but it did not work of course. I am tried to guess/look for how the keys might be called in this case.
HM0880 · 2022-02-23 21:14
Yeah, I do not know where the handoffs are from keyboard-to-OS and OS-to-Emacs. If you run Windows, AutoHotKey can probably do what you want, and if you run a Unix-y OS, then scripts must exist for this low-level task. Let me know what you find out.
SlowValue · 2022-02-23 22:29
Emacs at user level only registers "complete" keystrokes, it is not able to handle modifier key presses alone. You could try some different approach, like a [leader key implementation](https://github.com/mohkale/spaceleader) or [general](https://github.com/noctuid/general.el). Maybe there is even a windows tool, which can remap keys before Emacs receives them (some Window Managers can do that on Linux/X11, so maybe this also possible with Windows).
EFLS_ · 2022-02-23 23:00
In my Emacs (on macOS) there's variables like `mac-option-modifier` and `mac-command-modifier`, provided by `ns-win.el`. Not sure where they are added to my Emacs installation, but it might give you a clue? EDIT: I think you need to look at the variables `ns-control-modifier` and `ns-alternate-modifier`. Do you have those?
cazzipropri · 2022-02-23 23:25
This might not be exactly what you had in mind, but under windows, [http://www.kbdedit.com/](http://www.kbdedit.com/) is sort of the best product you can get for that purpose. It's not free software, but the lite edition ($9) should be sufficient to swap two keys. You can make your own custom layout and then use windows to set a different layout per each application. For emacs, you'll select your custom layout.
SoraFirestorm · 2022-02-24 00:42
Those variables are platform-specific. Look for the `win32-*` variants.
mmarshall540 · 2022-02-24 02:40
[This gets it done by translating all-the-things.](https://gist.github.com/mmarshall540/8db9b5bac8dc5670cb9323e387de1317) But if there are platform-specific functions that do this on Windows, that might be preferable.
aimebertrand · 2022-02-24 13:29
Hi, Thank you for the hint as well. In the end: The Link by u/mmarshall540 was exactly what I was looking for.
aimebertrand · 2022-02-24 13:34
MVP - Thank you very much. I mean very very much.
cazzipropri · 2022-02-24 16:04
Oh yeah - that's some elisp-fu over there. I wrote some elisp but meddling with keymaps and how they nest is beyond my comfort level.
cattmamp · 2022-02-24 16:57
How did you go about swapping Control and Alt in Windows?
aimebertrand · 2022-02-24 18:11
Hi, I just used some new Registry Key Value pair. Put this in `.reg` file: Windows Registry Editor Version 5.00 \[HKEY\_LOCAL\_MACHINE\\SYSTEM\\CurrentControlSet\\Control\\Keyboard Layout\] "Scancode Map"=hex:00,00,00,00,00,00,00,00,\\ 05,00,00,00,\\ 38,00,1d,00,\\ 1d,00,38,00,\\ 3a,00,01,00,\\ 01,00,3a,00,\\ 00,00,00,00 Then import the file using the registry editor. Careful, this swaps Caps Lock and Escape too. However there are some versions. See here https://superuser.com/questions/1190329/can-i-switch-the-alt-and-ctrl-keys-on-my-keyboard
aimebertrand · 2022-02-24 18:13
Say, I just noticed that M-x and some other character combination do not work here. Any Ideas. Sorry to say that my lisp is abysmal.
cattmamp · 2022-02-24 18:13
Thank you so much!
not-quite-himself · 2022-02-25 14:35
I use AutoHotkey to map CapsLock to Ctrl in emacs, with the following code in a file caps2ctrl.ahk: ; https://www.emacswiki.org/emacs/MovingTheCtrlKey#h5o-14 ; https://www.autohotkey.com/docs/FAQ.htm#Startup #IfWinActive emacs ; if in emacs +Capslock::Capslock ; make shift+Caps-Lock the Caps Lock toggle Capslock::Control ; make Caps Lock the control button #IfWinActive ; end if in emacs Maybe this could work for your case too. The included urls have more details.
aimebertrand · 2022-02-26 21:12
Hi, I think this might be a solution too. Willl of course try it out. Thanx a bunch.
mmarshall540 · 2022-03-23 18:21
Hi, sorry I didn't see this until now. The GitHub gist has been updated to use `input-decode-map` instead of `key-translation-map`. That fixes the `M-x` problem. It also has the side benefit that we can now swap `C-i` with `M-i` and `C-m` with `M-m`. You can read more [here](https://www.gnu.org/software/emacs/manual/html_node/elisp/Translation-Keymaps.html) about how the key translation maps work. Basically, each time you press a key, your key sequence as it was entered up to that point gets filtered through 3 different translation maps. Since there are some translations in the middle map that use `C-x` as a prefix key, swapping `C-x` with `M-x` in the last map lead Emacs to continue waiting for more keys to see if any of the `C-x` translations from the middle map would apply. If you try the updated code and find any other keys that don't work, please let me know. I usually check GitHub more often than here.
aimebertrand · 2022-03-24 16:16
Hola, That did the trick. I will definetelly read up on it some more. But so far you have done me a huge solid. Thanx a bunch