Reading — step 1 of 5
Read
Key Input & Escape Sequences
In raw mode, every keystroke shows up as 1+ bytes on stdin. Simple printable keys are one byte ('a' = 0x61). But arrow keys, function keys, Home/End/Page-Up — these arrive as escape sequences: an ESC (0x1b) followed by [ and a payload.
A condensed map:
| Bytes | Key |
|---|---|
0x61 | a |
0x1b | Esc (alone — must wait briefly to see if more bytes follow) |
0x1b 0x5b 0x41 | Arrow Up |
0x1b 0x5b 0x42 | Arrow Down |
0x1b 0x5b 0x43 | Arrow Right |
0x1b 0x5b 0x44 | Arrow Left |
0x1b 0x5b 0x35 0x7e | Page Up |
0x1b 0x5b 0x36 0x7e | Page Down |
0x1b 0x5b 0x33 0x7e | Delete |
0x1b 0x5b 0x48 | Home |
0x1b 0x5b 0x46 | End |
0x0d | Enter (Carriage Return, since we killed ICRNL) |
0x7f | Backspace (on most modern terms — sometimes 0x08) |
0x09 | Tab |
0x01..0x1a | Ctrl-A .. Ctrl-Z (Ctrl+letter is letter - 'a' + 1) |
0x1b 0x78 | Alt-X (ESC followed by a printable) |
The ambiguity: a real ESC press also produces 0x1b. Editors handle this with a short read timeout (10 ms in kilo) — if no more bytes follow ESC, treat it as a standalone Esc. Otherwise read the escape sequence.
Different terminals send slightly different sequences. xterm, screen, tmux, kitty, and Windows Terminal all agree on the basics but differ on shifted/modified keys (Ctrl+Arrow, Shift+F5). For a tutorial editor, the table above is enough. Production editors use the terminfo database or libraries like crossterm to handle the variation.
This lesson asks you to decode the byte sequences into key names. You will recognize the same logic when you read the source of editorReadKey() in kilo, KeyCode::from_bytes in Hecto, or crossterm's event parser.
Reference: xterm Control Sequences, kilo editorReadKey, Hecto chapter 3.
Discussion
Ask a question, share an insight, or help someone who’s stuck.
Sign in to post a comment or reply.
Loading…