keyboard-protocol.rst 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757
  1. Comprehensive keyboard handling in terminals
  2. ==============================================
  3. There are various problems with the current state of keyboard handling in
  4. terminals. They include:
  5. * No way to use modifiers other than ``ctrl`` and ``alt``
  6. * No way to reliably use multiple modifier keys, other than, ``shift+alt`` and
  7. ``ctrl+alt``.
  8. * Many of the existing escape codes used to encode these events are ambiguous
  9. with different key presses mapping to the same escape code.
  10. * No way to handle different types of keyboard events, such as press, release or repeat
  11. * No reliable way to distinguish single ``Esc`` key presses from the start of a
  12. escape sequence. Currently, client programs use fragile timing related hacks
  13. for this, leading to bugs, for example:
  14. `neovim #2035 <https://github.com/neovim/neovim/issues/2035>`_.
  15. To solve these issues and others, kitty has created a new keyboard protocol,
  16. that is backward compatible but allows applications to opt-in to support more
  17. advanced usages. The protocol is based on initial work in `fixterms
  18. <http://www.leonerd.org.uk/hacks/fixterms/>`_, however, it corrects various
  19. issues in that proposal, listed at the :ref:`bottom of this document
  20. <fixterms_bugs>`. For public discussion of this spec, see :iss:`3248`.
  21. You can see this protocol with all enhancements in action by running::
  22. kitten show-key -m kitty
  23. inside the kitty terminal to report key events.
  24. In addition to kitty, this protocol is also implemented in:
  25. * The `foot terminal <https://codeberg.org/dnkl/foot/issues/319>`__
  26. * The `WezTerm terminal <https://wezfurlong.org/wezterm/config/lua/config/enable_kitty_keyboard.html>`__
  27. * The `alacritty terminal <https://github.com/alacritty/alacritty/pull/7125>`__
  28. * The `rio terminal <https://github.com/raphamorim/rio/commit/cd463ca37677a0fc48daa8795ea46dadc92b1e95>`__
  29. * The `iTerm2 terminal <https://gitlab.com/gnachman/iterm2/-/issues/10017>`__
  30. Libraries implementing this protocol:
  31. * The `notcurses library <https://github.com/dankamongmen/notcurses/issues/2131>`__
  32. * The `crossterm library <https://github.com/crossterm-rs/crossterm/pull/688>`__
  33. * The `textual library <https://github.com/Textualize/textual/pull/4631>`__
  34. * The vaxis library `go <https://sr.ht/~rockorager/vaxis/>`__ and `zig <https://github.com/rockorager/libvaxis/>`__
  35. Programs implementing this protocol:
  36. * The `Vim text editor <https://github.com/vim/vim/commit/63a2e360cca2c70ab0a85d14771d3259d4b3aafa>`__
  37. * The `Emacs text editor via the kkp package <https://github.com/benjaminor/kkp>`__
  38. * The `Neovim text editor <https://github.com/neovim/neovim/pull/18181>`__
  39. * The `kakoune text editor <https://github.com/mawww/kakoune/issues/4103>`__
  40. * The `dte text editor <https://gitlab.com/craigbarnes/dte/-/issues/138>`__
  41. * The `Helix text editor <https://github.com/helix-editor/helix/pull/4939>`__
  42. * The `far2l file manager <https://github.com/elfmz/far2l/commit/e1f2ee0ef2b8332e5fa3ad7f2e4afefe7c96fc3b>`__
  43. * The `Yazi file manager <https://github.com/sxyazi/yazi>`__
  44. * The `awrit web browser <https://github.com/chase/awrit>`__
  45. * The `Turbo Vision <https://github.com/magiblot/tvision/commit/6e5a7b46c6634079feb2ac98f0b890bbed59f1ba>`__/`Free Vision <https://gitlab.com/freepascal.org/fpc/source/-/issues/40673#note_2061428120>`__ IDEs
  46. * The `aerc email client <https://git.sr.ht/~rjarry/aerc/commit/d73cf33c2c6c3e564ce8aff04acc329a06eafc54>`__
  47. Shells implementing this protocol:
  48. * The `nushell shell <https://github.com/nushell/nushell/pull/10540>`__
  49. * The `fish shell <https://github.com/fish-shell/fish-shell/commit/8bf8b10f685d964101f491b9cc3da04117a308b4>`__
  50. .. versionadded:: 0.20.0
  51. Quickstart
  52. ---------------
  53. If you are an application or library developer just interested in using this
  54. protocol to make keyboard handling simpler and more robust in your application,
  55. without too many changes, do the following:
  56. #. Emit the escape code ``CSI > 1 u`` at application startup if using the main
  57. screen or when entering alternate screen mode, if using the alternate
  58. screen.
  59. #. All key events will now be sent in only a few forms to your application,
  60. that are easy to parse unambiguously.
  61. #. Emit the escape sequence ``CSI < u`` at application exit if using the main
  62. screen or just before leaving alternate screen mode if using the alternate screen,
  63. to restore whatever the keyboard mode was before step 1.
  64. Key events will all be delivered to your application either as plain UTF-8
  65. text, or using the following escape codes, for those keys that do not produce
  66. text (``CSI`` is the bytes ``0x1b 0x5b``)::
  67. CSI number ; modifiers [u~]
  68. CSI 1; modifiers [ABCDEFHPQS]
  69. 0x0d - for the Enter key
  70. 0x7f or 0x08 - for Backspace
  71. 0x09 - for Tab
  72. The ``number`` in the first form above will be either the Unicode codepoint for a
  73. key, such as ``97`` for the :kbd:`a` key, or one of the numbers from the
  74. :ref:`functional` table below. The ``modifiers`` optional parameter encodes any
  75. modifiers active for the key event. The encoding is described in the
  76. :ref:`modifiers` section.
  77. The second form is used for a few functional keys, such as the :kbd:`Home`,
  78. :kbd:`End`, :kbd:`Arrow` keys and :kbd:`F1` ... :kbd:`F4`, they are enumerated in
  79. the :ref:`functional` table below. Note that if no modifiers are present the
  80. parameters are omitted entirely giving an escape code of the form ``CSI
  81. [ABCDEFHPQS]``.
  82. If you want support for more advanced features such as repeat and release
  83. events, alternate keys for shortcut matching et cetera, these can be turned on
  84. using :ref:`progressive_enhancement` as documented in the rest of this
  85. specification.
  86. An overview
  87. ------------------
  88. Key events are divided into two types, those that produce text and those that
  89. do not. When a key event produces text, the text is sent directly as UTF-8
  90. encoded bytes. This is safe as UTF-8 contains no C0 control codes.
  91. When the key event does not have text, the key event is encoded as an escape code. In
  92. legacy compatibility mode (the default) this uses legacy escape codes, so old terminal
  93. applications continue to work. For more advanced features, such as release/repeat
  94. reporting etc., applications can tell the terminal they want this information by
  95. sending an escape code to :ref:`progressively enhance <progressive_enhancement>` the data reported for
  96. key events.
  97. The central escape code used to encode key events is::
  98. CSI unicode-key-code:alternate-key-codes ; modifiers:event-type ; text-as-codepoints u
  99. Spaces in the above definition are present for clarity and should be ignored.
  100. ``CSI`` is the bytes ``0x1b 0x5b``. All parameters are decimal numbers. Fields
  101. are separated by the semi-colon and sub-fields by the colon. Only the
  102. ``unicode-key-code`` field is mandatory, everything else is optional. The
  103. escape code is terminated by the ``u`` character (the byte ``0x75``).
  104. .. _key_codes:
  105. Key codes
  106. ~~~~~~~~~~~~~~
  107. The ``unicode-key-code`` above is the Unicode codepoint representing the key, as a
  108. decimal number. For example, the :kbd:`A` key is represented as ``97`` which is
  109. the unicode code for lowercase ``a``. Note that the codepoint used is *always*
  110. the lower-case (or more technically, un-shifted) version of the key. If the
  111. user presses, for example, :kbd:`ctrl+shift+a` the escape code would be ``CSI
  112. 97;modifiers u``. It *must not* be ``CSI 65; modifiers u``.
  113. If *alternate key reporting* is requested by the program running in the
  114. terminal, the terminal can send two additional Unicode codepoints, the
  115. *shifted key* and *base layout key*, separated by colons.
  116. The shifted key is simply the upper-case version of ``unicode-codepoint``, or
  117. more technically, the shifted version. So `a` becomes `A` and so on, based on
  118. the current keyboard layout. This is needed to be able to match against a
  119. shortcut such as :kbd:`ctrl+plus` which depending on the type of keyboard could
  120. be either :kbd:`ctrl+shift+equal` or :kbd:`ctrl+plus`. Note that the shifted
  121. key must be present only if shift is also present in the modifiers.
  122. The *base layout key* is the key corresponding to the physical key in the
  123. standard PC-101 key layout. So for example, if the user is using a Cyrillic
  124. keyboard with a Cyrillic keyboard layout pressing the :kbd:`ctrl+С` key will
  125. be :kbd:`ctrl+c` in the standard layout. So the terminal should send the *base
  126. layout key* as ``99`` corresponding to the ``c`` key.
  127. If only one alternate key is present, it is the *shifted key* if the terminal
  128. wants to send only a base layout key but no shifted key, it must use an empty
  129. sub-field for the shifted key, like this::
  130. CSI unicode-key-code::base-layout-key
  131. .. _modifiers:
  132. Modifiers
  133. ~~~~~~~~~~~~~~
  134. This protocol supports six modifier keys, :kbd:`shift`, :kbd:`alt`,
  135. :kbd:`ctrl`, :kbd:`super`, :kbd:`hyper`, :kbd:`meta`, :kbd:`num_lock` and
  136. :kbd:`caps_lock`. Here :kbd:`super` is either the *Windows/Linux* key or the
  137. :kbd:`command` key on mac keyboards. The :kbd:`alt` key is the :kbd:`option`
  138. key on mac keyboards. :kbd:`hyper` and :kbd:`meta` are typically present only
  139. on X11/Wayland based systems with special XKB rules. Modifiers are encoded as a
  140. bit field with::
  141. shift 0b1 (1)
  142. alt 0b10 (2)
  143. ctrl 0b100 (4)
  144. super 0b1000 (8)
  145. hyper 0b10000 (16)
  146. meta 0b100000 (32)
  147. caps_lock 0b1000000 (64)
  148. num_lock 0b10000000 (128)
  149. In the escape code, the modifier value is encoded as a decimal number which is
  150. ``1 + actual modifiers``. So to represent :kbd:`shift` only, the value would be
  151. ``1 + 1 = 2``, to represent :kbd:`ctrl+shift` the value would be ``1 + 0b101 =
  152. 6`` and so on. If the modifier field is not present in the escape code, its
  153. default value is ``1`` which means no modifiers. If a modifier is *active* when
  154. the key event occurs, i.e. if the key is pressed or the lock (for caps lock/num
  155. lock) is enabled, the key event must have the bit for that modifier set.
  156. When the key event is related to an actual modifier key, the corresponding
  157. modifier's bit must be set to the modifier state including the effect for the
  158. current event. For example, when pressing the :kbd:`LEFT_CONTROL` key, the
  159. ``ctrl`` bit must be set and when releasing it, it must be reset. When both
  160. left and right control keys are pressed and one is released, the release event
  161. must have the ``ctrl`` bit set. See :iss:`6913` for discussion of this design.
  162. .. _event_types:
  163. Event types
  164. ~~~~~~~~~~~~~~~~
  165. There are three key event types: ``press, repeat and release``. They are
  166. reported (if requested ``0b10``) as a sub-field of the modifiers field
  167. (separated by a colon). If no modifiers are present, the modifiers field must
  168. have the value ``1`` and the event type sub-field the type of event. The
  169. ``press`` event type has value ``1`` and is the default if no event type sub
  170. field is present. The ``repeat`` type is ``2`` and the ``release`` type is
  171. ``3``. So for example::
  172. CSI key-code # this is a press event
  173. CSI key-code;modifier # this is a press event
  174. CSI key-code;modifier:1 # this is a press event
  175. CSI key-code;modifier:2 # this is a repeat event
  176. CSI key-code;modifier:3 # this is a release event
  177. .. note:: Key events that result in text are reported as plain UTF-8 text, so
  178. events are not supported for them, unless the application requests *key
  179. report mode*, see below.
  180. .. _text_as_codepoints:
  181. Text as code points
  182. ~~~~~~~~~~~~~~~~~~~~~
  183. The terminal can optionally send the text associated with key events as a
  184. sequence of Unicode code points. This behavior is opt-in by the :ref:`progressive
  185. enhancement <progressive_enhancement>` mechanism described below. Some examples::
  186. shift+a -> CSI 97 ; 2 ; 65 u # The text 'A' is reported as 65
  187. option+a -> CSI 97 ; ; 229 u # The text 'å' is reported as 229
  188. If multiple code points are present, they must be separated by colons. If no
  189. known key is associated with the text the key number ``0`` must be used. The
  190. associated text must not contain control codes (control codes are code points
  191. below U+0020 and codepoints in the C0 and C1 blocks).
  192. Non-Unicode keys
  193. ~~~~~~~~~~~~~~~~~~~~~~~
  194. There are many keys that don't correspond to letters from human languages, and
  195. thus aren't represented in Unicode. Think of functional keys, such as
  196. :kbd:`Escape`, :kbd:`Play`, :kbd:`Pause`, :kbd:`F1`, :kbd:`Home`, etc. These
  197. are encoded using Unicode code points from the Private Use Area (``57344 -
  198. 63743``). The mapping of key names to code points for these keys is in the
  199. :ref:`Functional key definition table below <functional>`.
  200. .. _progressive_enhancement:
  201. Progressive enhancement
  202. --------------------------
  203. While, in theory, every key event could be completely represented by this
  204. protocol and all would be hunk-dory, in reality there is a vast universe of
  205. existing terminal programs that expect legacy control codes for key events and
  206. that are not likely to ever be updated. To support these, in default mode,
  207. the terminal will emit legacy escape codes for compatibility. If a terminal
  208. program wants more robust key handling, it can request it from the terminal,
  209. via the mechanism described here. Each enhancement is described in detail
  210. below. The escape code for requesting enhancements is::
  211. CSI = flags ; mode u
  212. Here ``flags`` is a decimal encoded integer to specify a set of bit-flags. The
  213. meanings of the flags are given below. The second, ``mode`` parameter is
  214. optional (defaulting to ``1``) and specifies how the flags are applied.
  215. The value ``1`` means all set bits are set and all unset bits are reset.
  216. The value ``2`` means all set bits are set, unset bits are left unchanged.
  217. The value ``3`` means all set bits are reset, unset bits are left unchanged.
  218. .. csv-table:: The progressive enhancement flags
  219. :header: "Bit", "Meaning"
  220. "0b1 (1)", ":ref:`disambiguate`"
  221. "0b10 (2)", ":ref:`report_events`"
  222. "0b100 (4)", ":ref:`report_alternates`"
  223. "0b1000 (8)", ":ref:`report_all_keys`"
  224. "0b10000 (16)", ":ref:`report_text`"
  225. The program running in the terminal can query the terminal for the
  226. current values of the flags by sending::
  227. CSI ? u
  228. The terminal will reply with::
  229. CSI ? flags u
  230. The program can also push/pop the current flags onto a stack in the
  231. terminal with::
  232. CSI > flags u # for push, if flags omitted default to zero
  233. CSI < number u # to pop number entries, defaulting to 1 if unspecified
  234. Terminals should limit the size of the stack as appropriate, to prevent
  235. Denial-of-Service attacks. Terminals must maintain separate stacks for the main
  236. and alternate screens. If a pop request is received that empties the stack,
  237. all flags are reset. If a push request is received and the stack is full, the
  238. oldest entry from the stack must be evicted.
  239. .. note:: The main and alternate screens in the terminal emulator must maintain
  240. their own, independent, keyboard mode stacks. This is so that a program that
  241. uses the alternate screen such as an editor, can change the keyboard mode
  242. in the alternate screen only, without affecting the mode in the main screen
  243. or even knowing what that mode is. Without this, and if no stack is
  244. implemented for keyboard modes (such as in some legacy terminal emulators)
  245. the editor would have to somehow know what the keyboard mode of the main
  246. screen is and restore to that mode on exit.
  247. .. _disambiguate:
  248. Disambiguate escape codes
  249. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  250. This type of progressive enhancement (``0b1``) fixes the problem of some legacy key press
  251. encodings overlapping with other control codes. For instance, pressing the
  252. :kbd:`Esc` key generates the byte ``0x1b`` which also is used to indicate the
  253. start of an escape code. Similarly pressing the key :kbd:`alt+[` will generate
  254. the bytes used for CSI control codes.
  255. Turning on this flag will cause the terminal to report the :kbd:`Esc`, :kbd:`alt+key`,
  256. :kbd:`ctrl+key`, :kbd:`ctrl+alt+key`, :kbd:`shift+alt+key` keys using ``CSI u`` sequences instead
  257. of legacy ones. Here key is any ASCII key as described in :ref:`legacy_text`.
  258. Additionally, all keypad keys will be reported as separate keys with ``CSI u``
  259. encoding, using dedicated numbers from the :ref:`table below <functional>`.
  260. With this flag turned on, all key events that do not generate text are
  261. represented in one of the following two forms::
  262. CSI number; modifier u
  263. CSI 1; modifier [~ABCDEFHPQS]
  264. This makes it very easy to parse key events in an application. In particular,
  265. :kbd:`ctrl+c` will no longer generate the ``SIGINT`` signal, but instead be
  266. delivered as a ``CSI u`` escape code. This has the nice side effect of making it
  267. much easier to integrate into the application event loop. The only exceptions
  268. are the :kbd:`Enter`, :kbd:`Tab` and :kbd:`Backspace` keys which still generate the same
  269. bytes as in legacy mode this is to allow the user to type and execute commands
  270. in the shell such as ``reset`` after a program that sets this mode crashes
  271. without clearing it. Note that the Lock modifiers are not reported for text
  272. producing keys, to keep them useable in legacy programs. To get lock modifiers
  273. for all keys use the :ref:`report_all_keys` enhancement.
  274. .. _report_events:
  275. Report event types
  276. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  277. This progressive enhancement (``0b10``) causes the terminal to report key repeat
  278. and key release events. Normally only key press events are reported and key
  279. repeat events are treated as key press events. See :ref:`event_types` for
  280. details on how these are reported.
  281. .. note::
  282. The :kbd:`Enter`, :kbd:`Tab` and :kbd:`Backspace` keys will not have release
  283. events unless :ref:`report_all_keys` is also set, so that the user can still
  284. type reset at a shell prompt when a program that sets this mode ends without
  285. resetting it.
  286. .. _report_alternates:
  287. Report alternate keys
  288. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  289. This progressive enhancement (``0b100``) causes the terminal to report
  290. alternate key values in addition to the main value, to aid in shortcut
  291. matching. See :ref:`key_codes` for details on how these are reported.
  292. .. _report_all_keys:
  293. Report all keys as escape codes
  294. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  295. Key events that generate text, such as plain key presses without modifiers,
  296. result in just the text being sent, in the legacy protocol. There is no way to
  297. be notified of key repeat/release events. These types of events are needed for
  298. some applications, such as games (think of movement using the ``WASD`` keys).
  299. This progressive enhancement (``0b1000``) turns on key reporting even for key
  300. events that generate text. When it is enabled, text will not be sent, instead
  301. only key events are sent. If the text is needed as well, combine with the
  302. Report associated text enhancement below.
  303. Additionally, with this mode, events for pressing modifier keys are reported.
  304. Note that *all* keys are reported as escape codes, including :kbd:`Enter`,
  305. :kbd:`Tab`, :kbd:`Backspace` etc.
  306. .. _report_text:
  307. Report associated text
  308. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  309. This progressive enhancement (``0b10000``) causes key events that generate text
  310. to be reported as ``CSI u`` escape codes with the text embedded in the escape
  311. code. See :ref:`text_as_codepoints` above for details on the mechanism.
  312. .. _detection:
  313. Detection of support for this protocol
  314. ------------------------------------------
  315. An application can query the terminal for support of this protocol by sending
  316. the escape code querying for the :ref:`current progressive enhancement
  317. <progressive_enhancement>` status
  318. followed by request for the `primary device attributes
  319. <https://vt100.net/docs/vt510-rm/DA1.html>`__. If an answer for the device
  320. attributes is received without getting back an answer for the progressive
  321. enhancement the terminal does not support this protocol.
  322. Legacy key event encoding
  323. --------------------------------
  324. In the default mode, the terminal uses a legacy encoding for key events. In
  325. this encoding, only key press and repeat events are sent and there is no
  326. way to distinguish between them. Text is sent directly as UTF-8 bytes.
  327. Any key events not described in this section are sent using the standard
  328. ``CSI u`` encoding. This includes keys that are not encodable in the legacy
  329. encoding, thereby increasing the space of usable key combinations even without
  330. progressive enhancement.
  331. Legacy functional keys
  332. ~~~~~~~~~~~~~~~~~~~~~~~~
  333. These keys are encoded using three schemes::
  334. CSI number ; modifier ~
  335. CSI 1 ; modifier {ABCDEFHPQS}
  336. SS3 {ABCDEFHPQRS}
  337. In the above, if there are no modifiers, the modifier parameter is omitted.
  338. The modifier value is encoded as described in the :ref:`modifiers` section,
  339. above. When the second form is used, the number is always ``1`` and must be
  340. omitted if the modifiers field is also absent. The third form becomes the
  341. second form when modifiers are present (``SS3 is the bytes 0x1b 0x4f``).
  342. These sequences must match entries in the terminfo database for maximum
  343. compatibility. The table below lists the key, its terminfo entry name and
  344. the escape code used for it by kitty. A different terminal would use whatever
  345. escape code is present in its terminfo database for the key.
  346. Some keys have an alternate representation when the terminal is in *cursor key
  347. mode* (the ``smkx/rmkx`` terminfo capabilities). This form is used only in
  348. *cursor key mode* and only when no modifiers are present.
  349. .. csv-table:: Legacy functional encoding
  350. :header: "Name", "Terminfo name", "Escape code"
  351. "INSERT", "kich1", "CSI 2 ~"
  352. "DELETE", "kdch1", "CSI 3 ~"
  353. "PAGE_UP", "kpp", "CSI 5 ~"
  354. "PAGE_DOWN", "knp", "CSI 6 ~"
  355. "UP", "cuu1,kcuu1", "CSI A, SS3 A"
  356. "DOWN", "cud1,kcud1", "CSI B, SS3 B"
  357. "RIGHT", "cuf1,kcuf1", "CSI C, SS3 C"
  358. "LEFT", "cub1,kcub1", "CSI D, SS3 D"
  359. "HOME", "home,khome", "CSI H, SS3 H"
  360. "END", "-,kend", "CSI F, SS3 F"
  361. "F1", "kf1", "SS3 P"
  362. "F2", "kf2", "SS3 Q"
  363. "F3", "kf3", "SS3 R"
  364. "F4", "kf4", "SS3 S"
  365. "F5", "kf5", "CSI 15 ~"
  366. "F6", "kf6", "CSI 17 ~"
  367. "F7", "kf7", "CSI 18 ~"
  368. "F8", "kf8", "CSI 19 ~"
  369. "F9", "kf9", "CSI 20 ~"
  370. "F10", "kf10", "CSI 21 ~"
  371. "F11", "kf11", "CSI 23 ~"
  372. "F12", "kf12", "CSI 24 ~"
  373. "MENU", "kf16", "CSI 29 ~"
  374. There are a few more functional keys that have special cased legacy encodings.
  375. These are present because they are commonly used and for the sake of legacy
  376. terminal applications that get confused when seeing CSI u escape codes:
  377. .. csv-table:: C0 controls
  378. :header: "Key", "No mods", "Ctrl", "Alt", "Shift", "Ctrl + Shift", "Alt + Shift", "Ctrl + Alt"
  379. "Enter", "0xd", "0xd", "0x1b 0xd", "0xd", "0xd", "0x1b 0xd", "0x1b 0xd"
  380. "Escape", "0x1b", "0x1b", "0x1b 0x1b", "0x1b", "0x1b", "0x1b 0x1b", "0x1b 0x1b"
  381. "Backspace", "0x7f", "0x8", "0x1b 0x7f", "0x7f", "0x8", "0x1b 0x7f", "0x1b 0x8"
  382. "Tab", "0x9", "0x9", "0x1b 0x9", "CSI Z", "CSI Z", "0x1b CSI Z", "0x1b 0x9"
  383. "Space", "0x20", "0x0", "0x1b 0x20", "0x20", "0x0", "0x1b 0x20", "0x1b 0x0"
  384. Note that :kbd:`Backspace` and :kbd:`ctrl+Backspace` are swapped in some
  385. terminals, this can be detected using the ``kbs`` terminfo property that
  386. must correspond to the :kbd:`Backspace` key.
  387. All keypad keys are reported as their equivalent non-keypad keys. To
  388. distinguish these, use the :ref:`disambiguate <disambiguate>` flag.
  389. Terminals may choose what they want to do about functional keys that have no
  390. legacy encoding. kitty chooses to encode these using ``CSI u`` encoding even in
  391. legacy mode, so that they become usable even in programs that do not
  392. understand the full kitty keyboard protocol. However, terminals may instead choose to
  393. ignore such keys in legacy mode instead, or have an option to control this behavior.
  394. .. _legacy_text:
  395. Legacy text keys
  396. ~~~~~~~~~~~~~~~~~~~
  397. For legacy compatibility, the keys :kbd:`a`-:kbd:`z` :kbd:`0`-:kbd:`9`
  398. :kbd:`\`` :kbd:`-` :kbd:`=` :kbd:`[` :kbd:`]` :kbd:`\\` :kbd:`;` :kbd:`'`
  399. :kbd:`,` :kbd:`.` :kbd:`/` with the modifiers :kbd:`shift`, :kbd:`alt`,
  400. :kbd:`ctrl`, :kbd:`shift+alt`, :kbd:`ctrl+alt` are output using the following
  401. algorithm:
  402. #. If the :kbd:`alt` key is pressed output the byte for ``ESC (0x1b)``
  403. #. If the :kbd:`ctrl` modifier is pressed map the key using the table
  404. in :ref:`ctrl_mapping`.
  405. #. Otherwise, if the :kbd:`shift` modifier is pressed, output the shifted key,
  406. for example, ``A`` for ``a`` and ``$`` for ``4``.
  407. #. Otherwise, output the key unmodified
  408. Additionally, :kbd:`ctrl+space` is output as the NULL byte ``(0x0)``.
  409. Any other combination of modifiers with these keys is output as the appropriate
  410. ``CSI u`` escape code.
  411. .. csv-table:: Example encodings
  412. :header: "Key", "Plain", "shift", "alt", "ctrl", "shift+alt", "alt+ctrl", "ctrl+shift"
  413. "i", "i (105)", "I (73)", "ESC i", ") (41)", "ESC I", "ESC )", "CSI 105; 6 u"
  414. "3", "3 (51)", "# (35)", "ESC 3", "3 (51)", "ESC #", "ESC 3", "CSI 51; 6 u"
  415. ";", "; (59)", ": (58)", "ESC ;", "; (59)", "ESC :", "ESC ;", "CSI 59; 6 u"
  416. .. note::
  417. Many of the legacy escape codes are ambiguous with multiple different key
  418. presses yielding the same escape code(s), for example, :kbd:`ctrl+i` is the
  419. same as :kbd:`tab`, :kbd:`ctrl+m` is the same as :kbd:`Enter`, :kbd:`ctrl+r`
  420. is the same :kbd:`ctrl+shift+r`, etc. To resolve these use the
  421. :ref:`disambiguate progressive enhancement <disambiguate>`.
  422. .. _functional:
  423. Functional key definitions
  424. ----------------------------
  425. All numbers are in the Unicode Private Use Area (``57344 - 63743``) except
  426. for a handful of keys that use numbers under 32 and 127 (C0 control codes) for legacy
  427. compatibility reasons.
  428. .. {{{
  429. .. start functional key table (auto generated by gen-key-constants.py do not edit)
  430. .. csv-table:: Functional key codes
  431. :header: "Name", "CSI", "Name", "CSI"
  432. "ESCAPE", "``27 u``", "ENTER", "``13 u``"
  433. "TAB", "``9 u``", "BACKSPACE", "``127 u``"
  434. "INSERT", "``2 ~``", "DELETE", "``3 ~``"
  435. "LEFT", "``1 D``", "RIGHT", "``1 C``"
  436. "UP", "``1 A``", "DOWN", "``1 B``"
  437. "PAGE_UP", "``5 ~``", "PAGE_DOWN", "``6 ~``"
  438. "HOME", "``1 H or 7 ~``", "END", "``1 F or 8 ~``"
  439. "CAPS_LOCK", "``57358 u``", "SCROLL_LOCK", "``57359 u``"
  440. "NUM_LOCK", "``57360 u``", "PRINT_SCREEN", "``57361 u``"
  441. "PAUSE", "``57362 u``", "MENU", "``57363 u``"
  442. "F1", "``1 P or 11 ~``", "F2", "``1 Q or 12 ~``"
  443. "F3", "``13 ~``", "F4", "``1 S or 14 ~``"
  444. "F5", "``15 ~``", "F6", "``17 ~``"
  445. "F7", "``18 ~``", "F8", "``19 ~``"
  446. "F9", "``20 ~``", "F10", "``21 ~``"
  447. "F11", "``23 ~``", "F12", "``24 ~``"
  448. "F13", "``57376 u``", "F14", "``57377 u``"
  449. "F15", "``57378 u``", "F16", "``57379 u``"
  450. "F17", "``57380 u``", "F18", "``57381 u``"
  451. "F19", "``57382 u``", "F20", "``57383 u``"
  452. "F21", "``57384 u``", "F22", "``57385 u``"
  453. "F23", "``57386 u``", "F24", "``57387 u``"
  454. "F25", "``57388 u``", "F26", "``57389 u``"
  455. "F27", "``57390 u``", "F28", "``57391 u``"
  456. "F29", "``57392 u``", "F30", "``57393 u``"
  457. "F31", "``57394 u``", "F32", "``57395 u``"
  458. "F33", "``57396 u``", "F34", "``57397 u``"
  459. "F35", "``57398 u``", "KP_0", "``57399 u``"
  460. "KP_1", "``57400 u``", "KP_2", "``57401 u``"
  461. "KP_3", "``57402 u``", "KP_4", "``57403 u``"
  462. "KP_5", "``57404 u``", "KP_6", "``57405 u``"
  463. "KP_7", "``57406 u``", "KP_8", "``57407 u``"
  464. "KP_9", "``57408 u``", "KP_DECIMAL", "``57409 u``"
  465. "KP_DIVIDE", "``57410 u``", "KP_MULTIPLY", "``57411 u``"
  466. "KP_SUBTRACT", "``57412 u``", "KP_ADD", "``57413 u``"
  467. "KP_ENTER", "``57414 u``", "KP_EQUAL", "``57415 u``"
  468. "KP_SEPARATOR", "``57416 u``", "KP_LEFT", "``57417 u``"
  469. "KP_RIGHT", "``57418 u``", "KP_UP", "``57419 u``"
  470. "KP_DOWN", "``57420 u``", "KP_PAGE_UP", "``57421 u``"
  471. "KP_PAGE_DOWN", "``57422 u``", "KP_HOME", "``57423 u``"
  472. "KP_END", "``57424 u``", "KP_INSERT", "``57425 u``"
  473. "KP_DELETE", "``57426 u``", "KP_BEGIN", "``1 E or 57427 ~``"
  474. "MEDIA_PLAY", "``57428 u``", "MEDIA_PAUSE", "``57429 u``"
  475. "MEDIA_PLAY_PAUSE", "``57430 u``", "MEDIA_REVERSE", "``57431 u``"
  476. "MEDIA_STOP", "``57432 u``", "MEDIA_FAST_FORWARD", "``57433 u``"
  477. "MEDIA_REWIND", "``57434 u``", "MEDIA_TRACK_NEXT", "``57435 u``"
  478. "MEDIA_TRACK_PREVIOUS", "``57436 u``", "MEDIA_RECORD", "``57437 u``"
  479. "LOWER_VOLUME", "``57438 u``", "RAISE_VOLUME", "``57439 u``"
  480. "MUTE_VOLUME", "``57440 u``", "LEFT_SHIFT", "``57441 u``"
  481. "LEFT_CONTROL", "``57442 u``", "LEFT_ALT", "``57443 u``"
  482. "LEFT_SUPER", "``57444 u``", "LEFT_HYPER", "``57445 u``"
  483. "LEFT_META", "``57446 u``", "RIGHT_SHIFT", "``57447 u``"
  484. "RIGHT_CONTROL", "``57448 u``", "RIGHT_ALT", "``57449 u``"
  485. "RIGHT_SUPER", "``57450 u``", "RIGHT_HYPER", "``57451 u``"
  486. "RIGHT_META", "``57452 u``", "ISO_LEVEL3_SHIFT", "``57453 u``"
  487. "ISO_LEVEL5_SHIFT", "``57454 u``"
  488. .. end functional key table
  489. .. }}}
  490. .. note::
  491. The escape codes above of the form ``CSI 1 letter`` will omit the
  492. ``1`` if there are no modifiers, since ``1`` is the default value.
  493. .. note::
  494. The original version of this specification allowed F3 to be encoded as both
  495. CSI R and CSI ~. However, CSI R conflicts with the Cursor Position Report,
  496. so it was removed.
  497. .. _ctrl_mapping:
  498. Legacy :kbd:`ctrl` mapping of ASCII keys
  499. ------------------------------------------
  500. When the :kbd:`ctrl` key and another key are pressed on the keyboard, terminals
  501. map the result *for some keys* to a *C0 control code* i.e. an value from ``0 -
  502. 31``. This mapping was historically dependent on the layout of hardware
  503. terminal keyboards and is not specified anywhere, completely. The best known
  504. reference is `Table 3-5 in the VT-100 docs <https://vt100.net/docs/vt100-ug/chapter3.html>`_.
  505. The table below provides a mapping that is a commonly used superset of the table above.
  506. Any ASCII keys not in the table must be left untouched by :kbd:`ctrl`.
  507. .. {{{
  508. .. start ctrl mapping (auto generated by gen-key-constants.py do not edit)
  509. .. csv-table:: Emitted bytes when :kbd:`ctrl` is held down and a key is pressed
  510. :header: "Key", "Byte", "Key", "Byte", "Key", "Byte"
  511. "SPC ", "0", "/", "31", "0", "48"
  512. "1", "49", "2", "0", "3", "27"
  513. "4", "28", "5", "29", "6", "30"
  514. "7", "31", "8", "127", "9", "57"
  515. "?", "127", "@", "0", "[", "27"
  516. "\\", "28", "]", "29", "^", "30"
  517. "_", "31", "a", "1", "b", "2"
  518. "c", "3", "d", "4", "e", "5"
  519. "f", "6", "g", "7", "h", "8"
  520. "i", "9", "j", "10", "k", "11"
  521. "l", "12", "m", "13", "n", "14"
  522. "o", "15", "p", "16", "q", "17"
  523. "r", "18", "s", "19", "t", "20"
  524. "u", "21", "v", "22", "w", "23"
  525. "x", "24", "y", "25", "z", "26"
  526. "~", "30"
  527. .. end ctrl mapping
  528. .. }}}
  529. .. _fixterms_bugs:
  530. Bugs in fixterms
  531. -------------------
  532. The following is a list of errata in the `original fixterms proposal
  533. <http://www.leonerd.org.uk/hacks/fixterms/>`_, corrected in this
  534. specification.
  535. * No way to disambiguate :kbd:`Esc` key presses, other than using 8-bit controls
  536. which are undesirable for other reasons
  537. * Incorrectly claims special keys are sometimes encoded using ``CSI letter`` encodings when it
  538. is actually ``SS3 letter`` in all terminals newer than a VT-52, which is
  539. pretty much everything.
  540. * :kbd:`ctrl+shift+tab` should be ``CSI 9 ; 6 u`` not ``CSI 1 ; 5 Z``
  541. (shift+tab is not a separate key from tab)
  542. * No support for the :kbd:`super` modifier.
  543. * Makes no mention of cursor key mode and how it changes encodings
  544. * Incorrectly encoding shifted keys when shift modifier is used, for instance,
  545. for :kbd:`ctrl+shift+i` is encoded as :kbd:`ctrl+I`.
  546. * No way to have non-conflicting escape codes for :kbd:`alt+letter`,
  547. :kbd:`ctrl+letter`, :kbd:`ctrl+alt+letter` key presses
  548. * No way to specify both shifted and unshifted keys for robust shortcut
  549. matching (think matching :kbd:`ctrl+shift+equal` and :kbd:`ctrl+plus`)
  550. * No way to specify alternate layout key. This is useful for keyboard layouts
  551. such as Cyrillic where you want the shortcut :kbd:`ctrl+c` to work when
  552. pressing the :kbd:`ctrl+С` on the keyboard.
  553. * No way to report repeat and release key events, only key press events
  554. * No way to report key events for presses that generate text, useful for
  555. gaming. Think of using the :kbd:`WASD` keys to control movement.
  556. * Only a small subset of all possible functional keys are assigned numbers.
  557. * Claims the ``CSI u`` escape code has no fixed meaning, but has been used for
  558. decades as ``SCORC`` for instance by xterm and ansi.sys and `DECSMBV
  559. <https://vt100.net/docs/vt510-rm/DECSMBV.html>`_ by the VT-510 hardware
  560. terminal. This doesn't really matter since these uses are for communication
  561. to the terminal not from the terminal.
  562. * Handwaves that :kbd:`ctrl` *tends to* mask with ``0x1f``. In actual fact it
  563. does this only for some keys. The action of :kbd:`ctrl` is not specified and
  564. varies between terminals, historically because of different keyboard layouts.
  565. Why xterm's modifyOtherKeys should not be used
  566. ---------------------------------------------------
  567. * Does not support release events
  568. * Does not fix the issue of :kbd:`Esc` key presses not being distinguishable from
  569. escape codes.
  570. * Does not fix the issue of some keypresses generating identical bytes and thus
  571. being indistinguishable
  572. * There is no robust way to query it or manage its state from a program running
  573. in the terminal.
  574. * No support for shifted keys.
  575. * No support for alternate keyboard layouts.
  576. * No support for modifiers beyond the basic four.
  577. * No support for lock keys like Num lock and Caps lock.
  578. * Is completely unspecified. The most discussion of it available anywhere is
  579. `here <https://invisible-island.net/xterm/modified-keys.html>`__
  580. And it contains no specification of what numbers to assign to what function
  581. keys beyond running a Perl script on an X11 system!!