Redefining keys in Emacs on Windows - windows

I use my command keys as extra control keys on Mac OS X (and I believe the space cadet keyboards had this configuration). I want to emulate this on my Windows machines by switching Alt to Ctrl and the Windows key to Alt within Emacs. Is this possible? I found this post which suggests something like
(setq w32-pass-lwindow-to-system nil
w32-pass-rwindow-to-system nil
w32-pass-apps-to-system nil
w32-lwindow-modifier 'super ;; Left Windows key
w32-rwindow-modifier 'super ;; Right Windows key
w32-apps-modifier 'hyper) ;; Menu key
to get super and hyper keys upon pressing the windows key. When I try to remap this to 'control to test it out (but eventually I want it to be meta and Alt as Ctrl, as mentioned), windows-e still gets intercepted by Windows (XP) and opens Explorer, but seems like a good place to start? I would appreciate any suggestions.

From the documentation for w32-pass-lwindow-to-system:
Note that some combinations of the left "Windows" key with other keys are
caught by Windows at low level, and so binding them in Emacs will have no
effect. For example, <lwindow>-r always pops up the Windows Run dialog,
<lwindow>-<Pause> pops up the "System Properties" dialog, etc. However, see
the doc string of `w32-phantom-key-code'.
From the documentation for w32-phantom-key-code:
Virtual key code used to generate "phantom" key presses.
Value is a number between 0 and 255.
Phantom key presses are generated in order to stop the system from
acting on "Windows" key events when `w32-pass-lwindow-to-system' or
`w32-pass-rwindow-to-system' is nil.
And as it also says on Xah's website:
There's no way around that unless you use other tools such as AutoHotkey. Even so, it may not be possible to disable 【Win+l】 (lock window) and 【Win+u】 (easy access).
So it seems that if it doesn't work with the method he describes (which seems right), you should look into AutoHotKey.

Related

Standard (no third-party) way to get numeric keypad working as arrow keys on macOS (Xcode? hidutil?)

I always use numeric keypad as arrows, as I find it more convenient than using an separate arrow pad. Until recently I used a tool Karabiner Elements, but it stopped functionning at Big Sur.
Yes, The Karabiner developer is working on that issue, but it will be better to solve it without relying on a third-party tool.
It will be enough for me to either remap Xcode, or (which is preferred) to change key bindings in the system, using a tool like hidutil.
Xcode's Preferences - Key Binding has a section Text - Section. When I try to modify the binding for say Move Down by pressing Down arrow on numeric keypad it comes up as '2'. Now whereever I press '2', on keyboard or numeric keypad, it always works as Move Down which is certainly not what I am after.
About hidutil. I couldn't find a good manual or any example of keyboard mapping.
Any help?
Had another look at this article. Here is the answer:
hidutil property --set '{"UserKeyMapping":[{"HIDKeyboardModifierMappingSrc":0x70000005A,"HIDKeyboardModifierMappingDst":0x700000051},
{"HIDKeyboardModifierMappingSrc":0x700000060,"HIDKeyboardModifierMappingDst":0x700000052},
{"HIDKeyboardModifierMappingSrc":0x70000005C,"HIDKeyboardModifierMappingDst":0x700000050},
{"HIDKeyboardModifierMappingSrc":0x70000005E,"HIDKeyboardModifierMappingDst":0x70000004F},
{"HIDKeyboardModifierMappingSrc":0x70000005F,"HIDKeyboardModifierMappingDst":0x70000004A},
{"HIDKeyboardModifierMappingSrc":0x700000061,"HIDKeyboardModifierMappingDst":0x70000004B},
{"HIDKeyboardModifierMappingSrc":0x700000059,"HIDKeyboardModifierMappingDst":0x70000004D},
{"HIDKeyboardModifierMappingSrc":0x70000005B,"HIDKeyboardModifierMappingDst":0x70000004E},
{"HIDKeyboardModifierMappingSrc":0x700000062,"HIDKeyboardModifierMappingDst":0x700000049},
{"HIDKeyboardModifierMappingSrc":0x700000063,"HIDKeyboardModifierMappingDst":0x70000004C}]}'
Still can't fugure out, how to do (if possible) complex mapping with modifier keys (eg Ctrl+PgUp to Home), but that's another topic.

List of deadkeys for current keyboard

I want to show users a list of the deadkeys they can press using their current Windows language & keyboard. (As opposed to a list of possible graves, circumflexes and acutes which their keyboard doesn't operate as deadkeys.)
Grateful for help.
Keyboard layout (including dead keys) are fixed in the keyboard driver. This means your problem can be reduce to getting the setting for keyboard. Use Windows API GetLocaleInfoW() function to get the information and then create the list of dead keys for each keyboard. For your reference have a look at: Windows-keyboard-layout and GetLocaleInfoW

Change meta and control keys in emacs for mac keyboard

There are several things I'd like to do:
leave the left control key as is
map the left command key so it's a meta key
map the right command key so it's a meta key
map the right up arrow so it's a control key
I know the three map commands need to be of the form:
(define-key KEYMAP KEY DEF)
Also I know my .emacs file located in my home directory. However, I'm not sure how to define these three values or if I have the right idea.
One other thing, I installed emacs 24, but I noticed that mac OSX seems to also come with emacs 22. I'd like both to function the same. Is there a way to make sure the key mappings match? Or even better replace emacs 22 with 24 in the terminal?
Emacs has a numbers of mac specific settings, including something similar what you ask for. They are included in the group 'Ns'.
use: M-x customize-group RET ns RET
I'm not really sure if left and right is treated separately. So maybe in addition you might want to install keyremap4macbook. This can do some remapping - even on application specific basis.
Personally I found it usefull to use the fn key as wildcard e.g. I remap left alt to fn and tell emacs to treat fn as meta.
Never figured this out. My solution was to switch out keyboards so now I have my command keys mapped to meta and my alt keys mapped to control.

Remap Caps lock key to Esc in Mma 7

TLDR: How do I get CapsLock to translate to "ShortNameDelimiter" in Mma 7?
I like pretty text in my mma notebooks, and often define functions as f[\[Alpha]_] =... so as to match the exact equation that I'm working with. As such, it involves a lot of Esc-letter-Esc sequences, and reaching for Esc every other stroke breaks my flow of typing.
Now, the CapsLock key is seldom used (I can't remember the last time I needed it), but conveniently placed (your pinky is right there!). Remapping it to Esc on vim worked wonders for me and I was wondering if there was a way to do the same in mma, without having to modify the system's keyboard layout.
I tried editing KeyEventTranslations.tr by adding the following in EventTranslations[{...
Item[KeyEvent["CapsLock"], "ShortNameDelimiter"]
but that had no effect. Is there another way to do it? Is CapsLock not the correct identifier? If it helps, I'm using Mma7 student version on a Mac.
Modifier keys are handled quite specially, and I doubt Mathematica will be able to override the system. You probably have to do this in a layer between Mathematica and the OS. BUT, it is possible to make the key behave different depending on the application you are in. Thus with a bit of work, it MAY be possible to have the capslock key behave differently only in Mathematica.
edit: I did not see you say which operating system you had, so I've added Mac instructions.
Windows
For example, if you have Windows, you can use the program called http://www.autohotkey.com/ . It specifically has a feature where you can bind a key to a script, specifically the following script:
How can a hotkey or hotstring be made exclusive to certain program(s)?
In other words, I want a certain key to act as it normally does except when a specific window is active.
In the following example, NumpadEnter is made to perform normally except when a window titled "CAD Editor" is active. Note the use of the $ prefix in "$NumpadEnter", which is required to let the hotkey "send itself":
$NumpadEnter::
IfWinNotActive, CAD Editor
{
Send, {NumpadEnter}
return
}
; Otherwise, the desired application is active, so do a custom action:
Send, abc
return
This next example is more pure than the above, but it will only work if the "CAD Editor" application is designed to ignore the NumpadEnter key itself. The tilde prefix (~) makes NumpadEnter into a non-suppressed hotkey, meaning that the NumpadEnter keystroke itself is always sent to the active window, the only difference being that it triggers a hotkey action. The ~ feature requires Windows NT/2k/XP.
~NumpadEnter::
IfWinNotActive, CAD Editor
return
; Otherwise, the desired application is active, so do a custom action:
Send, abc
return
To quote from "MRCS" in this forum post, you may find the following useful:
The first one I named CapsLockR.ahk and contains the following script:
CapsLock UP::Run C:\Documents and Sett...[path to script]...\CapsLock.ahk
The second one is named CapsLock.ahk and has this script:
GetKeyState, state, CapsLock, T
if state = D
SetCapsLockState, off
else
SetCapsLockState, on
exit
Thus worse comes to worst, if you are having trouble modifying the "Behave like Foo if Active Window = Mathematica else behave like Bar" script, you can tack on this to manually toggle the CapsLock state I think. Googling will also reveal more results.
Linux
I know that on Linux, you can use the program called xbindkeys to bind the CapsLock to a script, from which you can in turn call xdo if you detect Mathematica is one of the topmost windows (e.g. via Getting pid and details for topmost window , or xdotool getwindowfocus) or worse-comes-to-worst, you can just have a script which toggles your configuration between CapsLock -> xdotool key Escape, xdotool type "whatever", xdotool key Escape ("Mathematica mode") and "normal mode"... though that may prevent you from YELLING AT MATHEMATICIANS OVER INSTANT MESSAGING WHILE DOING MATHEMATICS. Unless you You may need to find some way to programatically toggle CapsLock, perhaps by creating a dummy CapsLock key (though that's an extreme hack, it is likely one can find some kind of library; perhaps Anybody know how to toggle caps lock on/off in Python? may be useful). (This issue could be avoided by using a key besides CapsLock, or not caring that you want to keep your CapsLock functionality; you could also just turn another key you never use into CapsLock.)
Mac
Mac may have similar tools. For example, you can get xdotool like on Linux above via the MacPorts project. I hear the CapLock key cannot normally be rebound as easily on Mac, so if you can deal with another key it may be much easier. But theoretically it should be possible...
If you wish to use CapsLock, you can use PCKeyboardHack http://pqrs.org/macosx/keyremap4macbook/extra.html to remap the CapLock key to something which will tell OS X to let you remap the CapsLock. Then you remap it, then bind the key using Quicksilver to a script that makes calls xdotool to check if you're in Mathematica also also to issue the :esc:...:esc: if you are (see the Linux section of this answer). Otherwise you simulate a keypress on the CapsLock. But you remapped CapsLock! So you might need to make another dummy key you never use into the CapsLock key, and trigger a keypress on that using Cocoa libraries or a simple AppleScript. If you wish to pursue the CapsLock route, you might find Using Caps Lock as Esc in Mac OS X useful.

OS X Terminal: Meta key + alt functionality at the same time

Is there a way to use an alt / option key as a meta key but still be able to use it to make some characters which need it?
For example, in my local keyboard layout:
# is alt + 2
\ is alt + shift + 7
| is alt + 7
etc.
So, if I set alt as a meta key, I can't make those characters anymore. On the other hand, using "press esc, release esc, press a key" to make meta key sequences makes my hands hurt.
Any Emacs users with international keyboards who have solved this, please give any tips you might have! :)
Edit:
It appears that I can set alt as a meta key and then add these kind of settings in inputrc: "\e2": "#" This works in the bash shell but it still won't work with Emacs though, so no good.
I use a Swedish keyboard in Mac OS X and use the following setup for Emacs.app:
First I have KeyRemap4MacBook installed and I have it setup to change the left Option key to Command.
I then have the following in my .emacs:
(setq mac-option-modifier 'none)
(setq mac-command-modifier 'meta)
This makes Emacs treat command as Meta and ignore Option. Since the left Alt/Option key is remapped to command, this makes it work lite meta while the right Alt/Option key still works for entering special characters like # [] and {}.
App specific Mac shortcuts like Cmd+C and Cmd+V no longer work, but global shortcuts like Cmd+space for Spotlight and Cmd+Tab for app switching do.
Update:
If you use iTerm2 as a Terminal, that has support for mapping only the left Alt/Option key to meta, which means you can still use the right AltGr/Option key for entering special characters. This also means you can use meta as normal inside Emacs running in the iTerm2 terminal.
I have a Swedish keyboard on my Mac and I'm using Aquamacs. Adding the two lines (attribution goes to Joakim Hårsman)
(setq mac-option-modifier 'none)
(setq mac-command-modifier 'meta)
in .emacs (and restarting Aquamacs) did the trick for me.
with iTerm2 you can map the left option key to ESC/meta and leave the mapping of the right option key to "normal". This way you can use the right option key to type accents etc.
Think about using a full featured Emacs for Mac OS X. I use Carbon Emacs but I believe others (say Aqua Emacs or X11 Emacs) would do the trick.
If you insist on using Emacs in a terminal you can use the C-x 8 prefix as a compose key. Try C-x 8 C-h to get a list of possibilities.
See http://www.emacswiki.org/emacs/EmacsForMacOS
Two work-arounds I use:
I use xterm running under X11.app. X11.app uses the command key as meta.
When running emacs in Terminal.app, I just use the rfc1345 input-method, which allows me to enter all kinds of crazy non-ascii characters without needing an option key.
Check out cmd-key-happy. This little app has worked wonders for me. It allows you to use command as meta, with exceptions that you can configure.
Author's info follows:
This program allows you to swap the command and alt (or option) keys
in any application, but in particular Terminal.app.
http://github.com/aim-stuff/cmd-key-happy
This is an old post but adding an alternatives for Robots to find.
I borrowed a couple of files from Aquamacs(emulate-mac-keyboard-mode.el, aquamacs-tools.el) that fixes the problem (I have a Italian-Pro keyboard) :
(defun aq-binding (any)
nil)
(load "~/.emacs.d/emulate-mac-keyboard-mode.el") ;; 'noerror
this provides:
few minor modes (emulate-mac-italian-keyboard-mode,
emulate-mac-french-keyboard-mode,
mulate-mac-swiss-german-keyboard-mode, etc)
a menu to play with all the options provided by Aquamacs
Tested with Emacs-23.3-universal-10.6.6 and seems to work ok for me.
Thanks Aquamacs for this, what about pushing this upstream if not already done.
It's worth thinking about the assumption behind the question. I mean to say that the real problem is making the characters that currently seem to need the option key. This problem can be solved without achieving simultaneous 'meta key + alt functionality'.
Let me explain: I have a similar problem. On my UK keyboard, shift-3 is mapped to the pound sign, £, which means that I generally need to hit option-3 to get the very useful # - a real problem in Terminal, where I use option as meta. However, I find most of the above solutions to be a little over-the-top. I don't want to change to another version of Emacs (eg. Aquamacs), to switch to a different Terminal application (eg. iTerm or xterm), or to install a key-remapping application (eg. KeyRemap4MacBook) all for the sake of one symbol! Perhaps that would suit some people, but I thought it was worth pointing out that if the problem is just one character, it can be solved with a nine-character text file.
With regard to my personal version of this problem, I find the simplest solution is to create a file called .inputrc in my home directory, consisting of the following single line:
"§": "#"
This re-maps the (to me, quite useless) § that can be found to the left of the 1 on the current Mac keyboard to the (almost indispensable) #. That suits me even better than the default setup, because I can now get # with a single keypress. If there were any other characters I needed to remap, it would just be a matter of adding an extra line for each one.
Anyway, that deals with how to get the missing character in the shell, but if you also want it in Emacs (which you didn't say but is probably the case) then you can add something like this to your .emacs file (where 35 is the ASCII for the character I happen to want, ie. #):
(global-set-key (kbd "§") 'insert-hash)
(defun insert-hash ()
(interactive)
(insert 35))
It might not be such a neat solution if there were dozens of characters that I routinely wanted to access via the option key, but there aren't.
I hope this helps somebody. As a mere weekend hacker, I ordinarily wouldn't dare post an answer to a question on Stack Overflow...
Special keyboard layout with option deadkey
My solution to this problem is a special keyboard layout I have written, U.S. custom. It provides an option deadkey that produces option combos without having to use the physical ⌥ Option modifier key. For instance, the character ⟨ç⟩ is produced as follows on a normal U.S. keyboard layout:
Hit ⌥ Option+c.
The U.S. custom keyboard layout adds a second way of producing ⟨ç⟩:
Hit ⇧ Shift+§ (the option deadkey), then release it, then hit c.
This works even in Terminal.app when Use option as meta key has been checked.
Note that the U.S. custom keyboard layout does two additional things:
It converts plain § into a ⎄ Compose key (so you could also produce ⟨ç⟩ by hitting §, then ,, then c).
It replaces the CapsLock mapping by a (significantly extended) U.S. Extended keyboard layout.
Remap option deadkey to right option modifier key
If you have a physical ANSI keyboard, then you lack the § key. In that case, I recommend assigning the § key to some other key, for instance to the right ⌥ Option modifier key. Install KeyRemap4MacBook, open ~/Library/Application\ Support/KeyRemap4MacBook/private.xml and paste the following code:
<?xml version="1.0"?>
<root>
<item>
<name>Send ISO Section for right Option</name>
<identifier>private.send_iso_section_for_right_option</identifier>
<autogen>--KeyToKey-- KeyCode::OPTION_R, KeyCode::UK_SECTION</autogen>
</item>
<item>
<name>Send Shift+ISO Section for Shift+right Option (keep normal right Option without Shift)</name>
<identifier>private.send_shift_iso_section_for_shift_right_option</identifier>
<autogen>--KeyToKey-- KeyCode::OPTION_R, ModifierFlag::SHIFT_R, KeyCode::UK_SECTION, ModifierFlag::SHIFT_L</autogen>
<autogen>--KeyToKey-- KeyCode::OPTION_R, ModifierFlag::SHIFT_L, KeyCode::UK_SECTION, ModifierFlag::SHIFT_L</autogen>
</item>
</root>
Then, open the KeyRemap4MacBook preference pane, hit ReloadXML and then select “Send ISO Section for right option”. Now, your ⌥ Right Option key is remapped to §, so while the U.S. custom keyboard layout is active, hitting ⇧ Shift+⌥ Right Option will give you the option deadkey state (hitting ⌥ Right Option without additional modifiers will give you a deadkey state equivalent to the option modifier.
If want to keep ⌥ Right Option when ⇧ Shift is not pressed (instead of the ⎄ Compose key), then select “Send Shift+ISO Section for Shift+right Option (keep normal right Option without Shift)” in the KeyRemap4MacBook preference pane (instead of “Send ISO Section for right Option”). Like that, only ⇧ Shift+⌥ Right Option will produce the option deadkey, while plain ⌥ Right Option will continue to work as an option modifier. You can even continue using ⌥ Right Option+⇧ Shift modifier combos (for instance ⌥ Right Option+⇧ Shift+c→⟨Ç⟩) as long as you don’t hit ⇧ Shift first.
The above answers mention ways to enter non-ascii input, but that's not really the problem here. The C-x 8 method lets you enter a bunch of non-ascii, but the problem is that you need the option key on mac just to enter the (ascii) character `|' (vertical bar)!
So how do you then enter stuff like M-| (region to shell command) when running emacs in ssh under Terminal.app? No way for it but to use the escape key, since | is option-7 and Terminal.app for some stupid reason won't let you use Cmc as meta :-(
(If anyone knows of a hack (SIMBL perhaps?) that actually does let Terminal.app use Cmd as meta, I'd be very grateful...)
The following solution works, is UNOBTRUSIVE and you don't need iTerm2 or Aquamacs or whatever.
Step 1
Keep "Use option as meta key" turned ON in the Terminal.app Settings.
Step 2
Edit ~/.inputrc (make it if it doesn't exist already), add the following line:
"\e3": '#'
This will remap ⌥ alt + 3 so that you produce a # character as expected in the terminal, instead of it annoyingly coming back with the prompt (arg: 3).
Step 3
Edit ~/.emacs, add the following line:
(global-set-key (kbd "M-3") "#")
This will remap ⌥ alt + 3 so that you produce a # character as expected inside emacs, instead of it annoyingly doing nothing.
Caveat 1, This is just 1 re-map. So ⌥ alt + ? (other keys) won't be remapped, e.g. you may wish to remap ⌥ alt + n to produce the tilde key ~. Simply follow the same steps outlined above obviously switching 3 with whichever key you need going forward.
Caveat 2, When you ssh into another box, you will face the same difficulties, but all you have to do there is edit the ~/.inputrc and ~/.emacs files on that box.
Caveat 3, I'm British, on a British MacBook. So I can't vouch for foreign MacBooks.
Aquamacs supports that, at least for my (french) keyboard. Once Aquamacs is installed (via drag'n'drop), you can go to Options -> Option, Command, Meta keys and choose an appropriate setting for your keyboard (in my case, ...Meta & French). I am not proficient with Emacs, but all the useful combinations I have tried seem to work ([, {, |, and so forth).
Supported keyboards, according to the menu items as of version 2.1, are :
British
US
Swiss-french
Swiss-german
Finnish
Italian-Pro
Italian
Spanish
French
German
I would guess the Aquamacs team would be grateful for any contribution, though, should your keyboard not appear in the list.
I bit the grass and started hitting the escape key instead. You get use to it...
I would like to mod this question up or something because i also use a Swedish keyboard where i must press alt+7 or alt+shift+7 to make | and \ respectively.
I also use Irssi in Terminal.app and these don't mix well.
I'm a long time vim user though so emacs is not an issue to me.
Isn't there some way to remap CMD to be Meta in Terminal.app?
So far i've been surviving by simply typing /wg N in Irssi because having those characters in my Terminal is much more important than IRC.
A solution would be worth gold to me though! :)
This is years late, but for people coming across this page whilst searching for a solution, as I did, I have decided to build one myself, one that doesn't require abandoning Terminal.app in favour of iTerm 2.
It is a simple status bar app that runs in the background and rewrites all left-Alt + $KEY key events to two key events in rapid succession, Esc, then $KEY; however, it only does this if Terminal.app is the focused application.
You can find it here at https://github.com/habibalamin/Metalt.
I assigned escape to the caps lock key and it works quite good. You can configure it easily in the mac system preferences > keyboard > modifier keys.
https://apple.stackexchange.com/a/282092/236382

Resources