What happens when you press a key in your terminal?
Summary (AI generated)
Archived original version »This article explains terminal operation by detailing the interaction between client and server processes in terminal emulators. Terminals convert user input (key presses) into byte sequences sent to server processes, which return output for display. Key mappings include control characters (e.g., Ctrl+C sends 0x03) and special keys like arrow/Home, mapped to escape sequences (e.g., Home → \x1b[H). Input modes are discussed: cooked mode processes input line-by-line with editing, while raw mode sends bytes immediately.
Terminals use pseudo-terminals (pty), where the client connects to the “master” side and server processes to the “slave.” Output from servers is rendered by clients according to terminal capabilities. Escape codes—originating from 1970s-era hardware like the VT100—control cursor movement, colors, etc., but can disrupt terminals if mishandled (e.g., via cat on binary data). The Alt key’s equivalence to pressing Escape first is noted, along with timing delays required for input interpretation.
Resizing terminals involves ioctl system calls (TIOCSWINSZ) to adjust window size. Local terminal emulators like xterm operate identically to remote ones, using the same byte-based communication and escape codes. The article highlights historical design choices (e.g., control keys sharing mappings, Escape’s dual role) that persist for backward compatibility despite modern oddities. It underscores terminals’ enduring complexity rooted in decades-old standards while acknowledging their foundational role in computing interactions.