How can I detect a key combination such as AltGr and special characters (I want to detect AltGr + ì, I have an Italian keyboard layout). Looking up online I came across SC combinations, which would be SC236, so I tried
<^>!SC236::send ~
Hotkeys can be triggered by using the hexadecimal virtual key (VK) code of a key
<^>!vkDD::send ~
in the rare case where a key has no name or the standard code doesn't work.
The virtual key code of such a key can be determined by following the steps at Special Keys. (On step 6 note the value in the first column of the list (VK)).
I am using the Japanese IME on Windows 10. TL;DR there is a shortcut to switch from alphanumeric (latin alphabet) to Japanese input. The combination is ALT+` [l or r alt + tilde key]. For it to work, both keys must be held down but the alt key must be pressed first. I am trying to bind this combination to one key.
I have been able to bind normal letter keys to a combination of other letters (e.g. a -> b + c) but I cannot get it to work with the keys I need, and especially not because there needs to be a delay between them. The code confuses me.
I am slightly confused by the difference between keys and key-bindings in emacs after reading the first chapter of "Writing GNU Emacs Extensions" (O'Reilly book).
The chapter starts by noting that in some emacs version the keys backspace and DEL, invoke the help menu instead of deleting. In other words, they invoke what is invoked by C-h.
The chapter then goes on to show how to change this behaviour using Lisp code. This is done by binding the keystroke C-h to the delete command.
This is a bit confusing to me. In my mind DEL, Backspace and C-h are three different keystrokes (the first two consisting of a single key).
Why does remapping C-h effect what DEL and Backspace does?
I would have thought to change what backspace does for example, you would remap backspace to another command, not the keystroke C-h.
Unless remapped by a low-level keybord driver, the effect of the Backspace key is to send the character with numeric code 8, which, in certain operating systems, is exactly the same code generated by pressing Control-h. You can verify this fact by simply writing anything on a unix (or linux) shell and then pressing Backspace and Control-h: both of them have the effect of erasing the previous character, since the character with numeric code 8 is interpreted by the operating system as the control character BS, backspace, used either to erase the last character printed or to overprint it (see wikipedia). Analogously, Control-J is equivalent to the RETURN key, etc.
The same Wikipedia page describe DEL as “originally intended to be an ignored character, but now used in some systems to erase a character”.
So, when you assign a command to a keystroke you are actually assigning a command to a character code, and if two or more keys generate the same code, by pressing them you are invoking the same command.
#Renzo answered your question about how these keys are related and how binding one can seem to affect another. Here is some more information about what's going on in this particular case.
See the Emacs manual, node DEL Does Not Delete.
There you will see this, following an explanation of the problem/confusion:
To fix the problem in every Emacs session, put one of the following
lines into your initialization file (*note Init File::). For the first
case above, where BACKSPACE deletes forwards instead of backwards, use
this line to make BACKSPACE act as DEL:
(normal-erase-is-backspace-mode 0)
For the other two cases, use this line:
(normal-erase-is-backspace-mode 1)
Another way to fix the problem for every Emacs session is to
customize the variable normal-erase-is-backspace: the value t
specifies the mode where BS or BACKSPACE is DEL, and nil
specifies the other mode. *Note Easy Customization::.
See also the GNU Emacs FAQ question about Backspace invoking help. There you will see, in addition to information similar to that above, information about how to remap DEL on UNIX - use this:
stty erase '^?'
Wrt C-j and RET (not mentioned in the question, but mentioned in #Renzo's answer): The default behavior of Emacs changed in most programming modes, in Emacs 24.4.
Starting with that release, electric--indent-mode is turned on by default, which means that RET inserts a newline character and indents, whereas C-j just inserts a newline character. Prior to that release, these keys had the opposite behaviors. If you prefer the old behavior then do this, to turn off electric-indent-mode:
(when (fboundp 'electric-indent-mode)
(electric-indent-mode -1)) ; Use classic `C-j' and `RET'.
I've got:
(global-set-key '[f11] 'menu-bar-mode)
How can I bind it to left ALT / Meta and will there be conflicts of using alt as meta?
You can't bind a function to a modifier key, because Emacs does not receive any input when a modifier key is pressed on its own (or more accurately, when one or more modifier keys are pressed without a non-modifier key also being pressed).
n.b. Your operating system determines which keys are modifiers, not Emacs.
You could probably tell your OS not to use the left Alt key as the Meta modifier and to instead send some sequence which you could then configure Emacs to recognise. You would want to configure some other key as the Meta modifier in that case of course, unless you are happy using right Alt exclusively.
I'm unsure precisely how you'd do that, but in a Unix-like OS reading up on xmodmap is typically the first port of call.
Is there a set of criteria to determine whether a command should be a ctrl keybinding or a meta keybinding?
For example, file handling commands seem to fall under C-x bindings. Cursor movements are a mix of ctrl and meta. I don't notice much of a pattern other than built-in bindings leaning towards ctrl which suggets meta may be somewhat reserved for user bindings.
I'm not aware of a fixed set of criteria to distinguish between commands bound to Ctrl or Meta. However, the Emacs Lisp manual has a section on Key Binding Conventions that should be interesting to you.
Meta is not officially reserved for user-defined bindings:
Sequences consisting of C-c and a letter (either upper or lower case) are reserved for users; they are the only sequences reserved for users, [...].
If you're asking because you want to set up custom key bindings for commands that don't have any by default, I suggest you also take a look at Emacs: How to choose good custom key bindings? and Are there any emacs key combinations reserved for custom commands?
Additionally, when binding keys be aware that
A <Control>-modified alphabetical character is always considered case-insensitive: Emacs always treats C-A as C-a, C-B as C-b, and so forth. [...]
For all other modifiers, you can make the modified alphabetical characters case-sensitive when you customize Emacs. For instance, you could make M-a and M-A run different commands.
... as described here.