Skip to content

The agent that grows with you

fix(desktop): keybind normalization survives a missing KeyboardEvent.code (#91611)

Brooklyn Nicholsone63432152702
diff --git a/apps/desktop/src/lib/keybinds/combo.test.ts b/apps/desktop/src/lib/keybinds/combo.test.ts
index 1cb16dcc1db..07754ee8a56 100644
--- a/apps/desktop/src/lib/keybinds/combo.test.ts
+++ b/apps/desktop/src/lib/keybinds/combo.test.ts
@@ -165,3 +165,31 @@ describe('comboFromEvent — IME composition keydowns never resolve to combos (#
expect(comboFromEvent(keydown({ code: 'KeyN', isComposing: false, key: 'n', metaKey: true }))).toBe('mod+n')
})
})
+
+describe('comboFromEvent — malformed keyboard events (#91611)', () => {
+ // Built as plain objects rather than via `new KeyboardEvent`, because the
+ // constructor coerces `code` to a string ("undefined", "42") and would hide
+ // the very shapes under test (packaged-renderer TypeError logs in the issue).
+ const malformed = (init: Record<string, unknown>): KeyboardEvent => init as unknown as KeyboardEvent
+
+ it.each([
+ ['both key and code are absent', { code: undefined, key: undefined }],
+ ['code is null', { code: null, key: undefined }],
+ ['code is a number', { code: 42, key: undefined }],
+ ['code is an empty string', { code: '', key: undefined }]
+ ])('returns null when %s', (_label, init) => {
+ expect(comboFromEvent(malformed(init as Record<string, unknown>))).toBeNull()
+ })
+
+ // A junk `code` must make the physical fallback inert without discarding an
+ // otherwise legitimate event: some IME and synthetic keydowns carry an empty
+ // `code` alongside a real `key`, which still has to resolve via the key path.
+ it.each([
+ ['null', null],
+ ['a number', 42],
+ ['an empty string', '']
+ ])('resolves via event.key when code is %s but key is valid', (_label, code) => {
+ expect(comboFromEvent(malformed({ code, key: 'a' }))).toBe('a')
+ expect(comboFromEvent(malformed({ code, ctrlKey: true, key: 'k' }))).toBe('mod+k')
+ })
+})
diff --git a/apps/desktop/src/lib/keybinds/combo.ts b/apps/desktop/src/lib/keybinds/combo.ts
index 7815c3e4454..0113c35c987 100644
--- a/apps/desktop/src/lib/keybinds/combo.ts
+++ b/apps/desktop/src/lib/keybinds/combo.ts
@@ -57,7 +57,14 @@ const MODIFIER_CODES = new Set([
// Modifier names as reported by `event.key` on a bare modifier keydown.
const MODIFIER_KEYS = new Set(['Alt', 'Control', 'Meta', 'Shift'])
-function baseKeyFromCode(code: string): string | null {
+function baseKeyFromCode(code: unknown): string | null {
+ // event.code is typed string, but synthetic/IME keydowns can arrive without
+ // one (packaged-renderer TypeError reproductions in #91611); treat a
+ // non-string or empty code as "no physical key" instead of throwing.
+ if (typeof code !== 'string' || !code) {
+ return null
+ }
+
if (code.startsWith('Key')) {
return code.slice(3).toLowerCase()
}