useKeyPress

A hook that elegantly manages KeyboardEvent of keyup adn keydown, Keyboard key combinations are supported to define key and keyCode alias input for keyboard events.

API is consistent with ahooks.

Examples

Basic usage

Compound mode

Advanced

API

useKeyPress(
  keyFilter: KeyFilter,
  eventHandler: EventHandler = noop,
  options?: Options
)

Params

Tips: keyType is the key or keyCode of KeyboardEvent.

PropertyDescriptionTypeDefault
keyFilterSupport for key and keyCode in keyboard events,function that return Boolean, key aliaseskeyType | Array<keyType> | ((event: KeyboardEvent) => boolean)-
eventHandlerCallback Function(event: KeyboardEvent) => void() => {}
optionsadvanced options,see Options below--

Options

PropertyDescriptionTypeDefault
eventsTrigger EventsArray<keydown | keyup>['keydown']
targetDOM element or Ref Object(() => HTMLElement) | HTMLElement | React.MutableRefObject-

Remarks

1.All key aliases

enter;
tab;
delete ('Backspace', 'Delete');
esc;
space;
up;
down;
left;
right;

2.Modifier keys

ctrl;
alt;
shift;
meta;

最近更新: