Skip to content

useKeyPressEvent ​

sensors
test coverage
Last changed: 24 days ago

Hook that listens for key press events on specified targets

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

Usage ​

typescript
useKeyPressEvent(ref, 'Enter', () => console.log('pressed'));
// or
const ref = useKeyPressEvent('Enter', (event) => console.log('pressed'));

Demo ​

Api ​

Parameters

NameTypeDefaultNote
keyUseKeyPressEventKey-The key or array of keys to listen for.
targetHookTarget | Window-The target 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?UseKeyPressEventOptions-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?UseKeyPressEventOptions-The options for the event listener.

Source ​

Source • Demo

Contributors ​

Avatardebabin
Avatarbabin
AvatarWinter010

Released under the MIT License.