1 Keyboard Mouse and Paste
liquibyte edited this page 2026-11-19 16:23:30 -06:00

Keyboard, Mouse and Paste

A program on a terminal reads bytes. The input encoder (hosho_input.h) turns what the user does into the bytes an xterm-compatible terminal sends, by the modes the program set. This page says how a host uses it, and what each key, mouse event, focus change and paste sends, with each keyboard and protocol a program can choose.

1. How a host sends a key

The host turns its own key codes into Hosho's: a key that sends a sequence (the arrows, Home, the function keys, the keypad, Enter, Tab, Backspace, Escape) is a HoshoKey; a key that types is the character it types. The modifiers are HOSHO_MODIFIER_SHIFT, ALT and CONTROL (and SUPER, CAPS_LOCK and NUM_LOCK, which only the Kitty keyboard protocol reports). Then, for each key:

  1. When the program asked for the Kitty keyboard protocol, hosho_input_key_event(terminal, &event, bytes) gives the bytes for the whole event (section 6). It returns HOSHO_INPUT_LEGACY when the program did not ask for it, or when the protocol sends this key as always: then go on to step 2 for a press or a repeat, and send nothing for a release.
  2. hosho_input_key(terminal, key, modifiers, bytes) for a HoshoKey, or hosho_input_character(terminal, codepoint, modifiers, bytes) for a key that types. Give the character the key types without Control and Alt (for Control+C: c or C); Shift is already in it. A modifier key alone is neither, so it sends nothing.

Each function writes into a buffer of HOSHO_INPUT_SIZE bytes and returns how many to send, 0 when the terminal sends nothing. The host sends them with hosho_pseudoterminal_write(), and gives them to hosho_terminal_typed() too for shell integration. No function here keeps state or knows a toolkit.

In the tables, CSI is ESC [ and SS3 is ESC O. With 8-bit controls set (S8C1T), the CSI or SS3 at the start of what hosho_input_key(), hosho_input_key_event(), hosho_input_mouse() and hosho_input_focus() give, and of a modifyOtherKeys form from hosho_input_character(), becomes the one C1 control U+009B or U+008F. All a program reads is UTF-8, so the control is the two bytes C2 9B or C2 8F, as xterm sends it in UTF-8 mode; the terminal's own answers do the same. The marks of a bracketed paste always start with ESC [.

2. Keys that send a sequence

A key with a modifier sends the number m: 1, plus 1 for Shift, 2 for Alt and 4 for Control (so Control+Shift is 6). This is xterm's form, and the terminfo entry xterm-256color describes it.

Key No modifier Application cursor keys (DECCKM) With modifier m
Up, Down, Right, Left CSI A to CSI D SS3 A to SS3 D CSI 1 ; m A ...
Home, End CSI H, CSI F SS3 H, SS3 F CSI 1 ; m H, CSI 1 ; m F
Begin (keypad 5 with Num Lock off) CSI E SS3 E CSI 1 ; m E
Insert, Delete, Page Up, Page Down CSI 2 ~, CSI 3 ~, CSI 5 ~, CSI 6 ~ the same CSI 2 ; m ~ ...
F1 to F4 SS3 P to SS3 S the same CSI 1 ; m P to CSI 1 ; m S
F5 to F12 CSI 15 ~, 17, 18, 19, 20, 21, 23, 24 ~ the same CSI 15 ; m ~ ...
F13 to F20 What F1 to F8 send with Shift: CSI 1 ; 2 P to CSI 1 ; 2 S, then CSI 15 ; 2 ~, 17, 18, 19 ; 2 ~ (the terminfo entry's kf13 to kf20). With the VT220 keyboard: CSI 25 ~, 26, 28, 29, 31, 32, 33, 34 ~ the same The same with Shift added to m; with the VT220 keyboard CSI 25 ; m ~ ...
Key What it sends
Backspace DEL (0x7F); with Control, BS (0x08). A program that set DECBKM (mode 67) exchanges the two. With modifyOtherKeys 2 and a modifier other than Control alone, the modifyOtherKeys form.
Tab HT; Shift+Tab sends CSI Z (back tab), and with Alt or Control held too CSI 1 ; m Z. With modifyOtherKeys, Control+Tab (without Shift) says what it is; at level 2 every Tab with Control or Alt.
Enter CR, or CR LF in newline mode (LNM). With modifyOtherKeys, Control+Enter and Shift+Enter say what they are; at level 2 Enter with any modifier.
Escape ESC. With modifyOtherKeys 2 and a modifier, the modifyOtherKeys form.
Keypad Enter CR (never CR LF); in application keypad mode (DECKPAM) SS3 M, or SS3, the number m and M with a modifier.
Keypad digits, . + - * / = Their characters, with a PC's keyboard in every mode. With the VT220 keyboard in application keypad mode: SS3 p to SS3 y for 0 to 9, SS3 n for the point, SS3 l for plus (the VT220's comma key), SS3 m for minus, SS3 j, SS3 o and SS3 X for multiply, divide and equal; with a modifier SS3, the number m and the letter. Outside application mode a VT220's keypad plus is a comma. Control does not change these characters.

Alt with any of these keys puts ESC in front of what the key sends when the key sends one byte (Backspace, Tab, Enter, Escape, the keypad's characters).

3. The keyboards

By default the keyboard is a PC's, as in xterm: Home and End send CSI H and CSI F, and the keypad's digits are digits. Programs expect this. keyboard = vt220 in the theme (or hosho_terminal_set_default_keyboard()) makes it a VT220's: Home and End are Find and Select (CSI 1 ~ and CSI 4 ~, or CSI 1 ; m ~ with a modifier), and the keypad sends its own codes in application mode, as the table above shows. A program can switch this itself with mode 1061.

The keyboard setting can also name another keyboard or terminal (hosho_terminal_set_default_key_set()). Then the function keys and the editing keys send that keyboard's codes, when no modifier is held; a key the set has no code for, and every key with a modifier, sends xterm's code. A program can choose the first three itself (modes 1051 to 1053; resetting the mode gives xterm's codes back).

Set F1 to F12 Other keys
sun (mode 1051) CSI 224 z to CSI 233 z, then CSI 192 z, CSI 193 z Home CSI 214 z, End CSI 220 z, Begin CSI 218 z, Insert CSI 2 z, Delete CSI 3 z, Page Up CSI 216 z, Page Down CSI 222 z
hp (mode 1052) F1 to F8: ESC p to ESC w arrows ESC A to ESC D, Home ESC h, End ESC F, Insert ESC Q, Delete ESC P, Page Up ESC T, Page Down ESC S
sco (mode 1053) CSI M to CSI X arrows CSI A to CSI D, Home CSI H, End CSI F, Begin CSI E, Insert CSI L, Delete DEL, Page Up CSI I, Page Down CSI G
rxvt F1 to F5: CSI 11 ~ to CSI 15 ~ Home CSI 7 ~, End CSI 8 ~
linux F1 to F5: CSI [ A to CSI [ E Home CSI 1 ~, End CSI 4 ~

In VT52 mode (a program reset mode 2), the arrows send ESC A to ESC D, F1 to F4 send ESC P to ESC S, and in application keypad mode the keypad sends ESC ? p to ESC ? y for the digits, ESC ? n, ESC ? m, ESC ? l for the point, minus and plus, and ESC ? M for Enter. The other keys send what they send outside VT52 mode.

A program can define what Shift and a function key sends (DECUDK). Shift and F1 to F20 then sends that text, as long as the program defined something for the key.

4. Keys that type

hosho_input_character() sends the character as UTF-8. With Control it sends the control character the character has: @ to ~ give 0x00 to 0x1F (Control+A and Control+a are both 0x01, Control+[ is ESC); Control+Space and Control+2 give NUL, Control+3 to Control+7 give ESC, FS, GS, RS, US, Control+/ gives US, and Control+8 and Control+? give DEL. Control with any other character (Control+1, or a letter outside ASCII) sends the character itself. Alt puts ESC in front.

That Alt puts ESC in front is xterm's mode 1036, which is on after a reset. A program can reset mode 1036 and set mode 1034, so that Alt sets the character's eighth bit. The encoder follows these modes, and so does the widget for the text a key types: while mode 1036 is set, Alt puts ESC in front; when it is reset and mode 1034 is set, an ASCII character (or the control character Control makes) with Alt is sent with 0x80 added, as UTF-8; when both are reset, Alt changes nothing. As in xterm, mode 1036 comes first: a program that sets mode 1034 alone (bash does, through the terminfo entry's smm) leaves Alt as ESC.

5. modifyOtherKeys

In the old forms some keys cannot be told apart: Control+I is Tab, Control+M is Enter, Control+Shift+A is Control+A. A program that sets modifyOtherKeys (CSI > 4 ; level m) gets such keys in a form that says what they are: CSI 27 ; m ; code ~, where code is the character, or CSI code ; m u after the program asked for that form (CSI > 4 ; 1 f).

Level Keys in the new form
1 Control with a character that has no control character of its own (and Control+?); Control+Tab; Control+Enter and Shift+Enter.
2 Every character with Control or Alt, and Shift with @ to _ and { to ~; Tab with Control or Alt; Enter, Escape and Backspace with a modifier (Control+Backspace alone stays BS).

CSI > 4 m with no level, CSI > m and CSI > 4 n set it back to 0; CSI ? 4 m asks for the level. While a program has modifyOtherKeys on, the widget gives it Control+Plus, Control+Minus and Control+0 too.

6. The Kitty keyboard protocol

A program can ask for the Kitty keyboard protocol, which reports each key without the old forms' doubts, and keys that repeat or go up. It sets flags:

Flag Bit What the program gets
HOSHO_KITTY_DISAMBIGUATE 1 Escape, and keys with Alt or Control, say what they are.
HOSHO_KITTY_EVENT_TYPES 2 A key that repeats and a key that goes up are reported.
HOSHO_KITTY_ALTERNATES 4 For a key that types: the key with Shift (when Shift is held) and the key of the US layout are reported too, when they are not the key itself.
HOSHO_KITTY_ALL_KEYS 8 Each key is reported as an escape code, the keys that type text too.
HOSHO_KITTY_TEXT 16 With all keys: the text a key types is in its report, for a press or a repeat with no Alt, Control or Super held. Control characters are left out of it.

The encoder does what flag 1 asks for whenever any flag is set: the other flags only add to it.

The program sets them with CSI = flags ; how u (how 1 sets them, 2 adds these, 3 removes these), saves the flags it had and sets new ones with CSI > flags u, takes saved ones back with CSI < count u, and asks for them with CSI ? u. The primary and the alternate screen each have their own flags and their own stack of 8; when the stack is full, a save drops the oldest flags in it, and with nothing saved, a take back sets all flags off. A reset sets them off.

A report is CSI number[:shifted[:base]] ; modifiers[:event] ; text u, or for a few keys the old last character (A to D, H, F, E, P, Q, S, or ~) in place of u. modifiers is 1 plus Shift 1, Alt 2, Control 4, Super 8, Caps Lock 64 and Num Lock 128; Caps Lock and Num Lock count only for a key that types no text, or with HOSHO_KITTY_ALL_KEYS. event is 2 for a repeat and 3 for a release. Parts with nothing to say are left out. number is the character of the key with no modifier, or the number of a functional key:

Key Number Key Number
Escape, Enter, Tab, Backspace 27 u, 13 u, 9 u, 127 u Insert, Delete, Page Up, Page Down 2 ~, 3 ~, 5 ~, 6 ~
Arrows, Home, End, Begin 1 A to 1 D, 1 H, 1 F, 1 E F1, F2, F3, F4 1 P, 1 Q, 13 ~, 1 S
F5 to F12 15 ~ to 24 ~, as in xterm Keypad 0 to 9 57399 u to 57408 u
F13 to F20 57376 u to 57383 u
Keypad point, divide, multiply, minus, plus, Enter, equal 57409 u to 57415 u Modifier keys (left Shift ...) 57441 u and up

A key with the number 1 and a letter at its end leaves out the 1 when nothing follows it. Without HOSHO_KITTY_ALL_KEYS, a key that types text with no Alt, Control or Super held (the keypad's digits and signs count as such keys, but not its Enter), and Enter, Tab and Backspace with no modifier, are sent in the old way (so a user can still type at a shell after a program that did not take the protocol back), and a modifier key alone is not reported. Also without it, the arrows, Home, End and Begin with no modifier follow application cursor keys (DECCKM) as in the old way: SS3 A in place of CSI A. Without HOSHO_KITTY_EVENT_TYPES, a release sends nothing.

The host fills a HoshoKeyEvent for each event: the HoshoKey, or for a key that types the codepoint of the key with no modifier, shifted and base (the key with Shift, and in the US layout), the modifiers, the action (press, repeat, release) and the text the key types. For a modifier key, codepoint is its number in the protocol (57441 for the left Shift). The widget sends releases only to a program that asked for this protocol.

7. The mouse

When a program asks for mouse reports, the mouse belongs to the program and not to the host's selection: hosho_input_mouse_wanted() says so. The host then gives each event to hosho_input_mouse(terminal, action, button, modifiers, row, column, bytes): a press (a wheel step is a press), a release, or a motion to another cell, the button (HOSHO_BUTTON_LEFT, MIDDLE, RIGHT, the wheel's four directions, 8 to 11 for more buttons; for a motion the button held, or HOSHO_BUTTON_NONE), the modifiers and the cell. It gives 0 bytes for an event the program did not ask for.

Mode What is reported
9 (X10) A press of the three buttons, with no modifiers.
1000 Presses and releases.
1002 Also motion with a button down.
1003 Also every motion.
Form What it sends
The first form CSI M and three bytes: the button code, the column and the row, each with 32 added. A place beyond column or row 223 is not sent.
UTF-8 (mode 1005) The same, each value as UTF-8, up to 2015.
SGR (mode 1006) CSI < code ; column ; row M, with m at the end for a release, which keeps the button's number.
urxvt (mode 1015) CSI code+32 ; column ; row M.
SGR in pixels (mode 1016) The SGR form with the pointer's place in pixels (from 1), which the host told with hosho_terminal_pointer(). It is a form of its own and needs no mode 1006; it works only after the host told the terminal where the pointer is, and until then the places are cells.

When the program set more than one form, SGR comes first, then urxvt, then UTF-8.

The button code is 0, 1 or 2 for the left, middle and right buttons, 3 for a release in the forms that do not say which button, 64 to 67 for the wheel, and 128 to 131 for buttons 8 to 11; Shift adds 4, Alt 8, Control 16, and a motion 32. Columns and rows count from 1.

With alternate scroll mode (1007), a host sends the wheel on the alternate screen as Up and Down keys; the widget sends three for each step. In the widget, a mouse event with Shift held stays with the selection and is not reported. The DEC locator is another way for a program to read the pointer: see Control Functions. The host tells the terminal about the pointer with hosho_terminal_pointer(), which returns 1 while the locator takes the events.

8. Focus and paste

hosho_input_focus(terminal, focused, bytes) gives CSI I when the terminal got the keyboard and CSI O when it lost it, when the program asked for focus reports (mode 1004), and 0 bytes when it did not.

hosho_input_paste(terminal, text, length, &result_length) gives the bytes of pasted text, which the caller frees. Each line end (LF, or CR LF) becomes a carriage return, as the Enter key sends. Control characters other than tab and carriage return are removed, DEL too, and C1 controls written as UTF-8, so pasted text cannot end the bracketed paste or send a control sequence. With bracketed paste mode (2004) the text goes between CSI 200 ~ and CSI 201 ~, and the program knows it was pasted.