v1.0.0 — released 3.2KB gzip 0 dependencies TypeScript-first MIT license

keyboard.js

Every keyboard interaction your web app will ever need.

3.2
KB gzipped
0
Runtime deps
6
Core features
0
Keys pressed here
One API, every pattern

Everything the keyboard can do.

Hotkeys, sequences, chords, scopes, layouts, and a recorder — composable, TypeScript-typed, and under 4KB.

Hotkeys

The classics. Full modifier support, platform-aware mod, cross-browser key normalization.

Kb.on('mod+k', openPalette);
Kb.on('shift+?', showHelp);

Sequences

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);

Chords

Multiple keys held together within a time window. Auto-resets on window blur to avoid stuck keys.

Kb.chord(['j', 'k'], exitMode);

Scopes

Stackable scopes for modals, editors, palettes. exclusive suppresses globals while active.

Kb.pushScope('modal', {
  exclusive: true,
});

Cross-platform

mod resolves to ⌘ on Mac, Ctrl on Windows/Linux. Format returns native glyphs per-OS.

Kb.platform // 'mac'
Kb.format('mod+k') // ⌘K

Record & Replay

Capture keystrokes with timing, replay at any speed. Perfect for macros, tutorials, tests.

Kb.record();
const r = Kb.stop();
await Kb.replay(r, 2);

Install

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

Try it — press any binding

Cards light up when their binding fires. Focus anywhere on the page.

Hotkey

Mod+K
Cross-platform: mod resolves to Cmd on Mac, Ctrl on Windows/Linux.
0 fires

Hotkey

Shift+?
Show help overlay.
0 fires

Sequence

G G
Press twice within 1s.
0 fires

Sequence

G H
Go to home (Vim-style).
0 fires

Chord

J+K
Press both keys simultaneously.
0 fires

Hotkey

Esc
Close modals, cancel actions.
0 fires

Scopes

Bindings only fire in their active scope. Toggle below.

Active: global

Scope: editor

Mod+S
Only fires when editor scope is active. Switch scope above first.
0 fires

Scope: modal

Enter
Only fires in modal scope.
0 fires

Command Palette

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.

Open Command Palette
or press +K

Snake

Because every keyboard library deserves a snake game. or WASD. Space to start / pause.

Score
0
State
idle
Controls
W/ up · S/ down
A/ left · D/ right
Space start / pause
R reset

Easter egg

Try the Konami Code: B A

Classic sequence demo — 10 keys in order with a generous timeout.

🎮 Konami unlocked — +30 lives granted

Recorder

Capture a key sequence, then replay it — events fire exactly as if you typed them.

idle

Console

Every fire is logged here. Most recent on top.

0 events

Code

// 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);