"Fossies" - the Fresh Open Source Software Archive

Member "matomo/node_modules/mousetrap/README.md" (3 Aug 2022, 3260 Bytes) of package /linux/www/matomo-4.11.0.tar.gz:

As a special service "Fossies" has tried to format the requested source page into HTML format (assuming markdown format). Alternatively you can here view or download the uninterpreted source code file. A member file download can also be achieved by clicking within a package contents listing on the according byte size field.



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:

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

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

    or install mousetrap from npm and require it

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

        // single keys
        Mousetrap.bind('4', function() { console.log('4'); });
        Mousetrap.bind("?", function() { console.log('show shortcuts!'); });
        Mousetrap.bind('esc', function() { console.log('escape'); }, 'keyup');
        // combinations
        Mousetrap.bind('command+shift+k', function() { console.log('command shift k'); });
        // map multiple combinations to the same callback
        Mousetrap.bind(['command+k', 'ctrl+k'], function() {
            console.log('command k or control k');
            // return false to prevent default browser behavior
            // and stop event from bubbling
            return false;
        // gmail style sequences
        Mousetrap.bind('g i', function() { console.log('go to inbox'); });
        Mousetrap.bind('* a', function() { console.log('select all'); });
        // konami code!
        Mousetrap.bind('up up down down left right left right b a enter', function() {
            console.log('konami code');

Why Mousetrap?

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


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

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

    npm test


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