Documentation
Hooks
useKeyboard

useKeyboard

Bind any keyboard keys or hotkeys with handlers in a very simple way.

Press F, 1, 2, 3, or Ctrl + C

info

You can use any string case you want for the key codes, for example:

  • ctrl+f is valid.
  • Ctrl + F is valid.
  • ctrl + KeyF is also valid.

Type Definitions

type KeyboardEventCallback = (event: KeyboardEvent) => any;
type KeysRecord = Record<string, KeyboardEventCallback>;
 
const useKeyboard: <T extends EventTarget = Window>(args: {
    keys: KeysRecord;
    ref?: React.RefObject<T> | null;
    separator?: string;
}) => React.RefObject<T>;