TypeScript
fix(web): handle Backspace when Android omits the key code (#18063)
JIAa440198ebd11
diff --git a/apps/web/src/terminal/ghostty/core.test.ts b/apps/web/src/terminal/ghostty/core.test.tsindex 5899efdf4bb..1075fc9d5c0 100644--- a/apps/web/src/terminal/ghostty/core.test.ts+++ b/apps/web/src/terminal/ghostty/core.test.ts@@ -108,6 +108,30 @@ describe("GhosttyTerminalCore snapshots", () => {vi.restoreAllMocks();});+ it.each([+ { code: "", key: "Backspace", expected: "\x7f" },+ { code: "Unidentified", key: "Backspace", expected: "\x7f" },+ { code: "Backspace", key: "Backspace", expected: "\x7f" },+ { code: "Delete", key: "Backspace", expected: "\x1b[3~" },+ { code: "", key: "a", expected: "a" },+ { code: "", key: "Unidentified", expected: "" },+ ])("encodes key $key with physical code '$code'", async ({ code, key, expected }) => {+ const core = await createCore();+ const event = {+ code,+ key,+ repeat: false,+ shiftKey: false,+ ctrlKey: false,+ altKey: false,+ metaKey: false,+ isComposing: false,+ getModifierState: () => false,+ } as unknown as KeyboardEvent;++ expect(core.encodeKey(event)).toBe(expected);+ });+it("preserves styles, wide cells, and selection after shared memory grows", async () => {const core = await createCore();const runtime = await loadGhosttyRuntime();diff --git a/apps/web/src/terminal/ghostty/core.ts b/apps/web/src/terminal/ghostty/core.tsindex 3ce9f0ceb51..3071bb12a6c 100644--- a/apps/web/src/terminal/ghostty/core.ts+++ b/apps/web/src/terminal/ghostty/core.ts@@ -1,7 +1,7 @@import {type GhosttyKeyboardLayoutMap,ghosttyConsumedMods,- ghosttyKeyForCode,+ ghosttyKeyForEvent,ghosttyUnshiftedCodepoint,loadGhosttyKeyboardLayoutMap,} from "./keyCodes";@@ -516,7 +516,7 @@ export class GhosttyTerminalCore {this.keyEvent,action === "release" ? 0 : event.repeat ? 2 : 1,);- this.runtime.call("ghostty_key_event_set_key", this.keyEvent, ghosttyKeyForCode(event.code));+ this.runtime.call("ghostty_key_event_set_key", this.keyEvent, ghosttyKeyForEvent(event));const mods =(event.shiftKey ? 1 : 0) |(event.ctrlKey ? 1 << 1 : 0) |diff --git a/apps/web/src/terminal/ghostty/keyCodes.test.ts b/apps/web/src/terminal/ghostty/keyCodes.test.tsindex 22dff2fae70..5cc965bf97d 100644--- a/apps/web/src/terminal/ghostty/keyCodes.test.ts+++ b/apps/web/src/terminal/ghostty/keyCodes.test.ts@@ -1,6 +1,11 @@import { describe, expect, it } from "vite-plus/test";-import { ghosttyConsumedMods, ghosttyKeyForCode, ghosttyUnshiftedCodepoint } from "./keyCodes";+import {+ ghosttyConsumedMods,+ ghosttyKeyForCode,+ ghosttyKeyForEvent,+ ghosttyUnshiftedCodepoint,+} from "./keyCodes";describe("ghosttyKeyForCode", () => {it("keeps the tail of the pinned Ghostty key enum in order", () => {@@ -11,6 +16,29 @@ describe("ghosttyKeyForCode", () => {});});+describe("ghosttyKeyForEvent", () => {+ it.each(["", "Unidentified", "Unknown"])(+ "falls back to the logical key when code '%s' is not mapped",+ (code) => {+ expect(ghosttyKeyForEvent({ code, key: "Backspace" })).toBe(ghosttyKeyForCode("Backspace"));+ },+ );++ it("prefers recognized physical codes over logical keys", () => {+ expect(ghosttyKeyForEvent({ code: "Delete", key: "Backspace" })).toBe(+ ghosttyKeyForCode("Delete"),+ );+ expect(ghosttyKeyForEvent({ code: "NumpadEnter", key: "Enter" })).toBe(+ ghosttyKeyForCode("NumpadEnter"),+ );+ });++ it("leaves printable characters and unknown keys to the text encoding path", () => {+ expect(ghosttyKeyForEvent({ code: "", key: "a" })).toBe(0);+ expect(ghosttyKeyForEvent({ code: "", key: "Unidentified" })).toBe(0);+ });+});+describe("ghosttyConsumedMods", () => {const shifted = { altKey: false, ctrlKey: false, key: "@", metaKey: false, shiftKey: true };diff --git a/apps/web/src/terminal/ghostty/keyCodes.ts b/apps/web/src/terminal/ghostty/keyCodes.tsindex a643827d6f2..7751f9b14b5 100644--- a/apps/web/src/terminal/ghostty/keyCodes.ts+++ b/apps/web/src/terminal/ghostty/keyCodes.ts@@ -188,6 +188,11 @@ export function ghosttyKeyForCode(code: string): number {return codeToGhosttyKey.get(code) ?? 0;}+export function ghosttyKeyForEvent(event: Pick<KeyboardEvent, "code" | "key">): number {+ // Android browsers can omit the physical code for keys such as Backspace.+ return ghosttyKeyForCode(event.code) || ghosttyKeyForCode(event.key);+}+export interface GhosttyKeyboardLayoutMap {get(code: string): string | undefined;}