Fixed the bug where chords would stay active when user switches to another window

This commit is contained in:
Aria Minaei 2021-08-07 13:13:07 +02:00
parent 62f363f6d4
commit 0b783b82f7

View file

@ -4,7 +4,7 @@ type AcceptableCombo = 'Shift' | 'Meta' | 'Control' | 'Alt'
export default function useKeyDownCallback( export default function useKeyDownCallback(
combo: AcceptableCombo, combo: AcceptableCombo,
listener: (opts: {down: boolean; event: KeyboardEvent}) => void, listener: (opts: {down: boolean; event: KeyboardEvent | undefined}) => void,
) { ) {
const refs = useRef({combo, listener}) const refs = useRef({combo, listener})
refs.current = {combo, listener} refs.current = {combo, listener}
@ -21,11 +21,17 @@ export default function useKeyDownCallback(
} }
} }
function onBlur(event: unknown) {
refs.current.listener({down: false, event: undefined})
}
document.addEventListener('keydown', onKeyDown) document.addEventListener('keydown', onKeyDown)
document.addEventListener('keyup', onKeyUp) document.addEventListener('keyup', onKeyUp)
window.addEventListener('blur', onBlur)
return () => { return () => {
document.removeEventListener('keydown', onKeyDown) document.removeEventListener('keydown', onKeyDown)
document.removeEventListener('keyup', onKeyUp) document.removeEventListener('keyup', onKeyUp)
window.removeEventListener('blur', onBlur)
} }
}, []) }, [])
} }