When you say Function-F1, I assume you mean pressing that funny little Fn key in the corner of a laptop followed by the F1 key at the top left of the keyboard. And that your trying to map one of the extra keys like Sleep that some laptop keyboards have. It looks like that key is literally pressing multiple keys for you. First I see Win+L as one combo. On Windows, this shortcut locks the screen, but does not start the screensaver. Next, I see a mapping for XF86ScreenSaver which might mean that X is seeing some kind of sleep keycode. I'm guessing that that key on your laptop keyboard is physically sending out those three keycodes of Win + L + Sleep as a convenience for Windows users to quickly lock the screen and activate the screensaver. I'm not sure of a good way to filter those out. The Fn key on keyboards normally is not seen by the Linux Kernel. Instead, it changes which codes the keyboard tells the kernel. When I was trying to use a USB RF PowerPoint remote with OOo on Linux, I discovered that the button to start/stop the presentation was just a lame control to send out alternating F5 and ESC key codes. Other buttons were just as lame like sending out b to blank the screen.
The interface between a terminal application and a terminal emulator (or hardware terminal) transmits bytes, not keys. Function keys such as cursor movement keys are translated into escape sequences (beginning with the escape character ESC a.k.a. \e
a.k.a. \033
a.k.a. 0x1b a.k.a. ^[). The same goes for combinations of a function key or a character key with modifiers, though not all terminals send different sequences for all the different modifier combinations. A few keys are sent encoded as control characters (e.g. Tab → Ctrl-I = \t
= \011
).
As you can see, there are many ways to describe control characters. Some have a name, corresponding to their traditional function (e.g. Tab, Line feed); those tend to have a backslash+letter combination that you can use inside $'…'
or in an argument to echo
or print
(as well as in sed regular expressions and in string literals in awk, C and other languages (note that different tools may have a slightly different set of escape sequences)). You can use backslash+octal (e.g. \033
) in these contexts as well.
There is some variation as to which escape sequence terminals send for each key. Fortunately, there is almost no overlap: there are very few character sequences that mean different keys on different terminal. The main problem is character 127 = \177
= 0x7f which is most often Backspace nowadays but sometimes Delete.
^[OF
and ^[[F
(i.e. \eOF
and \e[F
) are the two common escape sequences sent by End. ^E
(i.e. \005
) is the Emacs key binding (Ctrl+E) for end-of-line
.
To see what your terminal emulator sends for a particular key or key combination, press Ctrl+V and then the key in question. This inserts the first character of the escape sequence literally. Escape sequences normally consist of an escape character followed by printable characters, so the rest of the escape sequence is inserted literally too.
The Terminfo database contains the escape sequences for some keys. You'll find the list of Terminfo capabilities in the terminfo(5) man page on your system. In zsh, you can list the values in the database through the terminfo
associative array. Beware when printing out values that they contain escape sequences which are also interpreted by the terminal when displayed, so print them in a quoted form.
% print -lr ${(q)terminfo[kend]}
$'\033'OF
See How do keyboard input and text output work? for a more complete overview of what happens when you press a key. It isn't necessary to understand key bindings in zsh.
Best Answer
^c
is a common notation for Ctrl+c where c is a (uppercase) letter or one of@[\]^_
. It designates the corresponding control character. The correspondence is that the numeric code of the control character is the numeric code of the printable character (letter or punctuation symbol) minus 64, which corresponds to setting a bit to 0 in base 2. In addition,^?
often means character 127.Some keys send a control character:
Alt (often called Meta because that was the name of the key at that position on historical Unix machines) plus a printable character sends
^[
(escape) followed by that character.Most function and cursor keys send an escape sequence, i.e. the character
^[
followed by some printable characters. The details depend on the terminal and its configuration. For xterm, the defaults are documented in the manual. The manual is not beginner-friendly. Here are some tips to help:^[[
, i.e. escape followed by open-bracket.^[O
, i.e. escape followed by uppercase-O.^[[15~
, and Shift+F5 sends^[[15;2~
. For cursor keys that send^[[
and one letter X, to indicate a modifier M, the escape sequence is^[[1;MX
.Xterm follows an ANSI standard which itself is based on historical usage dating back from physical terminals. Most modern terminal emulators follow that ANSI standard and implement some but not all of xterm's extensions. Do expect minor variations between terminals though.
Thus:
^X^I
= Ctrl+X Ctrl+I = Ctrl+X Tab^[^@
= Ctrl+Alt+@ = Escape Ctrl+@. On most terminals, Ctrl+Space also sends^@
so^[^@
= Ctrl+Alt+Space = Escape Ctrl+Space.^X^[q
= Ctrl+X Alt+q = Ctrl+X Escape q^XQ
= Ctrl+X Shift+q^[[A
= Up^[[1;3A
= Alt+Up (Up, with1;M
to indicate the modifier M). Note that many terminals don't actually send these escape sequences for Alt+cursor key.^[[1;3D
= Alt+Left^[[1;5C
= Ctrl+RightThere's no general, convenient way to look up the key corresponding to an escape sequence. The other way round, pressing Ctrl+V followed by a key chord at a shell prompt (or in many terminal-based editors) inserts the escape sequence literally.
See also How do keyboard input and text output work? and key bindings table?