Every keyboard interaction your web app will ever need.
Hotkeys, sequences, chords, scopes, layouts, and a recorder — composable, TypeScript-typed, and under 4KB.
The classics. Full modifier support, platform-aware mod, cross-browser key normalization.
Kb.on('mod+k', openPalette); Kb.on('shift+?', showHelp);
Ordered key runs like g g or ctrl+k ctrl+s. Configurable timeout between keys.
Kb.on(['g', 'g'], scrollTop); Kb.on(['g', 'h'], navHome);
Multiple keys held together within a time window. Auto-resets on window blur to avoid stuck keys.
Kb.chord(['j', 'k'], exitMode);
Stackable scopes for modals, editors, palettes. exclusive suppresses globals while active.
Kb.pushScope('modal', { exclusive: true, });
mod resolves to ⌘ on Mac, Ctrl on Windows/Linux. Format returns native glyphs per-OS.
Kb.platform // 'mac' Kb.format('mod+k') // ⌘K
Capture keystrokes with timing, replay at any speed. Perfect for macros, tutorials, tests.
Kb.record(); const r = Kb.stop(); await Kb.replay(r, 2);
3.2KB gzipped. Zero runtime dependencies. Ships ESM, UMD, and TypeScript declarations.
npm install @techzunction/keyboard.js
import Kb from '@techzunction/keyboard.js'; Kb.init(); // Cross-platform hotkey — ⌘K on Mac, Ctrl+K on Windows/Linux Kb.on('mod+k', () => openPalette()); // Sequences Kb.on(['g', 'g'], () => window.scrollTo(0, 0)); // Chords — keys held together Kb.chord(['j', 'k'], () => exitInsertMode()); // Scoped — exclusive scope suppresses globals while active const pop = Kb.pushScope('modal', { exclusive: true }); Kb.on('enter', submit, { scope: 'modal' }); // Record + replay for macros or tests Kb.record(); const frames = Kb.stop(); await Kb.replay(frames, 2); // 2× speed
Cards light up when their binding fires. Focus anywhere on the page.
mod resolves to Cmd on Mac, Ctrl on Windows/Linux.Bindings only fire in their active scope. Toggle below.
The classic. ⌘+K opens a searchable command list. ↑/↓ to navigate, Enter to run, Esc to close. Uses scope stacking — bindings outside the palette pause while it's open.
Because every keyboard library deserves a snake game. ↑↓←→ or WASD. Space to start / pause.
Try the Konami Code: ↑ ↑ ↓ ↓ ← → ← → B A
Classic sequence demo — 10 keys in order with a generous timeout.
Capture a key sequence, then replay it — events fire exactly as if you typed them.
Every fire is logged here. Most recent on top.
// Everything you saw above — in 15 lines. import Kb from 'keyboard.js'; Kb.init(); Kb.on('ctrl+k', () => openPalette()); Kb.on('shift+?', () => showHelp()); Kb.on(['g', 'g'], () => scrollTop()); Kb.on(['g', 'h'], () => navigate('/')); Kb.chord(['j', 'k'], () => exitInsertMode()); // Scoped bindings Kb.on('ctrl+s', save, { scope: 'editor' }); Kb.scope('editor'); // activate // Record + replay for tests or macros Kb.record(); const frames = Kb.stop(); await Kb.replay(frames);