fix(shortcuts): ignore synthetic key events without a key

Guard shortcut parsing against synthetic key events that do not expose a string key.
This commit is contained in:
YYClaw
2026-07-17 16:51:21 +08:00
committed by GitHub
parent 6dba74c3c1
commit 71141559c2
2 changed files with 10 additions and 0 deletions

View File

@@ -98,6 +98,14 @@ describe("keyboard shortcut definitions", () => {
expect(shortcutMatchesEvent(null, keyEvent(key, modifiers), "macos")).toBe(false);
});
it("ignores synthetic events without a key, such as Chrome autofill", () => {
const event = keyEvent(undefined as unknown as string, { metaKey: true });
expect(shortcutFromEvent(event, "macos")).toBeNull();
expect(
shortcutMatchesEvent(createShortcutChord("K", { primary: true }), event, "macos"),
).toBe(false);
});
it("formats the same semantic binding for each platform", () => {
const shortcut = createShortcutChord("Enter", { primary: true });
expect(formatShortcut(shortcut, "macos")).toBe("⌘↵");

View File

@@ -114,6 +114,8 @@ const KEY_LABELS: Record<string, string> = {
};
function eventKey(event: KeyboardEvent): string | null {
// Synthetic events (e.g. Chrome autofill) may omit `key` entirely.
if (typeof event.key !== "string") return null;
if (MODIFIER_KEYS.has(event.key) || NON_ACTIONABLE_KEYS.has(event.key)) return null;
const key = KEY_LABELS[event.key] ?? event.key;
if (key.length === 1 && /[a-z]/i.test(key)) return key.toUpperCase();