diff --git a/pkgs/id/web/src/world-keyboard.test.ts b/pkgs/id/web/src/world-keyboard.test.ts index 6a4434bf69..0a98e1eaa0 100644 --- a/pkgs/id/web/src/world-keyboard.test.ts +++ b/pkgs/id/web/src/world-keyboard.test.ts @@ -60,8 +60,72 @@ describe("terminal-compatible world key input", () => { it("leaves browser shortcuts and unsupported keys alone", () => { expect(key("s", { metaKey: true })).toBeNull(); - expect(key("ArrowLeft", { ctrlKey: true })).toBeNull(); expect(key("Dead")).toBeNull(); expect(key("F5")).toBeNull(); }); + + it("encodes modified CSI tilde keys with the xterm modifier parameter", () => { + expect(key("Delete", { ctrlKey: true })).toBe("\x1b[3;5~"); + expect(key("Delete", { shiftKey: true })).toBe("\x1b[3;2~"); + expect(key("PageUp", { altKey: true })).toBe("\x1b[5;3~"); + expect(key("PageDown", { ctrlKey: true, shiftKey: true })).toBe("\x1b[6;6~"); + }); + + it("encodes modified CSI letter keys with the xterm modifier parameter", () => { + expect(key("ArrowUp", { altKey: true })).toBe("\x1b[1;3A"); + expect(key("ArrowLeft", { ctrlKey: true })).toBe("\x1b[1;5D"); + expect(key("ArrowDown", { shiftKey: true })).toBe("\x1b[1;2B"); + expect(key("ArrowRight", { ctrlKey: true, altKey: true })).toBe("\x1b[1;7C"); + expect(key("Home", { shiftKey: true })).toBe("\x1b[1;2H"); + expect(key("End", { shiftKey: true })).toBe("\x1b[1;2F"); + expect(key("Home", { ctrlKey: true, altKey: true, shiftKey: true })).toBe("\x1b[1;8H"); + }); + + it("encodes Shift+Tab as backtab and keeps other modifiers on it", () => { + expect(key("Tab", { shiftKey: true })).toBe("\x1b[Z"); + expect(key("Tab", { shiftKey: true, ctrlKey: true })).toBe("\x1b[1;6Z"); + }); + + it("ESC-prefixes modified special keys that have no CSI form", () => { + expect(key("Backspace", { altKey: true })).toBe("\x1b\x7f"); + expect(key("Enter", { altKey: true })).toBe("\x1b\r"); + expect(key("Enter", { shiftKey: true })).toBe("\x1b\r"); + expect(key("Tab", { altKey: true })).toBe("\x1b\t"); + expect(key("Escape", { ctrlKey: true })).toBe("\x1b\x1b"); + }); + + it("never returns the plain bytes for a modified special key", () => { + const specials = [ + "Enter", + "Tab", + "Backspace", + "Escape", + "Delete", + "Home", + "End", + "PageUp", + "PageDown", + "ArrowUp", + "ArrowDown", + "ArrowRight", + "ArrowLeft", + ]; + const modifierSets = [ + { shiftKey: true }, + { altKey: true }, + { ctrlKey: true }, + { ctrlKey: true, shiftKey: true }, + { altKey: true, shiftKey: true }, + { ctrlKey: true, altKey: true }, + { ctrlKey: true, altKey: true, shiftKey: true }, + ]; + for (const special of specials) { + const plain = key(special); + for (const modifiers of modifierSets) { + const encoded = key(special, modifiers); + expect(encoded, `${special} with ${JSON.stringify(modifiers)}`).not.toBeNull(); + expect(encoded, `${special} with ${JSON.stringify(modifiers)}`).not.toBe(plain); + } + } + }); }); diff --git a/pkgs/id/web/src/world-keyboard.ts b/pkgs/id/web/src/world-keyboard.ts index 190dcb93be..ba8095e924 100644 --- a/pkgs/id/web/src/world-keyboard.ts +++ b/pkgs/id/web/src/world-keyboard.ts @@ -2,26 +2,65 @@ type KeyEventLike = Pick; -const specialKeys: Record = { - Enter: "\r", - Tab: "\t", - Backspace: "\x7f", - Escape: "\x1b", - Delete: "\x1b[3~", - Home: "\x1b[H", - End: "\x1b[F", - PageUp: "\x1b[5~", - PageDown: "\x1b[6~", - ArrowUp: "\x1b[A", - ArrowDown: "\x1b[B", - ArrowRight: "\x1b[C", - ArrowLeft: "\x1b[D", +/** + * A special (non-printable) key, described by the forms it can take. + * + * - `plain` is the byte sequence an unmodified press sends. + * - `csiLetter` keys have a CSI form ending in a letter (arrows, Home, End, + * and Shift+Tab's `Z`): with a modifier they send `CSI 1 ; `. + * - `csiTilde` keys have a CSI form ending in `~` (Delete, PageUp, PageDown): + * with a modifier they send `CSI ; ~`. + * - Keys with neither form (Enter, Tab without Shift, Backspace, Escape) have + * no CSI encoding for a modifier; see the policy in `terminalBytesForKey`. + */ +interface SpecialKey { + plain: string; + csiLetter?: string; + csiTilde?: number; +} + +const specialKeys: Record = { + Enter: { plain: "\r" }, + Tab: { plain: "\t", csiLetter: "Z" }, + Backspace: { plain: "\x7f" }, + Escape: { plain: "\x1b" }, + Delete: { plain: "\x1b[3~", csiTilde: 3 }, + Home: { plain: "\x1b[H", csiLetter: "H" }, + End: { plain: "\x1b[F", csiLetter: "F" }, + PageUp: { plain: "\x1b[5~", csiTilde: 5 }, + PageDown: { plain: "\x1b[6~", csiTilde: 6 }, + ArrowUp: { plain: "\x1b[A", csiLetter: "A" }, + ArrowDown: { plain: "\x1b[B", csiLetter: "B" }, + ArrowRight: { plain: "\x1b[C", csiLetter: "C" }, + ArrowLeft: { plain: "\x1b[D", csiLetter: "D" }, }; +/** xterm modifier parameter: 1 + (Shift ? 1) + (Alt ? 2) + (Ctrl ? 4). */ +function modifierParameter(event: KeyEventLike): number { + return 1 + (event.shiftKey ? 1 : 0) + (event.altKey ? 2 : 0) + (event.ctrlKey ? 4 : 0); +} + /** * Encode a browser key as the bytes a terminal sends for that key. * Returns null for browser/OS shortcuts and keys the Roc `Key` module does not * recognize yet. Printable input is UTF-8; control-letter input is C0. + * + * Modified special keys follow one policy (issue #732, Option 1 — encode the + * modifier; no modified special key ever returns the plain key's bytes): + * + * - A key with a CSI form carries the standard xterm modifier parameter: + * Delete is `CSI 3 ; ~` (Ctrl+Delete = `ESC [ 3 ; 5 ~`), arrows, + * Home, and End are `CSI 1 ; ` (Alt+Up = `ESC [ 1 ; 3 A`). + * Shift+Tab is the traditional backtab `ESC [ Z`; Shift+Tab with further + * modifiers is `CSI 1 ; Z`, like the other CSI letter keys. + * - A key with no CSI form (Backspace, Enter, Escape, Tab without Shift) is + * ESC-prefixed when modified, the same convention the printable path below + * already uses for Alt+printable (Alt+Backspace = `ESC DEL`). The prefix + * signals "modified" even though these keys cannot distinguish which + * modifier was held. + * + * Unmodified special keys, control letters, and AltGr (Ctrl+Alt printable) + * behaviour are unchanged. */ export function terminalBytesForKey(event: KeyEventLike): Uint8Array | null { if (event.metaKey || event.key === "Unidentified" || event.key === "Dead" || event.key === "Process") { @@ -29,9 +68,27 @@ export function terminalBytesForKey(event: KeyEventLike): Uint8Array | null { } if (event.key in specialKeys) { - if ((event.ctrlKey || event.altKey || event.shiftKey) && event.key.startsWith("Arrow")) return null; - if (event.shiftKey && event.key === "Tab") return null; - return new TextEncoder().encode(specialKeys[event.key]); + const special = specialKeys[event.key]; + const modified = event.ctrlKey || event.altKey || event.shiftKey; + if (!modified) return new TextEncoder().encode(special.plain); + const mod = modifierParameter(event); + // Tab's CSI form exists only with Shift (backtab); a Tab modified only by + // Ctrl and/or Alt has no CSI form and falls through to the ESC prefix. + const hasCsiForm = + special.csiTilde !== undefined || (special.csiLetter !== undefined && !(event.key === "Tab" && !event.shiftKey)); + if (hasCsiForm && special.csiTilde !== undefined) { + return new TextEncoder().encode(`\x1b[${special.csiTilde};${mod}~`); + } + if (hasCsiForm && special.csiLetter !== undefined) { + // Plain Shift+Tab keeps its traditional form, with no parameter. + if (event.key === "Tab" && mod === 2) return new TextEncoder().encode("\x1b[Z"); + return new TextEncoder().encode(`\x1b[1;${mod}${special.csiLetter}`); + } + const plain = new TextEncoder().encode(special.plain); + const withEscape = new Uint8Array(plain.length + 1); + withEscape[0] = 0x1b; + withEscape.set(plain, 1); + return withEscape; } if (event.ctrlKey && !event.altKey) {