Getting started
shortcutter is a tiny, dependency-free library for keyboard shortcuts in the browser. Shortcuts live in contexts and fire in phases, so a modal, an editor and the rest of your app can each have their own keys.
Install
Section titled “Install”pnpm add shortcutternpm install shortcutteryarn add shortcutterYour first shortcut
Section titled “Your first shortcut”import { createShortcutter } from 'shortcutter';
const shortcutter = createShortcutter();
// Opens the palette with ⌘ K on macOS and Ctrl K elsewhere.const off = shortcutter.listen('ControlOrMeta+K', () => openCommandPalette(), { preventDefault: true,});
off(); // removes it againPress ⌘ + K on macOS or Ctrl + K elsewhere.
Phases
Section titled “Phases”A handler runs when the shortcut goes down unless you ask for more:
| Phase | When |
|---|---|
down |
every key of the shortcut became pressed |
press |
the shortcut is held and the system repeats the key |
up |
a key was released, another key joined, or the window blurred |
// Hold Alt to show hintsshortcutter.listen('Alt', (_event, { phase }) => toggleHints(phase === 'down'), { phases: ['down', 'up'],});Contexts
Section titled “Contexts”Only shortcuts in active contexts run. Every shortcut belongs to default unless you pass a context.
shortcutter.listen('Escape', closeModal, { context: 'modal' });
shortcutter.contexts.activate('modal'); // only modal shortcuts runshortcutter.contexts.activate('modal', 'default'); // both, modal first