Mousetrap

A simple, dependency-free JavaScript library for binding keyboard shortcuts

Library
npm
v1.6.5
11,776 stars
Apache License 2.0

Repository Health

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

Technical Analysis

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

Mousetrap is a small, dependency-free JavaScript library for binding keyboard shortcuts in the browser — single keys, modifier combinations like command+shift+k, and Gmail-style key sequences like g i or the Konami code. At roughly 2KB minified and gzipped, it has no external dependencies and has historically supported browsers back to Internet Explorer 6.

The core library handles keypress, keydown, and keyup binding with a simple Mousetrap.bind(keys, callback, event) API, and its functionality is extended through a small set of official plugins (global binding outside focused inputs, pausing/unpausing all bindings, recording key sequences, and binding from a dictionary of named shortcuts) rather than baking every feature into the core.

What You Get

  • Mousetrap.bind(keys, callback, event) for registering single keys, modifier combinations, or multi-key sequences
  • Support for keypress, keydown, and keyup event types on the same binding API
  • Konami-code-style and Gmail-style sequence binding (e.g. g i, up up down down left right left right b a enter)
  • Official plugins for global binding (shortcuts that fire even while an input is focused), pausing/unpausing all bindings, recording key sequences, and binding from a name-to-shortcut dictionary
  • Zero external runtime dependencies and a small (~2KB gzipped) footprint

Common Use Cases

  • Adding power-user keyboard shortcuts to web applications (e.g. Gmail-style g i navigation shortcuts)
  • Implementing global application shortcuts that need to work even when a text input has focus, via the global-bind plugin
  • Building customizable keyboard-shortcut settings screens by combining the bind-dictionary and record plugins to capture and label user-defined shortcuts
  • Adding modal or context-specific keyboard shortcuts that can be paused/resumed via the pause plugin when a dialog opens

Under The Hood

Architecture The entire core library lives in a single ~1,058-line mousetrap.js file structured as a self-invoking function that returns a Mousetrap constructor/singleton. Internally it maintains a direct map from normalized key-combination strings to callback arrays, listens for keydown/keyup/keypress at the document level, and normalizes cross-browser key codes and modifier detection (handling historical quirks like differing keyCode values across browsers) before matching the pressed sequence against registered bindings, including multi-key sequence state tracking for combos like g i. Additional behavior — global binding, pause/resume, sequence recording, dictionary-based binding — is deliberately kept out of the core and shipped as separate plugin files under plugins/, each extending the core Mousetrap object rather than being conditionally compiled in.

Tech Stack Plain vanilla JavaScript (no TypeScript, no framework dependency), built and linted via Grunt (Gruntfile.js, including a complexity-analysis task), with mousetrap.min.js as the pre-built minified distributable checked into the repo alongside the source. Tests run via Mocha with Chai assertions and jsdom/jsdom-global to simulate a DOM environment without a real browser.

Code Quality The project has a dedicated tests/ suite run via npm test (mocha --reporter=nyan) and a Grunt complexity-analysis task, suggesting some attention to keeping the single large core file maintainable despite its age and broad legacy-browser support requirements. Being last released in 2020, the library is stable but not actively evolving; its cross-browser key-code normalization logic, while effective, reflects patterns from an era before the standardized KeyboardEvent.key API was universally available.

API Design The public API is a single global object with a small, memorable method set (bind, unbind, trigger, reset) and a string-based key-combination syntax ('command+shift+k', 'g i') that reads close to natural language, requiring no build step or configuration — just <script src="mousetrap.min.js"> or require('mousetrap') and a call to bind. This low-ceremony design is a major reason for its long-standing popularity, though the plugin-based extension model means some commonly wanted behavior (global shortcuts, pausing) requires pulling in additional files rather than being available out of the box.

Used by 13 apps in this directory

TypeScript
76%
AGPL 3.0

Abby

Developer Tools · Product Management

166

Statically typed feature flags, remote config, and A/B testing with framework-native SDKs for TypeScript teams.

View details
31
Repo Health
70
Technical
64
Dependency
Built with
TypeScript 76%
MDX 18%
Updated 1 years ago
Vue
46%
MIT

flatnotes

Knowledge Management · Note Taking

3,235

Self-hosted, database-free note-taking with plain Markdown files, full-text search, wikilinks, and zero proprietary lock-in.

View details
71
Repo Health
64
Technical
79
Dependency
Built with
Vue 46%
Python 32%
JavaScript 13%
Updated 1 months ago
Ruby
67%
Other

GitLab

Developer Tools · Devops

24,552

The complete DevOps platform that unifies Git hosting, CI/CD, issue tracking, and security scanning into a single self-hostable application.

View details
87
Repo Health
86
Technical
67
Dependency
Built with
Ruby 67%
JavaScript 20%
Updated 4 days ago
TypeScript
83%
Apache 2.0

Grist

Databases · No Code Platforms

11,875

A modern relational spreadsheet that combines Python-powered formulas, drag-and-drop dashboards, and granular access controls in a self-hostable, SQLite-backed data platform.

View details
91
Repo Health
93
Technical
66
Dependency
Built with
TypeScript 83%
Python 11%
Updated 5 days ago
PHP
78%
GPL 3.0

matomo

Analytics

21,905

Open-source, privacy-first web and app analytics that puts you in complete control of your data.

View details
95
Repo Health
82
Technical
62
Dependency
Built with
PHP 78%
Updated 4 days ago
PHP
82%
Other

Mautic

Automation · Ecommerce · Marketing

10,570

The world's largest open source marketing automation platform — own your data, run multi-channel campaigns, and escape vendor lock-in forever.

View details
97
Repo Health
76
Technical
63
Dependency
Built with
PHP 82%
Updated 1 weeks ago
JavaScript
88%
GPL 3.0

NodeBB

Community

15,227

Modern Node.js forum software with real-time WebSockets, multi-database support, and a plugin ecosystem — the community platform built for the open web and the Fediverse.

View details
97
Repo Health
78
Technical
70
Dependency
Built with
JavaScript 88%
Go Template 11%
Updated 4 days ago
Ruby
79%
GPL 3.0

OpenProject

Collaboration · Productivity · Project Management

16,229

The open source project management platform that unifies Gantt charts, agile boards, time tracking, and team collaboration under full self-hosted control.

View details
96
Repo Health
79
Technical
69
Dependency
Built with
Ruby 79%
TypeScript 13%
Updated 4 days ago
TypeScript
89%
MIT

Plasmic

CMS · Design Tools · Low Code Platforms

7,048

The open-source visual builder that lets teams design React apps and websites with drag-and-drop while integrating seamlessly with your codebase.

View details
81
Repo Health
78
Technical
64
Dependency
Built with
TypeScript 89%
Updated 5 days ago

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