|
Download react-use/docs/useKeyPress.md from Devendra174/react-code-dataset: direct link, hf CLI and curl.
- Browser
- Download file 707 Bytes
-
https://huggingface.co/datasets/Devendra174/react-code-dataset/resolve/main/react-use/docs/useKeyPress.md
- Command line
-
hf download hf://datasets/Devendra174/react-code-dataset/react-use/docs/useKeyPress.md
-
curl -L -o useKeyPress.md https://huggingface.co/datasets/Devendra174/react-code-dataset/resolve/main/react-use/docs/useKeyPress.md
707 Bytes
| # `useKeyPress` | |
| React UI sensor hook that detects when the user is pressing a specific | |
| key on their keyboard. | |
| ## Usage | |
| ```jsx | |
| import {useKeyPress} from 'react-use'; | |
| const keys = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '0']; | |
| const Demo = () => { | |
| const states = []; | |
| for (const key of keys) states.push(useKeyPress(key)[0]); | |
| return ( | |
| <div style={{textAlign: 'center'}}> | |
| Try pressing numbers | |
| <br /> | |
| {states.reduce((s, pressed, index) => s + (pressed ? (s ? ' + ' : '') + keys[index] : ''), '')} | |
| </div> | |
| ); | |
| }; | |
| ``` | |
| ## Examples | |
| ```js | |
| const isPressed = useKeyPress('a'); | |
| const predicate = (event) => event.key === 'a'; | |
| const isPressed = useKeyPress(predicate); | |
| ``` | |