keyboard-protocol.rst 33 KB

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