Playground

useHotkeys

Keyboard shortcuts such as mod+k or escape, ignored while typing in fields unless allowed.

  • No DOM output. Shortcuts are additions: every control must still be reachable by keyboard and Tab.
  • mod means Cmd on macOS and Ctrl elsewhere. Typing in inputs is ignored unless allowInInputs is set.

Import

import '@monoline/css';
import {useHotkeys} from '@monoline/react';

Not installed yet? Pick a route.

Copies the source and its styles into your project.

npx monoline add use-hotkeys

Usage

import {useState} from 'react';
import {useHotkeys} from '@monoline/react';

function HotkeysDemo() {
  const [last, setLast] = useState('none yet');
  useHotkeys({
    'mod+k': () => setLast('mod+k'),
    'shift+h': () => setLast('shift+h'),
    escape: () => setLast('escape'),
  });
  return (
    <div>
      <p>Press Mod+K (Cmd on macOS, Ctrl elsewhere), Shift+H or Escape.</p>
      <p>Last shortcut: {last}</p>
    </div>
  );
}

<HotkeysDemo />

Examples

Shortcuts

Ignored while typing in a field.

Props

UseHotkeysOptions

PropTypeDefaultDescription
enabledbooleantrueSet false to turn every binding off. Default true.
allowInInputsbooleanfalseAlso fire while focus is in a text field, select or contenteditable. Default false.
preventDefaultbooleantrueCall `preventDefault` on a matched key. Default true.

Behaviour

  • No DOM output. Shortcuts are additions: every control must still be reachable by keyboard and Tab.
  • mod means Cmd on macOS and Ctrl elsewhere. Typing in inputs is ignored unless allowInInputs is set.