Skip to content

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.

Terminal window
pnpm add shortcutter
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 again

Press ⌘ + K on macOS or Ctrl + K elsewhere.

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 hints
shortcutter.listen('Alt', (_event, { phase }) => toggleHints(phase === 'down'), {
phases: ['down', 'up'],
});

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 run
shortcutter.contexts.activate('modal', 'default'); // both, modal first