From 0b783b82f77e0db94fef7029f034b6688458b4a5 Mon Sep 17 00:00:00 2001 From: Aria Minaei Date: Sat, 7 Aug 2021 13:13:07 +0200 Subject: [PATCH] Fixed the bug where chords would stay active when user switches to another window --- theatre/studio/src/uiComponents/useKeyDownCallback.tsx | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/theatre/studio/src/uiComponents/useKeyDownCallback.tsx b/theatre/studio/src/uiComponents/useKeyDownCallback.tsx index 0fd4bb7..b29a892 100644 --- a/theatre/studio/src/uiComponents/useKeyDownCallback.tsx +++ b/theatre/studio/src/uiComponents/useKeyDownCallback.tsx @@ -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) } }, []) }