keyboard-protocol.rst 33 KB

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