mousetrap README

The project's README from github.com/ccampbell/mousetrap. As one markdown file: README.md.

Mousetrap

CDNJS

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

    <script>
        // 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');
        });
    </script>
    

Why Mousetrap?

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

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

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

    npm test
    

Documentation

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

mousetrap (ccampbell/mousetrap)

Searching Database…

11.8K ·GitHub
What is this?Mousetrap is a simple JavaScript library for handling keyboard shortcuts in the browser.
Is this a framework or a small standalone library?It's a tiny standalone library with no external dependencies and no framework required.
How do I add it to my page or project?Include mousetrap via a script tag before the closing body tag, or install it from npm and require it, then bind key events.
Do I need any other library or framework to use it?No, Mousetrap has zero external dependencies and works on its own.
What browsers does it actually support?It has been tested in Internet Explorer 6+, Safari, Firefox, and Chrome.
Where does this run — only in the browser, or also in Node?It runs in the browser for actual shortcut handling, but its test suite can also be run with Node.js.
How big is the library?Mousetrap is about 2kb minified and gzipped, or 4.5kb minified without gzip.
What kinds of key bindings can I create?It supports keypress, keydown, and keyup events on single keys, key combinations like ctrl+shift+k, and Gmail-style key sequences, plus programmatic triggering via trigger().
Why use Mousetrap instead of another shortcut library?It has no dependencies, works with international keyboard layouts and the numeric keypad, lets you bind special characters directly without specifying shift combos, and lets you choose keypress/keydown/keyup per binding.
How are the tests run?Unit tests run via mocha (npm test runs 'mocha --reporter=nyan tests/test.mousetrap.js'), or you can open tests/mousetrap.html directly in a browser to check compatibility.
Creator
ccampbell
Size
0.5 MB
Stars
11.8K
Age
14y
Commits · 90 Days