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
Name | Type | Default | Note |
---|---|---|---|
target? | HookTarget | window | The target to attach the event listeners to |
key | UseKeyPressKey | - | The key or keys to listen for |
callback? | (pressed: boolean, event: KeyboardEvent) => void | - | Callback function invoked when key is pressed |
Returns
boolean
Parameters
Name | Type | Default | Note |
---|---|---|---|
key | UseKeyPressKey | - | The key or keys to listen for |
callback? | (pressed: boolean, event: KeyboardEvent) => void | - | Callback function invoked when key is pressed |