Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
M

mousetrap

> 编程语言
Open source

Simple library for handling keyboard shortcuts in Javascript

11.8K stars0 likes4 views
WebsiteGitHub

About

Simple library for handling keyboard shortcuts in Javascript

Mousetrap

Mousetrap is a simple library for handling keyboard shortcuts in Javascript.

It is licensed under the Apache 2.0 license.

It is around 2kb minified and gzipped and 4.5kb minified, has no external dependencies, and has been tested in the following browsers:

  • Internet Explorer 6+
  • Safari
  • Firefox
  • Chrome

It has support for keypress, keydown, and keyup events on specific keys, keyboard combinations, or key sequences.

Getting started

  1. Include mousetrap on your page before the closing </body> tag

    xml
    <script src="/path/to/mousetrap.min.js"></script>

    or install mousetrap from npm and require it

    javascript
    var Mousetrap = require('mousetrap');
  2. Add some keyboard events to listen for

…

Why Mousetrap?

There are a number of other similar libraries out there so what makes this one different?

  • There are no external dependencies, no framework is required
  • You are not limited to keydown events (You can specify keypress, keydown, or keyup or let Mousetrap choose for you).
  • You can bind key events directly to special keys such as ? or * without having to specify shift+/ or shift+8 which are not consistent across all keyboards
  • It works with international keyboard layouts
  • You can bind Gmail like key sequences in addition to regular keys and key combinations
  • You can programatically trigger key events with the trigger() method
  • It works with the numeric keypad on your keyboard
  • The code is well documented/commented

Tests

Unit tests are run with mocha.

Running in browser

View it online to check your browser compatibility. You may also download the repo and open tests/mousetrap.html in your browser.

Running with Node.js

  1. Install development dependencies

    bash
    cd /path/to/repo
    npm install
  2. Run tests

    bash
    npm test

Documentation

Full documentation can be found at https://craig.is/killing/mice

Issues· 234 open

View all issuesOpen on GitHub
  • #30

    No way to prevent default for keys pressed as part of a sequence

    bugUpdated Jan 2, 2026
  • #54

    ctrl + num + num

    bugUpdated Jan 2, 2026
  • #47

    Check for scrolling with arrow keys

    featureUpdated Jan 2, 2026
  • #24

    For fun: Make the logo interactive

    featureUpdated Jan 2, 2026
  • #442

    please specify clearly this lib does not support multiple bindings to same key

    Updated Jun 11, 2025
  • #316

    Multiple modifier keys will not bind to 'keyup'

    Updated May 19, 2025
  • #161

    No way to detect "Next" button for input types of "number", "tel", etc. on Android

    Updated Mar 21, 2025
  • #516

    Allow mousetrap to work in inputs

    Updated Mar 18, 2025
  • #518

    Tired of working for free ?

    Updated Feb 3, 2025
  • #517

    keypress event is deprecated

    Updated Jul 31, 2024

> Tags

JavaScriptjavascriptkeyboardkeyboard-shortcutsmousetrap

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 28, 2026
Category编程语言
PricingOpen source

> Related tools

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言