Fixed the bug where chords would stay active when user switches to another window
This commit is contained in:
parent
62f363f6d4
commit
0b783b82f7
1 changed files with 7 additions and 1 deletions
|
@ -4,7 +4,7 @@ type AcceptableCombo = 'Shift' | 'Meta' | 'Control' | 'Alt'
|
|||
|
||||
export default function useKeyDownCallback(
|
||||
combo: AcceptableCombo,
|
||||
listener: (opts: {down: boolean; event: KeyboardEvent}) => void,
|
||||
listener: (opts: {down: boolean; event: KeyboardEvent | undefined}) => void,
|
||||
) {
|
||||
const refs = useRef({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('keyup', onKeyUp)
|
||||
window.addEventListener('blur', onBlur)
|
||||
return () => {
|
||||
document.removeEventListener('keydown', onKeyDown)
|
||||
document.removeEventListener('keyup', onKeyUp)
|
||||
window.removeEventListener('blur', onBlur)
|
||||
}
|
||||
}, [])
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue