Skip to content

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

NameTypeDefaultNote
target?HookTargetwindowThe target element to attach the event listener to
hotkeysstring-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?booleantrueEnable or disable the event listeners

Returns

void

Parameters

NameTypeDefaultNote
hotkeysstring-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?booleantrueEnable or disable the event listeners

Source ​

Source • Demo

Contributors ​

Avatardebabin
Avatarhywax

Released under the MIT License.