keyboard-protocol.rst 34 KB

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