|
@@ -5,6 +5,7 @@ import {useEffect, useState} from 'react';
|
|
|
*/
|
|
|
const useKeyPress = (targetKey: string, targetRef?: React.RefObject<HTMLElement>) => {
|
|
|
const [keyPressed, setKeyPressed] = useState(false);
|
|
|
+ const current = targetRef?.current ?? document.body;
|
|
|
|
|
|
useEffect(() => {
|
|
|
const downHandler = ({key}: KeyboardEvent) => {
|
|
@@ -19,7 +20,6 @@ const useKeyPress = (targetKey: string, targetRef?: React.RefObject<HTMLElement>
|
|
|
}
|
|
|
};
|
|
|
|
|
|
- const current = targetRef?.current ?? document.body;
|
|
|
current.addEventListener('keydown', downHandler);
|
|
|
current.addEventListener('keyup', upHandler);
|
|
|
|
|
@@ -27,7 +27,7 @@ const useKeyPress = (targetKey: string, targetRef?: React.RefObject<HTMLElement>
|
|
|
current.removeEventListener('keydown', downHandler);
|
|
|
current.removeEventListener('keyup', upHandler);
|
|
|
};
|
|
|
- }, [targetKey, targetRef]);
|
|
|
+ }, [targetKey, current]);
|
|
|
|
|
|
return keyPressed;
|
|
|
};
|