Skip to content

useKeyPressEvent ​

Category
Sensors

Hook that listens for key press events on specified targets

typescript
import { useKeyPressEvent } from '@siberiacancode/reactuse';

Usage ​

typescript
useKeyPressEvent('Enter', window, () => console.log('Enter key pressed'));

Demo ​

Api ​

Parameters

NameTypeDefaultNote
keyUseKeyPressEventKey-The key or array of keys to listen for.
targetWindow-The window object to attach the event listener to.
listener(event: KeyboardEvent) => void-The callback function to be executed when the specified key or keys are pressed.
options?UseEventListenerOptions-The options for the event listener.

Returns

void

Parameters

NameTypeDefaultNote
keyUseKeyPressEventKey-The key or array of keys to listen for.
targetDocument-The document object to attach the event listener to.
listener(event: KeyboardEvent) => void-The callback function to be executed when the specified key or keys are pressed.
options?UseEventListenerOptions-The options for the event listener.

Returns

void

Parameters

NameTypeDefaultNote
keyUseKeyPressEventKey-The key or array of keys to listen for.
targetTarget-The target element to attach the event listener to.
listener(event: KeyboardEvent) => void-The callback function to be executed when the specified key or keys are pressed.
options?UseEventListenerOptions-The options for the event listener.

Returns

void

Parameters

NameTypeDefaultNote
keyUseKeyPressEventKey-The key or array of keys to listen for.
listener(event: KeyboardEvent) => void-The callback function to be executed when the specified key or keys are pressed.
options?UseEventListenerOptions-The options for the event listener.

Returns

void

Contributors ​

Source • Demo