gitwtfhub

wtf is mousetrap?

ccampbell/mousetrap — explained in plain English

Analysis updated 2026-06-24

11,784JavaScriptAudience · developerComplexity · 1/5LicenseSetup · easy

TL;DR

Mousetrap is a tiny 2 KB JavaScript library with no dependencies that adds keyboard shortcuts to any web page, supporting single keys, combinations like Ctrl+K, and multi-key sequences like Gmail shortcuts.

Mindmap

mindmap
  root((mousetrap))
    What It Does
      Keyboard shortcuts
      Key sequences
      Programmatic triggers
    Shortcut Types
      Single key
      Key combinations
      Multi-key sequences
    Key Features
      No dependencies
      Cross-browser support
      2 KB footprint
    Use Cases
      Command palettes
      Dashboard navigation
      Easter eggs
    Audience
      Web developers
      App builders

Code map

Detail Auto

An interactive map of this repo's files and how they connect — its source is parsed live in your browser. Click Visualize to build it.

filefunction / class

Why would anyone build with this?

REASON 1

Add Ctrl+K or Cmd+K keyboard shortcuts to open a command palette in a web app.

REASON 2

Implement Gmail-style sequential key shortcuts where pressing two letters in order jumps to a specific section.

REASON 3

Bind the Konami code key sequence to trigger an easter egg in a web application.

REASON 4

Create keyboard navigation for a dashboard so power users can move between views without touching the mouse.

What's in the stack?

JavaScript

How it stacks up

ccampbell/mousetraprelaxedjs/relaxedsemporia/tiktok-unlock
Stars11,78411,79211,796
LanguageJavaScriptJavaScriptJavaScript
Setup difficultyeasyeasyhard
Complexity1/52/53/5
Audiencedeveloperdevelopergeneral

Figures from each repo's GitHub metadata at analysis time.

How do you spin it up?

Difficulty · easy Time to first run · 5min
Apache 2.0, use freely for any purpose including commercial projects, modify and distribute as long as you keep the license notice.

Wtf does this do

Mousetrap is a small JavaScript library that lets you add keyboard shortcuts to a web page. When someone presses a key or key combination, Mousetrap triggers whatever action you specify, whether that is opening a menu, navigating to a section, or running any custom behavior. The library weighs around 2 kilobytes when compressed, so it loads quickly without noticeably slowing down your page. It has no dependencies on other frameworks or libraries, which means it fits into any web project regardless of what tools you are already using. It has been tested in browsers going back to Internet Explorer 6 as well as modern browsers like Chrome, Firefox, and Safari. You can listen for single key presses, key combinations such as Control+K or Command+Shift+K, or sequences of keys typed one after another. Sequences follow the same pattern as Gmail keyboard shortcuts, where typing two letters in order triggers a specific action. The README even shows the Konami code, a long sequence of directional keys, bound to a callback as a demo of what is possible. You can also trigger key events from code programmatically, not just from user input. Setting it up involves adding a script tag to your page, then calling Mousetrap.bind() with the key or combination you want and a function to run when it is pressed. The library handles differences between operating systems and keyboard layouts automatically. You can bind multiple combinations to the same action at once, which is useful when Mac users press Command+K and Windows users press Control+K to do the same thing. The project is open source under the Apache 2.0 license and the full documentation is available on the author's site.

Yoink these prompts

Prompt 1
Using Mousetrap, add Ctrl+K on Windows and Cmd+K on Mac that both open the same search modal in my web app.
Prompt 2
Show me how to bind a two-key sequence in Mousetrap, like pressing G then I, to navigate to the inbox section of my app.
Prompt 3
I want to add Mousetrap to a React app. Write the useEffect hook that registers shortcuts on mount and cleans them up on unmount.
Prompt 4
Using Mousetrap, bind the Konami code sequence to show a hidden Easter egg div on my marketing page.
Prompt 5
How do I use Mousetrap.trigger() to programmatically fire a keyboard shortcut from a button click in my app?

Frequently asked questions

wtf is mousetrap?

Mousetrap is a tiny 2 KB JavaScript library with no dependencies that adds keyboard shortcuts to any web page, supporting single keys, combinations like Ctrl+K, and multi-key sequences like Gmail shortcuts.

What language is mousetrap written in?

Mainly JavaScript. The stack also includes JavaScript.

What license does mousetrap use?

Apache 2.0, use freely for any purpose including commercial projects, modify and distribute as long as you keep the license notice.

How hard is mousetrap to set up?

Setup difficulty is rated easy, with roughly 5min to a first successful run.

Who is mousetrap for?

Mainly developer.

View the repo → Decode another repo

This repo across BitVibe Labs

Don't trust strangers blindly. Verify against the repo.