diff --git a/packages/use-keyboard-shortcuts/src/__tests__/use-keyboard-shortcuts.test.ts b/packages/use-keyboard-shortcuts/src/__tests__/use-keyboard-shortcuts.test.ts index 063f32bc..24885a0b 100644 --- a/packages/use-keyboard-shortcuts/src/__tests__/use-keyboard-shortcuts.test.ts +++ b/packages/use-keyboard-shortcuts/src/__tests__/use-keyboard-shortcuts.test.ts @@ -197,4 +197,44 @@ describe('useKeyboardShortcuts', () => { expect(handler).toHaveBeenCalledTimes(1); }); + + it('does not fire Cmd+K shortcut when Shift is also pressed', () => { + const handler = vi.fn(); + const shortcuts: ShortcutDef[] = [{ key: 'k', meta: true, handler }]; + + renderHook(() => useKeyboardShortcuts(shortcuts)); + fireKey('k', { metaKey: true, shiftKey: true }); + + expect(handler).not.toHaveBeenCalled(); + }); + + it('does not fire Cmd+K shortcut when Alt is also pressed', () => { + const handler = vi.fn(); + const shortcuts: ShortcutDef[] = [{ key: 'k', meta: true, handler }]; + + renderHook(() => useKeyboardShortcuts(shortcuts)); + fireKey('k', { metaKey: true, altKey: true }); + + expect(handler).not.toHaveBeenCalled(); + }); + + it('does not fire bare-key shortcut when meta is pressed', () => { + const handler = vi.fn(); + const shortcuts: ShortcutDef[] = [{ key: 'Escape', handler }]; + + renderHook(() => useKeyboardShortcuts(shortcuts)); + fireKey('Escape', { metaKey: true }); + + expect(handler).not.toHaveBeenCalled(); + }); + + it('does not fire bare-key shortcut when shift is pressed', () => { + const handler = vi.fn(); + const shortcuts: ShortcutDef[] = [{ key: '/', handler }]; + + renderHook(() => useKeyboardShortcuts(shortcuts)); + fireKey('/', { shiftKey: true }); + + expect(handler).not.toHaveBeenCalled(); + }); }); diff --git a/packages/use-keyboard-shortcuts/src/use-keyboard-shortcuts.ts b/packages/use-keyboard-shortcuts/src/use-keyboard-shortcuts.ts index 5307c300..75657404 100644 --- a/packages/use-keyboard-shortcuts/src/use-keyboard-shortcuts.ts +++ b/packages/use-keyboard-shortcuts/src/use-keyboard-shortcuts.ts @@ -35,9 +35,9 @@ export function useKeyboardShortcuts(shortcuts: ShortcutDef[]): void { const inInput = isInputElement(e.target); for (const shortcut of shortcuts) { - const metaMatch = shortcut.meta ? e.metaKey || e.ctrlKey : true; - const shiftMatch = shortcut.shift ? e.shiftKey : !e.shiftKey || !shortcut.meta; // only enforce no-shift when meta is set - const altMatch = shortcut.alt ? e.altKey : true; + const metaMatch = shortcut.meta ? e.metaKey || e.ctrlKey : !(e.metaKey || e.ctrlKey); + const shiftMatch = shortcut.shift ? e.shiftKey : !e.shiftKey; + const altMatch = shortcut.alt ? e.altKey : !e.altKey; // Normalize key comparison (case-insensitive for letters) const keyMatch = e.key.toLowerCase() === shortcut.key.toLowerCase();