The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 23, 04:15 UTC
fix(web): handle Backspace when Android omits the key code (#18063)
JIA committed
Oct 19, 2026 at 03:28 UTC
a440198ebd11e0be17cb05f468a47bbb31d93900
4 files changed
+60
-3
apps/web/src/terminal/ghostty/core.test.ts
+24
@@ -108,6 +108,30 @@ describe("GhosttyTerminalCore snapshots", () => {
108
vi.restoreAllMocks();
109
});
110
111
+ it.each([
112
+ { code: "", key: "Backspace", expected: "\x7f" },
113
+ { code: "Unidentified", key: "Backspace", expected: "\x7f" },
114
+ { code: "Backspace", key: "Backspace", expected: "\x7f" },
115
+ { code: "Delete", key: "Backspace", expected: "\x1b[3~" },
116
+ { code: "", key: "a", expected: "a" },
117
+ { code: "", key: "Unidentified", expected: "" },
118
+ ])("encodes key $key with physical code '$code'", async ({ code, key, expected }) => {
119
+ const core = await createCore();
120
+ const event = {
121
+ code,
122
+ key,
123
+ repeat: false,
124
+ shiftKey: false,
125
+ ctrlKey: false,
126
+ altKey: false,
127
+ metaKey: false,
128
+ isComposing: false,
129
+ getModifierState: () => false,
130
+ } as unknown as KeyboardEvent;
131
+
132
+ expect(core.encodeKey(event)).toBe(expected);
133
+ });
134
+
135
it("preserves styles, wide cells, and selection after shared memory grows", async () => {
136
const core = await createCore();
137
const runtime = await loadGhosttyRuntime();
apps/web/src/terminal/ghostty/core.ts
+2
-2
@@ -1,7 +1,7 @@
1
import {
2
type GhosttyKeyboardLayoutMap,
3
ghosttyConsumedMods,
4
- ghosttyKeyForCode,
4
+ ghosttyKeyForEvent,
5
ghosttyUnshiftedCodepoint,
6
loadGhosttyKeyboardLayoutMap,
7
} from "./keyCodes";
@@ -516,7 +516,7 @@ export class GhosttyTerminalCore {
516
this.keyEvent,
517
action === "release" ? 0 : event.repeat ? 2 : 1,
518
);
519
- this.runtime.call("ghostty_key_event_set_key", this.keyEvent, ghosttyKeyForCode(event.code));
519
+ this.runtime.call("ghostty_key_event_set_key", this.keyEvent, ghosttyKeyForEvent(event));
520
const mods =
521
(event.shiftKey ? 1 : 0) |
522
(event.ctrlKey ? 1 << 1 : 0) |
apps/web/src/terminal/ghostty/keyCodes.test.ts
+29
-1
@@ -1,6 +1,11 @@
1
import { describe, expect, it } from "vite-plus/test";
2
3
-import { ghosttyConsumedMods, ghosttyKeyForCode, ghosttyUnshiftedCodepoint } from "./keyCodes";
3
+import {
4
+ ghosttyConsumedMods,
5
+ ghosttyKeyForCode,
6
+ ghosttyKeyForEvent,
7
+ ghosttyUnshiftedCodepoint,
8
+} from "./keyCodes";
9
10
describe("ghosttyKeyForCode", () => {
11
it("keeps the tail of the pinned Ghostty key enum in order", () => {
@@ -11,6 +16,29 @@ describe("ghosttyKeyForCode", () => {
16
});
17
});
18
19
+describe("ghosttyKeyForEvent", () => {
20
+ it.each(["", "Unidentified", "Unknown"])(
21
+ "falls back to the logical key when code '%s' is not mapped",
22
+ (code) => {
23
+ expect(ghosttyKeyForEvent({ code, key: "Backspace" })).toBe(ghosttyKeyForCode("Backspace"));
24
+ },
25
+ );
26
+
27
+ it("prefers recognized physical codes over logical keys", () => {
28
+ expect(ghosttyKeyForEvent({ code: "Delete", key: "Backspace" })).toBe(
29
+ ghosttyKeyForCode("Delete"),
30
+ );
31
+ expect(ghosttyKeyForEvent({ code: "NumpadEnter", key: "Enter" })).toBe(
32
+ ghosttyKeyForCode("NumpadEnter"),
33
+ );
34
+ });
35
+
36
+ it("leaves printable characters and unknown keys to the text encoding path", () => {
37
+ expect(ghosttyKeyForEvent({ code: "", key: "a" })).toBe(0);
38
+ expect(ghosttyKeyForEvent({ code: "", key: "Unidentified" })).toBe(0);
39
+ });
40
+});
41
+
42
describe("ghosttyConsumedMods", () => {
43
const shifted = { altKey: false, ctrlKey: false, key: "@", metaKey: false, shiftKey: true };
44
apps/web/src/terminal/ghostty/keyCodes.ts
+5
@@ -188,6 +188,11 @@ export function ghosttyKeyForCode(code: string): number {
188
return codeToGhosttyKey.get(code) ?? 0;
189
}
190
191
+export function ghosttyKeyForEvent(event: Pick<KeyboardEvent, "code" | "key">): number {
192
+ // Android browsers can omit the physical code for keys such as Backspace.
193
+ return ghosttyKeyForCode(event.code) || ghosttyKeyForCode(event.key);
194
+}
195
+
196
export interface GhosttyKeyboardLayoutMap {
197
get(code: string): string | undefined;
198
}