Skip to content

Commit

Permalink
Merge pull request #1080 from brendanlaschke/1013-fix-duplicate-activ…
Browse files Browse the repository at this point in the history
…ation

Fix: duplicate hotkey activiation
  • Loading branch information
JohannesKlauss authored Oct 30, 2023
2 parents 0b4a0c2 + 7d87cf5 commit 559e298
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/validators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ export const isHotkeyMatchingKeyboardEvent = (e: KeyboardEvent, hotkey: Hotkey,
const keyCode = mapKey(code)
const pressedKey = pressedKeyUppercase.toLowerCase()

if (!keys?.includes(keyCode) && !['ctrl', 'control', 'unknown', 'meta', 'alt', 'shift', 'os'].includes(keyCode)) {
return false;
}

if (!ignoreModifiers) {
// We check the pressed keys for compatibility with the keyup event. In keyup events the modifier flags are not set.
if (alt === !altKey && pressedKey !== 'alt') {
Expand Down
32 changes: 32 additions & 0 deletions tests/useHotkeys.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,38 @@ test('should listen to multiple hotkeys', async () => {
expect(callback).toHaveBeenCalledTimes(2)
})

test('should be able to always output correct keys on multiple hotkeys', async () => {
const user = userEvent.setup()

const callbackA = jest.fn()
const callbackB = jest.fn()

renderHook(() => useHotkeys(['a'], callbackA))
renderHook(() => useHotkeys(['b'], callbackB))

await user.keyboard('{A>}')

expect(callbackA).toHaveBeenCalledTimes(1)

await user.keyboard('B')
expect(callbackA).toHaveBeenCalledTimes(1)
expect(callbackB).toHaveBeenCalledTimes(1)

await user.keyboard('C')

expect(callbackA).toHaveBeenCalledTimes(1)
expect(callbackB).toHaveBeenCalledTimes(1)

await user.keyboard('B')
expect(callbackA).toHaveBeenCalledTimes(1)
expect(callbackB).toHaveBeenCalledTimes(2)

await user.keyboard('{/A}')
expect(callbackA).toHaveBeenCalledTimes(1)
expect(callbackB).toHaveBeenCalledTimes(2)

})

test('should be able to parse first argument as string, array or readonly array', async () => {
const user = userEvent.setup()
const callback = jest.fn()
Expand Down

1 comment on commit 559e298

@vercel
Copy link

@vercel vercel bot commented on 559e298 Oct 30, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.