useHotkeys ​
sensors
test coverage
Last changed: 24 days ago
Hook that listens for hotkeys
typescript
import { useHotkeys } from '@siberiacancode/reactuse';
Usage ​
typescript
useHotkeys(ref, 'ctrl+a', () => console.log('hotkey pressed'));
// or
useHotkeys(ref, 'ctrl+a, ctrl+b', () => console.log('hotkey pressed'));
// or
const ref = useHotkeys('ctrl+a', () => console.log('hotkey pressed'));
// or
const ref = useHotkeys('ctrl+a, ctrl+b', () => console.log('hotkey pressed'));
Demo ​
Api ​
Parameters
Name | Type | Default | Note |
---|---|---|---|
target? | HookTarget | window | The target element to attach the event listener to |
hotkeys | string | - | The hotkey to listen for |
callback | (event: KeyboardEvent) => void | - | The callback function to execute when hotkey is pressed |
options.alias? | Record<string, string> | - | Alias map for hotkeys |
options.enabled? | boolean | true | Enable or disable the event listeners |
Returns
Parameters
Name | Type | Default | Note |
---|---|---|---|
hotkeys | string | - | The hotkey to listen for |
callback | (event: KeyboardEvent) => void | - | The callback function to execute when hotkey is pressed |
options.alias? | Record<string, string> | - | Alias map for hotkeys |
options.enabled? | boolean | true | Enable or disable the event listeners |