Skip to content

useKeyPress ​

sensors
test coverage
Last changed: 24 days ago

Hook that listens for key press events

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

Usage ​

typescript
const isKeyPressed = useKeyPress('a', window);
// or
const { pressed, ref } = useKeyPress('a');

Demo ​

Api ​

Parameters

NameTypeDefaultNote
target?HookTargetwindowThe target to attach the event listeners to
keyUseKeyPressKey-The key or keys to listen for
callback?(pressed: boolean, event: KeyboardEvent) => void-Callback function invoked when key is pressed

Returns

boolean

Parameters

NameTypeDefaultNote
keyUseKeyPressKey-The key or keys to listen for
callback?(pressed: boolean, event: KeyboardEvent) => void-Callback function invoked when key is pressed

Source ​

Source • Demo

Contributors ​

Avatardebabin
Avatarhywax
AvatarGorilla Dev

Released under the MIT License.