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

ParameterType / variantsPurposeDefault
keysstring[]Keys the hook listens for.—
callback(event: KeyboardEvent) => voidCalled when a specified key is pressed.—
shouldHandleWhenDefaultPrevented?booleanAllows handling events with preventDefault() called.—

Returns

ValueType / variantsPurpose
resultvoidReturns nothing.

On this page