123456789101112131415161718192021222324252627 |
- import { useEffect, useState } from 'react';
- const useKeyPress = (targetKey: string): boolean => {
- const [keyPressed, setKeyPressed] = useState(false);
- const downHandler = ({ key }: { key: string }) => {
- if (key === targetKey) setKeyPressed(true);
- };
- const upHandler = ({ key }: { key: string }) => {
- if (key === targetKey) setKeyPressed(false);
- };
- useEffect(() => {
- window.addEventListener('keydown', downHandler);
- window.addEventListener('keyup', upHandler);
- return () => {
- window.removeEventListener('keydown', downHandler);
- window.removeEventListener('keyup', upHandler);
- };
- }, []);
- return keyPressed;
- };
- export default useKeyPress;
|