useKeydown
Calls a callback when one of the specified keys is pressed.
Calls a callback when one of the specified keys is pressed.
Usage
'use client';
import { useState } from 'react';
import { useKeydown } from '@prosazhin/pbcomponents';
function Example() {
const [open, setOpen] = useState(true);
useKeydown({ keys: ['Escape'], callback: () => setOpen(false) });
return open ? <div>Press Escape</div> : null;
}Hook API
Parameters
| Parameter | Type / variants | Purpose | Default |
|---|---|---|---|
keys | string[] | Keys the hook listens for. | — |
callback | (event: KeyboardEvent) => void | Called when a specified key is pressed. | — |
shouldHandleWhenDefaultPrevented? | boolean | Allows handling events with preventDefault() called. | — |
Returns
| Value | Type / variants | Purpose |
|---|---|---|
result | void | Returns nothing. |