is-hotkey

Check whether a keyboard event matches a hotkey string like 'mod+s' or 'cmd+alt+space'

Library
npm
v0.2.0
376 stars
MIT License

Repository Health

Pre-computed score based on development activity, maintenance, community, maturity, and trend momentum. How we score it →
47 /100 Fair
Development Activity 56
Maintenance 8
Community 44
Maturity 60
Momentum 20

Technical Analysis

AI-assessed by reading the actual repository — architecture, code quality, innovation, and documentation. How we score it →
66 /100 Good
Architecture 62
Code Quality 65
Innovation 50
Learning Curve 85

is-hotkey is a tiny (under 1kb minified+gzipped) utility that answers one question: does this KeyboardEvent match a given hotkey pattern? Instead of hand-rolling event.key/event.which/event.metaKey comparisons, you write hotkeys as natural strings like mod+s, where mod resolves to cmd on Mac and ctrl elsewhere, and pass the browser event straight through.

What You Get

  • isHotkey(hotkeyString, event) — a single function call returning true/false for a match against a KeyboardEvent
  • A curried form, isHotkey(hotkeyString), that returns a reusable matcher function so the hotkey string is parsed only once
  • Cross-platform mod keyword that resolves to cmd on macOS and ctrl on Windows/Linux automatically
  • Support for matching via either event.which (default, broadest compatibility) or event.key (layout-independent) depending on configuration

Common Use Cases

  • Wiring keyboard shortcuts (save, bold, undo) in a rich-text editor or WYSIWYG component’s onKeyDown handler
  • Adding app-wide keyboard shortcuts (command palette, search focus) without a full hotkey-management framework
  • Building editor or IDE-like web UIs that need to distinguish cmd+s on Mac from ctrl+s on Windows with one hotkey string
  • Performance-sensitive keydown handlers that curry a hotkey matcher once and reuse it across many events

Under The Hood

Architecture The entire implementation lives in a single 222-line file (src/index.js): a hotkey string like mod+alt+s is split on +, each token normalized against a lookup table of key aliases and modifier names (including the special cross-platform mod token), then compared against the event’s modifier flags (metaKey, ctrlKey, altKey, shiftKey) and its key code or key value. Tech Stack Plain ES2015+ JavaScript compiled with Babel to CommonJS (lib/index.js) plus a UMD build (lib/is-hotkey.umd.js) for direct <script> usage, with zero runtime dependencies — the package is intentionally kept under 1kb minified and gzipped. Code Quality A 259-line Mocha test suite in test/index.js exercises the parser and matcher against a range of hotkey strings and simulated events, giving solid coverage relative to the library’s small surface area, though the project has seen no formal releases logged on GitHub despite ongoing commits, suggesting slower, informal maintenance cadence. API Design The API is a single function with two calling conventions — direct isHotkey(str, event) for one-off checks, or curried isHotkey(str)(event) when the same hotkey will be tested repeatedly — which keeps integration to one line of code in a keydown handler with no configuration objects or setup required for the common case.

Join founders buildingwith open source

Opinionated takes, migration guides, cost-saving tips, and insights from the open source ecosystem.

Subscribe on Substack
Join 750+ subscribers