Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clear all held hotkeys when window blurs #892

Merged
merged 5 commits into from
Dec 28, 2022
Merged
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
6 changes: 6 additions & 0 deletions src/isHotkeyPressed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,10 @@ export function removeFromCurrentlyPressedKeys(key: string): void {
removeFromCurrentlyPressedKeys(mapKey(e.code))
})
}

if (typeof window !== 'undefined') {
window.addEventListener('blur', () => {
currentlyPressedKeys.clear()
})
}
})()
10 changes: 5 additions & 5 deletions src/useHotkeys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export default function useHotkeys<T extends HTMLElement>(
return
}

const listener = (e: KeyboardEvent) => {
const listener = (e: KeyboardEvent, isKeyUp: boolean = false) => {
if (isKeyboardEventTriggeredByInput(e) && !isHotkeyEnabledOnTag(e, memoisedOptions?.enableOnFormTags)) {
return
}
Expand Down Expand Up @@ -77,7 +77,9 @@ export default function useHotkeys<T extends HTMLElement>(
// Execute the user callback for that hotkey
cb(e, hotkey)

hasTriggeredRef.current = true
if (!isKeyUp) {
hasTriggeredRef.current = true
}
}
})
}
Expand Down Expand Up @@ -106,10 +108,8 @@ export default function useHotkeys<T extends HTMLElement>(
hasTriggeredRef.current = false

if (memoisedOptions?.keyup) {
listener(event)
listener(event, true)
}

hasTriggeredRef.current = false
}

// @ts-ignore
Expand Down
22 changes: 15 additions & 7 deletions tests/isHotkeyPressed.test.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,6 @@
import userEvent from '@testing-library/user-event'
import { isHotkeyPressed } from '../src/isHotkeyPressed'

beforeEach(() => {
window.document.dispatchEvent(new Event("DOMContentLoaded", {
bubbles: true,
cancelable: true
}))
})

test('should return true if hotkey is currently pressed down', async () => {
const user = userEvent.setup()

Expand Down Expand Up @@ -110,3 +103,18 @@ test.skip('should respect the splitKey option', async () => {
expect(isHotkeyPressed('ba,', 'a')).toBe(false)
expect(isHotkeyPressed(['b', ','])).toBe(false)
})

test('Should clear pressed hotkeys when window blurs', async () => {
const user = userEvent.setup()

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

expect(isHotkeyPressed('meta')).toBe(true)

window.document.dispatchEvent(new Event("blur", {
bubbles: true,
cancelable: true
}))

expect(isHotkeyPressed('meta')).toBe(false)
})
File renamed without changes.