@devries-cameron20 / t3code-1 / commits / 7379f1969b
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

fix(web): keypad navigation works with NumLock off (#37175)

Co-authored-by: Julius Marminge <julius0216@outlook.com>

Wout Stiens committed Nov 15, 2026 at 13:04 UTC 7379f1969b0f4b579327cf81299ef8cc3ddf8e9c
3 files changed +143 -18
apps/web/src/terminal/ghostty/core.test.ts
+28 -16
@@ -130,28 +130,40 @@ describe("GhosttyTerminalCore snapshots", () => {
130 });
131
132 it.each([
133 + { code: "Numpad1", key: "End", expected: "\x1b[F" },
134 + { code: "Numpad5", key: "Clear", expected: "\x1b[E" },
135 + { code: "Numpad8", key: "ArrowUp", expected: "\x1b[A" },
136 + { code: "NumpadDecimal", key: "Delete", expected: "\x1b[3~" },
137 + { mode: "\x1b[?1h", code: "Numpad8", key: "ArrowUp", expected: "\x1bOA" },
138 + { mode: "\x1b[>1u", code: "Numpad8", key: "ArrowUp", expected: "\x1b[57419u" },
139 + { mode: "\x1b=\x1b[?1035l", code: "Numpad8", key: "8", numLock: true, expected: "\x1bOx" },
140 + { mode: "\x1b[>1u", code: "Numpad8", key: "8", numLock: true, expected: "8" },
141 { code: "", key: "Backspace", expected: "\x7f" },
142 { code: "Unidentified", key: "Backspace", expected: "\x7f" },
143 { code: "Backspace", key: "Backspace", expected: "\x7f" },
144 { code: "Delete", key: "Backspace", expected: "\x1b[3~" },
145 { code: "", key: "a", expected: "a" },
146 { code: "", key: "Unidentified", expected: "" },
139 - ])("encodes key $key with physical code '$code'", async ({ code, key, expected }) => {
140 - const core = await createCore();
141 - const event = {
142 - code,
143 - key,
144 - repeat: false,
145 - shiftKey: false,
146 - ctrlKey: false,
147 - altKey: false,
148 - metaKey: false,
149 - isComposing: false,
150 - getModifierState: () => false,
151 - } as unknown as KeyboardEvent;
152 -
153 - expect(core.encodeKey(event)).toBe(expected);
154 - });
147 + ])(
148 + "encodes key $key with physical code '$code'",
149 + async ({ mode = "", code, key, numLock = false, expected }) => {
150 + const core = await createCore();
151 + core.write(mode);
152 + const event = {
153 + code,
154 + key,
155 + repeat: false,
156 + shiftKey: false,
157 + ctrlKey: false,
158 + altKey: false,
159 + metaKey: false,
160 + isComposing: false,
161 + getModifierState: (modifier: string) => modifier === "NumLock" && numLock,
162 + } as unknown as KeyboardEvent;
163 +
164 + expect(core.encodeKey(event)).toBe(expected);
165 + },
166 + );
167
168 it.each(["", "\x1b[>1u"])("encodes macOS Option text with keyboard mode %j", async (mode) => {
169 using _platform = vi.spyOn(navigator, "platform", "get").mockReturnValue("MacIntel");
apps/web/src/terminal/ghostty/keyCodes.ts
+25 -2
@@ -190,9 +190,32 @@ export function ghosttyKeyForCode(code: string): number {
190 return codeToGhosttyKey.get(code) ?? 0;
191 }
192
193 +// With NumLock off, keypad digits report navigation keys; Ghostty encodes those as keypad variants.
194 +const NUMPAD_NAVIGATION_KEYS = new Map<string, number>(
195 + (
196 + [
197 + ["ArrowDown", "NumpadArrowDown"],
198 + ["ArrowLeft", "NumpadArrowLeft"],
199 + ["ArrowRight", "NumpadArrowRight"],
200 + ["ArrowUp", "NumpadArrowUp"],
201 + ["Clear", "NumpadBegin"],
202 + ["Delete", "NumpadDelete"],
203 + ["End", "NumpadEnd"],
204 + ["Home", "NumpadHome"],
205 + ["Insert", "NumpadInsert"],
206 + ["PageDown", "NumpadPageDown"],
207 + ["PageUp", "NumpadPageUp"],
208 + ] as const
209 + ).map(([key, code]) => [key, ghosttyKeyForCode(code)]),
210 +);
211 +
212 export function ghosttyKeyForEvent(event: Pick<KeyboardEvent, "code" | "key">): number {
194 - // Android browsers can omit the physical code for keys such as Backspace.
195 - return ghosttyKeyForCode(event.code) || ghosttyKeyForCode(event.key);
213 + return (
214 + (event.code.startsWith("Numpad") && NUMPAD_NAVIGATION_KEYS.get(event.key)) ||
215 + ghosttyKeyForCode(event.code) ||
216 + // Android browsers can omit the physical code for keys such as Backspace.
217 + ghosttyKeyForCode(event.key)
218 + );
219 }
220
221 export interface GhosttyKeyboardLayoutMap {
scripts/scenarios/terminal-numlock-off-keypad-navigates.e2e.ts new
+90
@@ -0,0 +1,90 @@
1 +/// <reference lib="dom" />
2 +// The DOM lib types the function this scenario runs in the page.
3 +import { expect } from "e2e";
4 +import * as Effect from "effect/Effect";
5 +import * as FileSystem from "effect/FileSystem";
6 +
7 +import { runPromise, test, WEB } from "./t3.ts";
8 +
9 +// A raw-mode program turns on application cursor and keypad modes as ncurses keypad(TRUE) does,
10 +// asks for DECCKM so its reply marks when the terminal has applied them, and logs what it reads.
11 +const PROBE = String.raw`import { appendFileSync, writeFileSync } from "node:fs";
12 +
13 +const log = new URL("received", import.meta.url);
14 +writeFileSync(log, "");
15 +process.stdin.setRawMode(true);
16 +process.stdin.on("data", (chunk) => {
17 + appendFileSync(log, chunk);
18 + if (chunk.includes("x")) {
19 + process.stdout.write("\x1b[?1l\x1b>");
20 + process.stdin.setRawMode(false);
21 + process.exit(0);
22 + }
23 +});
24 +process.stdout.write("\x1b[?1h\x1b=\x1b[?1$p");
25 +`;
26 +
27 +const READY = "\x1b[?1;1$y";
28 +// Keypad keys as a browser reports them with NumLock off: the physical digit, the navigation key.
29 +const KEYPAD = [
30 + { code: "Numpad8", key: "ArrowUp" },
31 + { code: "Numpad1", key: "End" },
32 + { code: "NumpadDecimal", key: "Delete" },
33 +];
34 +
35 +test(
36 + "NumLock-off keypad navigation keys reach a terminal program",
37 + WEB,
38 + async ({ t3, app, browser, screen }) => {
39 + const project = await t3.project("demo", { "keypad-probe.mjs": PROBE });
40 + const thread = await t3.thread({ project, title: "Keypad", messages: ["hello"] });
41 + const read = (file: string) =>
42 + runPromise(
43 + Effect.gen(function* () {
44 + const fs = yield* FileSystem.FileSystem;
45 + return yield* fs.readFileString(`${project}/${file}`);
46 + }).pipe(Effect.orElseSucceed(() => "")),
47 + );
48 + await t3.open(thread);
49 +
50 + await screen.getByRole("button", "Toggle terminal drawer").tap();
51 + const terminal = screen.getByLabel("Terminal input");
52 +
53 + // Input sent before the shell runs is dropped, so retype until it lands.
54 + await expect
55 + .poll(
56 + async () => {
57 + await terminal.focus();
58 + await browser.keyboard.type(`echo ready > '${project}/shell-ready'`);
59 + await browser.keyboard.press("Enter");
60 + return read("shell-ready");
61 + },
62 + { timeout: 30_000 },
63 + )
64 + .toBe("ready\n");
65 +
66 + await terminal.focus();
67 + await browser.keyboard.type(`'${process.execPath}' '${project}/keypad-probe.mjs'`);
68 + await browser.keyboard.press("Enter");
69 + await expect.poll(() => read("received"), { timeout: 15_000 }).toBe(READY);
70 +
71 + // The e2e keyboard cannot press a keypad key with NumLock off, so send the events Chromium reports.
72 + await browser.evaluate((keys) => {
73 + const input = document.activeElement;
74 + if (!input) throw new Error("Terminal input lost focus");
75 + for (const init of keys) {
76 + for (const type of ["keydown", "keyup"]) {
77 + const options = { ...init, location: 3, bubbles: true, cancelable: true };
78 + input.dispatchEvent(new KeyboardEvent(type, options));
79 + }
80 + }
81 + return null;
82 + }, KEYPAD);
83 + await browser.keyboard.type("x");
84 + await app.screenshot("after-keypad");
85 +
86 + await expect
87 + .poll(() => read("received"), { timeout: 10_000 })
88 + .toBe(`${READY}\x1bOA\x1bOF\x1b[3~x`);
89 + },
90 +);