useHotkeys
Keyboard shortcuts such as mod+k or escape, ignored while typing in fields unless allowed.
Source@monoline/react 0.5.0Markdown for AI toolsstable
- 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
Props
UseHotkeysOptions
| Prop | Type | Default | Description |
|---|---|---|---|
enabled | boolean | true | Set false to turn every binding off. Default true. |
allowInInputs | boolean | false | Also fire while focus is in a text field, select or contenteditable. Default false. |
preventDefault | boolean | true | Call `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.