Download react-use/src/useKeyboardJs.ts from Devendra174/react-code-dataset: direct link, hf CLI and curl.
- Browser
- Download file 745 Bytes
-
https://huggingface.co/datasets/Devendra174/react-code-dataset/resolve/1e92f2d0b9e054abc23a53595fd89ad3e2b4a34e/react-use/src/useKeyboardJs.ts
- Command line
-
hf download hf://datasets/Devendra174/react-code-dataset@1e92f2d0b9e054abc23a53595fd89ad3e2b4a34e/react-use/src/useKeyboardJs.ts
-
curl -L -o useKeyboardJs.ts https://huggingface.co/datasets/Devendra174/react-code-dataset/resolve/1e92f2d0b9e054abc23a53595fd89ad3e2b4a34e/react-use/src/useKeyboardJs.ts
745 Bytes
| import { useEffect, useState } from 'react'; | |
| import useMount from './useMount'; | |
| const useKeyboardJs = (combination: string | string[]) => { | |
| const [state, set] = useState<[boolean, null | KeyboardEvent]>([false, null]); | |
| const [keyboardJs, setKeyboardJs] = useState<any>(null); | |
| useMount(() => { | |
| import('keyboardjs').then((k) => setKeyboardJs(k.default || k)); | |
| }); | |
| useEffect(() => { | |
| if (!keyboardJs) { | |
| return; | |
| } | |
| const down = (event) => set([true, event]); | |
| const up = (event) => set([false, event]); | |
| keyboardJs.bind(combination, down, up, true); | |
| return () => { | |
| keyboardJs.unbind(combination, down, up); | |
| }; | |
| }, [combination, keyboardJs]); | |
| return state; | |
| }; | |
| export default useKeyboardJs; | |