diff --git a/packages/app/src/context/command-keybind.test.ts b/packages/app/src/context/command-keybind.test.ts index c8e2dbb5d0f..6ca32401644 100644 --- a/packages/app/src/context/command-keybind.test.ts +++ b/packages/app/src/context/command-keybind.test.ts @@ -32,6 +32,14 @@ describe("command keybind helpers", () => { expect(matchKeybind(keybinds, new KeyboardEvent("keydown", { key: ",", ctrlKey: true, altKey: true }))).toBe(false) }) + test("matchKeybind handles control-letter browser key values", () => { + const keybinds = parseKeybind("ctrl+l") + + expect(matchKeybind(keybinds, new KeyboardEvent("keydown", { key: "\f", code: "KeyL", ctrlKey: true }))).toBe( + true, + ) + }) + test("matchKeybind supports bracket keys", () => { const keybinds = parseKeybind("mod+alt+[, mod+alt+]") const prev = keybinds[0] diff --git a/packages/app/src/context/command.tsx b/packages/app/src/context/command.tsx index 467a1115b55..c2cab1f5f1d 100644 --- a/packages/app/src/context/command.tsx +++ b/packages/app/src/context/command.tsx @@ -53,8 +53,15 @@ function signature(key: string, ctrl: boolean, meta: boolean, shift: boolean, al return `${key}:${mask}` } +function normalizeEventKey(event: KeyboardEvent) { + if (event.ctrlKey && event.key.length === 1 && event.key.charCodeAt(0) <= 0x1f && event.code.startsWith("Key")) { + return event.code.slice(3).toLowerCase() + } + return normalizeKey(event.key) +} + function signatureFromEvent(event: KeyboardEvent) { - return signature(normalizeKey(event.key), event.ctrlKey, event.metaKey, event.shiftKey, event.altKey) + return signature(normalizeEventKey(event), event.ctrlKey, event.metaKey, event.shiftKey, event.altKey) } function isAllowedEditableKeybind(id: string | undefined) { @@ -158,7 +165,7 @@ export function parseKeybind(config: string): Keybind[] { } export function matchKeybind(keybinds: Keybind[], event: KeyboardEvent): boolean { - const eventKey = normalizeKey(event.key) + const eventKey = normalizeEventKey(event) for (const kb of keybinds) { const keyMatch = kb.key === eventKey