Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions packages/react-hotkeys-hook/src/lib/validators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,10 @@ export const isHotkeyMatchingKeyboardEvent = (e: KeyboardEvent, hotkey: Hotkey,

const mappedCode = mapCode(code)

if (useKey && keys?.length === 1 && keys.includes(producedKey)) {
return true
}

if (
!keys?.includes(mappedCode) &&
!['ctrl', 'control', 'unknown', 'meta', 'alt', 'shift', 'os'].includes(mappedCode)
!['ctrl', 'control', 'unknown', 'meta', 'alt', 'shift', 'os'].includes(mappedCode) &&
!(useKey && keys?.includes(producedKey))
) {
return false
}
Expand Down Expand Up @@ -130,6 +127,11 @@ export const isHotkeyMatchingKeyboardEvent = (e: KeyboardEvent, hotkey: Hotkey,
}
}

// Check if the key matches when useKey is enabled
if (useKey && keys?.length === 1 && keys.includes(producedKey)) {
return true
}

// All modifiers are correct, now check the key
// If the key is set, we check for the key
if (keys && keys.length === 1 && keys.includes(mappedCode)) {
Expand Down
24 changes: 24 additions & 0 deletions packages/react-hotkeys-hook/src/test/useHotkeys.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1473,6 +1473,30 @@ test('Should ignore modifiers if option is set', async () => {
expect(callback).toHaveBeenCalledTimes(2)
})

test("Should test for modifiers when useKey is true", async () => {
const user = userEvent.setup()
const callback = vi.fn()

renderHook(() => useHotkeys('meta+.', callback, { useKey: true }))
renderHook(() => useHotkeys('shift+a', callback, { useKey: true }))

await user.keyboard('{Meta>}.{/Meta}')

expect(callback).toHaveBeenCalledTimes(1)

await user.keyboard('.')

expect(callback).toHaveBeenCalledTimes(1)

await user.keyboard('{Shift>}a{/Shift}')

expect(callback).toHaveBeenCalledTimes(2)

await user.keyboard('a')

expect(callback).toHaveBeenCalledTimes(2)
});


test('should respect dependencies array if they are passed', async () => {
function Fixture() {
Expand Down