123456789101112131415161718192021222324252627282930313233 |
- import {useEffect, useState} from 'react';
- /**
- * Hook to detect when a specific key is being pressed
- */
- function useKeyPress(targetKey: React.KeyboardEvent['key']) {
- const [keyPressed, setKeyPressed] = useState(false);
- useEffect(() => {
- function downHandler({key}: KeyboardEvent) {
- if (key === targetKey) {
- setKeyPressed(true);
- }
- }
- function upHandler({key}: KeyboardEvent) {
- if (key === targetKey) {
- setKeyPressed(false);
- }
- }
- window.addEventListener('keydown', downHandler);
- window.addEventListener('keyup', upHandler);
- return () => {
- window.removeEventListener('keydown', downHandler);
- window.removeEventListener('keyup', upHandler);
- };
- }, [targetKey]);
- return keyPressed;
- }
- export default useKeyPress;
|