Contexts
Scope shortcuts to a modal, an editor or a page, and activate several at once.
A tiny, dependency-free library for shortcuts in the browser. Group them into contexts, react to every phase of a key press, and use them from any framework.
pnpm add shortcutterCtrl K
Opens a command palette. It switches to the palette context, so only its keys work while it is open.
Alt hold
Shows the page's shortcuts while held, using the down and up phases.
C E T
Copy the install command, jump to the live example, switch the theme. Ignored while you type in a field, like the palette's search.
G G orG D
Vim-style sequences from the optional shortcutter/sequences entry: back to the top, or to the docs.
Give each shortcut a description when you register it. shortcuts() gathers them, with the keys resolved for the platform (⌘ on macOS, Ctrl elsewhere), so a keyboard help screen is one loop and never goes out of date.
? works too, as in Gmail, on US-style keyboard layouts. Why other layouts differ
shortcutter.listen('ControlOrMeta+Slash', openHelp, { description: 'Show all keyboard shortcuts',});
const help = shortcutter.shortcuts({ active: true });// [{ keys: ['meta', 'slash'],// description: 'Show all keyboard shortcuts', … }]
help.forEach(({ keys, description }) => addRow(keys, description));Scope shortcuts to a modal, an editor or a page, and activate several at once.
Handle the press, the key repeat and the release: hold-to-peek and push-to-talk become one line.
No dependencies, under 3 kB gzipped, and safe to import on the server.
Plain TypeScript, plus adapters for React, Vue, Svelte and Solid that clean up after themselves.
The same counter, live. Press J and K; hold ⇧ to count in tens (Shift+K is a shortcut of its own). Switch frameworks with 1–5 (a single Digit* wildcard shortcut), or with ← → in the tabs.
import { createShortcutter } from 'shortcutter';
const shortcutter = createShortcutter();let count = 0;
shortcutter.listen('K', () => render((count += 1)));shortcutter.listen('J', () => render((count -= 1)));shortcutter.listen('Shift+K', () => render((count += 10)));shortcutter.listen('Shift+J', () => render((count -= 10)));Plain TypeScript
import { useShortcut } from '@shortcutter/react';
function Counter() { const [count, setCount] = useState(0);
useShortcut('K', () => setCount((c) => c + 1)); useShortcut('J', () => setCount((c) => c - 1)); useShortcut('Shift+K', () => setCount((c) => c + 10)); useShortcut('Shift+J', () => setCount((c) => c - 10));
return <output>{count}</output>;}React
<script setup lang="ts">import { ref } from 'vue';import { useShortcut } from '@shortcutter/vue';
const count = ref(0);
useShortcut('K', () => count.value++);useShortcut('J', () => count.value--);useShortcut('Shift+K', () => (count.value += 10));useShortcut('Shift+J', () => (count.value -= 10));</script>
<template><output>{{ count }}</output></template>Vue
<script lang="ts"> import { useShortcut } from '@shortcutter/svelte';
let count = $state(0);
useShortcut('K', () => count++); useShortcut('J', () => count--); useShortcut('Shift+K', () => (count += 10)); useShortcut('Shift+J', () => (count -= 10));</script>
<output>{count}</output>Svelte
import { createShortcut } from '@shortcutter/solid';
function Counter() { const [count, setCount] = createSignal(0);
createShortcut('K', () => setCount((c) => c + 1)); createShortcut('J', () => setCount((c) => c - 1)); createShortcut('Shift+K', () => setCount((c) => c + 10)); createShortcut('Shift+J', () => setCount((c) => c - 10));
return <output>{count()}</output>;}Solid