Repository: ccampbell/mousetrap
Branch: master
Commit: 2f9a476ba615
Files: 22
Total size: 92.3 KB
Directory structure:
gitextract_1hmozx3b/
├── .gitignore
├── Gruntfile.js
├── LICENSE
├── README.md
├── mousetrap.js
├── mousetrap.sublime-project
├── package.json
├── plugins/
│ ├── README.md
│ ├── bind-dictionary/
│ │ ├── README.md
│ │ └── mousetrap-bind-dictionary.js
│ ├── global-bind/
│ │ ├── README.md
│ │ └── mousetrap-global-bind.js
│ ├── pause/
│ │ ├── README.md
│ │ └── mousetrap-pause.js
│ └── record/
│ ├── README.md
│ ├── mousetrap-record.js
│ └── tests/
│ ├── index.html
│ ├── jelly.css
│ └── jelly.js
└── tests/
├── libs/
│ └── key-event.js
├── mousetrap.html
└── test.mousetrap.js
================================================
FILE CONTENTS
================================================
================================================
FILE: .gitignore
================================================
node_modules
*.sublime-workspace
================================================
FILE: Gruntfile.js
================================================
/*jshint node:true */
module.exports = function(grunt) {
'use strict';
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
complexity: {
options: {
errorsOnly: false,
cyclomatic: 10,
halstead: 30,
maintainability: 85
},
generic: {
src: [
'mousetrap.js'
]
},
plugins: {
src: [
'plugins/**/*.js',
'!plugins/**/tests/**',
'!plugins/**/*.min.js'
]
}
}
});
grunt.loadNpmTasks('grunt-complexity');
grunt.registerTask('default', [
'complexity'
]);
};
================================================
FILE: LICENSE
================================================
Apache License
Version 2.0, January 2004
http://www.apache.org/licenses/
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
1. Definitions.
"License" shall mean the terms and conditions for use, reproduction,
and distribution as defined by Sections 1 through 9 of this document.
"Licensor" shall mean the copyright owner or entity authorized by
the copyright owner that is granting the License.
"Legal Entity" shall mean the union of the acting entity and all
other entities that control, are controlled by, or are under common
control with that entity. For the purposes of this definition,
"control" means (i) the power, direct or indirect, to cause the
direction or management of such entity, whether by contract or
otherwise, or (ii) ownership of fifty percent (50%) or more of the
outstanding shares, or (iii) beneficial ownership of such entity.
"You" (or "Your") shall mean an individual or Legal Entity
exercising permissions granted by this License.
"Source" form shall mean the preferred form for making modifications,
including but not limited to software source code, documentation
source, and configuration files.
"Object" form shall mean any form resulting from mechanical
transformation or translation of a Source form, including but
not limited to compiled object code, generated documentation,
and conversions to other media types.
"Work" shall mean the work of authorship, whether in Source or
Object form, made available under the License, as indicated by a
copyright notice that is included in or attached to the work
(an example is provided in the Appendix below).
"Derivative Works" shall mean any work, whether in Source or Object
form, that is based on (or derived from) the Work and for which the
editorial revisions, annotations, elaborations, or other modifications
represent, as a whole, an original work of authorship. For the purposes
of this License, Derivative Works shall not include works that remain
separable from, or merely link (or bind by name) to the interfaces of,
the Work and Derivative Works thereof.
"Contribution" shall mean any work of authorship, including
the original version of the Work and any modifications or additions
to that Work or Derivative Works thereof, that is intentionally
submitted to Licensor for inclusion in the Work by the copyright owner
or by an individual or Legal Entity authorized to submit on behalf of
the copyright owner. For the purposes of this definition, "submitted"
means any form of electronic, verbal, or written communication sent
to the Licensor or its representatives, including but not limited to
communication on electronic mailing lists, source code control systems,
and issue tracking systems that are managed by, or on behalf of, the
Licensor for the purpose of discussing and improving the Work, but
excluding communication that is conspicuously marked or otherwise
designated in writing by the copyright owner as "Not a Contribution."
"Contributor" shall mean Licensor and any individual or Legal Entity
on behalf of whom a Contribution has been received by Licensor and
subsequently incorporated within the Work.
2. Grant of Copyright License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
copyright license to reproduce, prepare Derivative Works of,
publicly display, publicly perform, sublicense, and distribute the
Work and such Derivative Works in Source or Object form.
3. Grant of Patent License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
(except as stated in this section) patent license to make, have made,
use, offer to sell, sell, import, and otherwise transfer the Work,
where such license applies only to those patent claims licensable
by such Contributor that are necessarily infringed by their
Contribution(s) alone or by combination of their Contribution(s)
with the Work to which such Contribution(s) was submitted. If You
institute patent litigation against any entity (including a
cross-claim or counterclaim in a lawsuit) alleging that the Work
or a Contribution incorporated within the Work constitutes direct
or contributory patent infringement, then any patent licenses
granted to You under this License for that Work shall terminate
as of the date such litigation is filed.
4. Redistribution. You may reproduce and distribute copies of the
Work or Derivative Works thereof in any medium, with or without
modifications, and in Source or Object form, provided that You
meet the following conditions:
(a) You must give any other recipients of the Work or
Derivative Works a copy of this License; and
(b) You must cause any modified files to carry prominent notices
stating that You changed the files; and
(c) You must retain, in the Source form of any Derivative Works
that You distribute, all copyright, patent, trademark, and
attribution notices from the Source form of the Work,
excluding those notices that do not pertain to any part of
the Derivative Works; and
(d) If the Work includes a "NOTICE" text file as part of its
distribution, then any Derivative Works that You distribute must
include a readable copy of the attribution notices contained
within such NOTICE file, excluding those notices that do not
pertain to any part of the Derivative Works, in at least one
of the following places: within a NOTICE text file distributed
as part of the Derivative Works; within the Source form or
documentation, if provided along with the Derivative Works; or,
within a display generated by the Derivative Works, if and
wherever such third-party notices normally appear. The contents
of the NOTICE file are for informational purposes only and
do not modify the License. You may add Your own attribution
notices within Derivative Works that You distribute, alongside
or as an addendum to the NOTICE text from the Work, provided
that such additional attribution notices cannot be construed
as modifying the License.
You may add Your own copyright statement to Your modifications and
may provide additional or different license terms and conditions
for use, reproduction, or distribution of Your modifications, or
for any such Derivative Works as a whole, provided Your use,
reproduction, and distribution of the Work otherwise complies with
the conditions stated in this License.
5. Submission of Contributions. Unless You explicitly state otherwise,
any Contribution intentionally submitted for inclusion in the Work
by You to the Licensor shall be under the terms and conditions of
this License, without any additional terms or conditions.
Notwithstanding the above, nothing herein shall supersede or modify
the terms of any separate license agreement you may have executed
with Licensor regarding such Contributions.
6. Trademarks. This License does not grant permission to use the trade
names, trademarks, service marks, or product names of the Licensor,
except as required for reasonable and customary use in describing the
origin of the Work and reproducing the content of the NOTICE file.
7. Disclaimer of Warranty. Unless required by applicable law or
agreed to in writing, Licensor provides the Work (and each
Contributor provides its Contributions) on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied, including, without limitation, any warranties or conditions
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
PARTICULAR PURPOSE. You are solely responsible for determining the
appropriateness of using or redistributing the Work and assume any
risks associated with Your exercise of permissions under this License.
8. Limitation of Liability. In no event and under no legal theory,
whether in tort (including negligence), contract, or otherwise,
unless required by applicable law (such as deliberate and grossly
negligent acts) or agreed to in writing, shall any Contributor be
liable to You for damages, including any direct, indirect, special,
incidental, or consequential damages of any character arising as a
result of this License or out of the use or inability to use the
Work (including but not limited to damages for loss of goodwill,
work stoppage, computer failure or malfunction, or any and all
other commercial damages or losses), even if such Contributor
has been advised of the possibility of such damages.
9. Accepting Warranty or Additional Liability. While redistributing
the Work or Derivative Works thereof, You may choose to offer,
and charge a fee for, acceptance of support, warranty, indemnity,
or other liability obligations and/or rights consistent with this
License. However, in accepting such obligations, You may act only
on Your own behalf and on Your sole responsibility, not on behalf
of any other Contributor, and only if You agree to indemnify,
defend, and hold each Contributor harmless for any liability
incurred by, or claims asserted against, such Contributor by reason
of your accepting any such warranty or additional liability.
END OF TERMS AND CONDITIONS
--- Exceptions to the Apache 2.0 License ----
As an exception, if, as a result of your compiling your source code, portions
of this Software are embedded into an Object form of such source code, you
may redistribute such embedded portions in such Object form without complying
with the conditions of Sections 4(a), 4(b) and 4(d) of the License.
In addition, if you combine or link compiled forms of this Software with
software that is licensed under the GPLv2 ("Combined Software") and if a
court of competent jurisdiction determines that the patent provision (Section
3), the indemnity provision (Section 9) or other Section of the License
conflicts with the conditions of the GPLv2, you may retroactively and
prospectively choose to deem waived or otherwise exclude such Section(s) of
the License, but only in their entirety and only with respect to the Combined
Software.
================================================
FILE: README.md
================================================
# Mousetrap
[](https://cdnjs.com/libraries/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
```html
<script src="/path/to/mousetrap.min.js"></script>
```
or install `mousetrap` from `npm` and require it
```js
var Mousetrap = require('mousetrap');
```
2. Add some keyboard events to listen for
```html
<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?
- 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 <a href="https://mochajs.org/">mocha</a>.
### Running in browser
[View it online](http://rawgit.com/ccampbell/mousetrap/master/tests/mousetrap.html) 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
```sh
cd /path/to/repo
npm install
```
3. Run tests
```sh
npm test
```
## Documentation
Full documentation can be found at https://craig.is/killing/mice
================================================
FILE: mousetrap.js
================================================
/*global define:false */
/**
* Copyright 2012-2017 Craig Campbell
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Mousetrap is a simple keyboard shortcut library for Javascript with
* no external dependencies
*
* @version 1.6.5
* @url craig.is/killing/mice
*/
(function(window, document, undefined) {
// Check if mousetrap is used inside browser, if not, return
if (!window) {
return;
}
/**
* mapping of special keycodes to their corresponding keys
*
* everything in this dictionary cannot use keypress events
* so it has to be here to map to the correct keycodes for
* keyup/keydown events
*
* @type {Object}
*/
var _MAP = {
8: 'backspace',
9: 'tab',
13: 'enter',
16: 'shift',
17: 'ctrl',
18: 'alt',
20: 'capslock',
27: 'esc',
32: 'space',
33: 'pageup',
34: 'pagedown',
35: 'end',
36: 'home',
37: 'left',
38: 'up',
39: 'right',
40: 'down',
45: 'ins',
46: 'del',
91: 'meta',
93: 'meta',
224: 'meta'
};
/**
* mapping for special characters so they can support
*
* this dictionary is only used incase you want to bind a
* keyup or keydown event to one of these keys
*
* @type {Object}
*/
var _KEYCODE_MAP = {
106: '*',
107: '+',
109: '-',
110: '.',
111 : '/',
186: ';',
187: '=',
188: ',',
189: '-',
190: '.',
191: '/',
192: '`',
219: '[',
220: '\\',
221: ']',
222: '\''
};
/**
* this is a mapping of keys that require shift on a US keypad
* back to the non shift equivelents
*
* this is so you can use keyup events with these keys
*
* note that this will only work reliably on US keyboards
*
* @type {Object}
*/
var _SHIFT_MAP = {
'~': '`',
'!': '1',
'@': '2',
'#': '3',
'$': '4',
'%': '5',
'^': '6',
'&': '7',
'*': '8',
'(': '9',
')': '0',
'_': '-',
'+': '=',
':': ';',
'\"': '\'',
'<': ',',
'>': '.',
'?': '/',
'|': '\\'
};
/**
* this is a list of special strings you can use to map
* to modifier keys when you specify your keyboard shortcuts
*
* @type {Object}
*/
var _SPECIAL_ALIASES = {
'option': 'alt',
'command': 'meta',
'return': 'enter',
'escape': 'esc',
'plus': '+',
'mod': /Mac|iPod|iPhone|iPad/.test(navigator.platform) ? 'meta' : 'ctrl'
};
/**
* variable to store the flipped version of _MAP from above
* needed to check if we should use keypress or not when no action
* is specified
*
* @type {Object|undefined}
*/
var _REVERSE_MAP;
/**
* loop through the f keys, f1 to f19 and add them to the map
* programatically
*/
for (var i = 1; i < 20; ++i) {
_MAP[111 + i] = 'f' + i;
}
/**
* loop through to map numbers on the numeric keypad
*/
for (i = 0; i <= 9; ++i) {
// This needs to use a string cause otherwise since 0 is falsey
// mousetrap will never fire for numpad 0 pressed as part of a keydown
// event.
//
// @see https://github.com/ccampbell/mousetrap/pull/258
_MAP[i + 96] = i.toString();
}
/**
* cross browser add event method
*
* @param {Element|HTMLDocument} object
* @param {string} type
* @param {Function} callback
* @returns void
*/
function _addEvent(object, type, callback) {
if (object.addEventListener) {
object.addEventListener(type, callback, false);
return;
}
object.attachEvent('on' + type, callback);
}
/**
* takes the event and returns the key character
*
* @param {Event} e
* @return {string}
*/
function _characterFromEvent(e) {
// for keypress events we should return the character as is
if (e.type == 'keypress') {
var character = String.fromCharCode(e.which);
// if the shift key is not pressed then it is safe to assume
// that we want the character to be lowercase. this means if
// you accidentally have caps lock on then your key bindings
// will continue to work
//
// the only side effect that might not be desired is if you
// bind something like 'A' cause you want to trigger an
// event when capital A is pressed caps lock will no longer
// trigger the event. shift+a will though.
if (!e.shiftKey) {
character = character.toLowerCase();
}
return character;
}
// for non keypress events the special maps are needed
if (_MAP[e.which]) {
return _MAP[e.which];
}
if (_KEYCODE_MAP[e.which]) {
return _KEYCODE_MAP[e.which];
}
// if it is not in the special map
// with keydown and keyup events the character seems to always
// come in as an uppercase character whether you are pressing shift
// or not. we should make sure it is always lowercase for comparisons
return String.fromCharCode(e.which).toLowerCase();
}
/**
* checks if two arrays are equal
*
* @param {Array} modifiers1
* @param {Array} modifiers2
* @returns {boolean}
*/
function _modifiersMatch(modifiers1, modifiers2) {
return modifiers1.sort().join(',') === modifiers2.sort().join(',');
}
/**
* takes a key event and figures out what the modifiers are
*
* @param {Event} e
* @returns {Array}
*/
function _eventModifiers(e) {
var modifiers = [];
if (e.shiftKey) {
modifiers.push('shift');
}
if (e.altKey) {
modifiers.push('alt');
}
if (e.ctrlKey) {
modifiers.push('ctrl');
}
if (e.metaKey) {
modifiers.push('meta');
}
return modifiers;
}
/**
* prevents default for this event
*
* @param {Event} e
* @returns void
*/
function _preventDefault(e) {
if (e.preventDefault) {
e.preventDefault();
return;
}
e.returnValue = false;
}
/**
* stops propogation for this event
*
* @param {Event} e
* @returns void
*/
function _stopPropagation(e) {
if (e.stopPropagation) {
e.stopPropagation();
return;
}
e.cancelBubble = true;
}
/**
* determines if the keycode specified is a modifier key or not
*
* @param {string} key
* @returns {boolean}
*/
function _isModifier(key) {
return key == 'shift' || key == 'ctrl' || key == 'alt' || key == 'meta';
}
/**
* reverses the map lookup so that we can look for specific keys
* to see what can and can't use keypress
*
* @return {Object}
*/
function _getReverseMap() {
if (!_REVERSE_MAP) {
_REVERSE_MAP = {};
for (var key in _MAP) {
// pull out the numeric keypad from here cause keypress should
// be able to detect the keys from the character
if (key > 95 && key < 112) {
continue;
}
if (_MAP.hasOwnProperty(key)) {
_REVERSE_MAP[_MAP[key]] = key;
}
}
}
return _REVERSE_MAP;
}
/**
* picks the best action based on the key combination
*
* @param {string} key - character for key
* @param {Array} modifiers
* @param {string=} action passed in
*/
function _pickBestAction(key, modifiers, action) {
// if no action was picked in we should try to pick the one
// that we think would work best for this key
if (!action) {
action = _getReverseMap()[key] ? 'keydown' : 'keypress';
}
// modifier keys don't work as expected with keypress,
// switch to keydown
if (action == 'keypress' && modifiers.length) {
action = 'keydown';
}
return action;
}
/**
* Converts from a string key combination to an array
*
* @param {string} combination like "command+shift+l"
* @return {Array}
*/
function _keysFromString(combination) {
if (combination === '+') {
return ['+'];
}
combination = combination.replace(/\+{2}/g, '+plus');
return combination.split('+');
}
/**
* Gets info for a specific key combination
*
* @param {string} combination key combination ("command+s" or "a" or "*")
* @param {string=} action
* @returns {Object}
*/
function _getKeyInfo(combination, action) {
var keys;
var key;
var i;
var modifiers = [];
// take the keys from this pattern and figure out what the actual
// pattern is all about
keys = _keysFromString(combination);
for (i = 0; i < keys.length; ++i) {
key = keys[i];
// normalize key names
if (_SPECIAL_ALIASES[key]) {
key = _SPECIAL_ALIASES[key];
}
// if this is not a keypress event then we should
// be smart about using shift keys
// this will only work for US keyboards however
if (action && action != 'keypress' && _SHIFT_MAP[key]) {
key = _SHIFT_MAP[key];
modifiers.push('shift');
}
// if this key is a modifier then add it to the list of modifiers
if (_isModifier(key)) {
modifiers.push(key);
}
}
// depending on what the key combination is
// we will try to pick the best event for it
action = _pickBestAction(key, modifiers, action);
return {
key: key,
modifiers: modifiers,
action: action
};
}
function _belongsTo(element, ancestor) {
if (element === null || element === document) {
return false;
}
if (element === ancestor) {
return true;
}
return _belongsTo(element.parentNode, ancestor);
}
function Mousetrap(targetElement) {
var self = this;
targetElement = targetElement || document;
if (!(self instanceof Mousetrap)) {
return new Mousetrap(targetElement);
}
/**
* element to attach key events to
*
* @type {Element}
*/
self.target = targetElement;
/**
* a list of all the callbacks setup via Mousetrap.bind()
*
* @type {Object}
*/
self._callbacks = {};
/**
* direct map of string combinations to callbacks used for trigger()
*
* @type {Object}
*/
self._directMap = {};
/**
* keeps track of what level each sequence is at since multiple
* sequences can start out with the same sequence
*
* @type {Object}
*/
var _sequenceLevels = {};
/**
* variable to store the setTimeout call
*
* @type {null|number}
*/
var _resetTimer;
/**
* temporary state where we will ignore the next keyup
*
* @type {boolean|string}
*/
var _ignoreNextKeyup = false;
/**
* temporary state where we will ignore the next keypress
*
* @type {boolean}
*/
var _ignoreNextKeypress = false;
/**
* are we currently inside of a sequence?
* type of action ("keyup" or "keydown" or "keypress") or false
*
* @type {boolean|string}
*/
var _nextExpectedAction = false;
/**
* resets all sequence counters except for the ones passed in
*
* @param {Object} doNotReset
* @returns void
*/
function _resetSequences(doNotReset) {
doNotReset = doNotReset || {};
var activeSequences = false,
key;
for (key in _sequenceLevels) {
if (doNotReset[key]) {
activeSequences = true;
continue;
}
_sequenceLevels[key] = 0;
}
if (!activeSequences) {
_nextExpectedAction = false;
}
}
/**
* finds all callbacks that match based on the keycode, modifiers,
* and action
*
* @param {string} character
* @param {Array} modifiers
* @param {Event|Object} e
* @param {string=} sequenceName - name of the sequence we are looking for
* @param {string=} combination
* @param {number=} level
* @returns {Array}
*/
function _getMatches(character, modifiers, e, sequenceName, combination, level) {
var i;
var callback;
var matches = [];
var action = e.type;
// if there are no events related to this keycode
if (!self._callbacks[character]) {
return [];
}
// if a modifier key is coming up on its own we should allow it
if (action == 'keyup' && _isModifier(character)) {
modifiers = [character];
}
// loop through all callbacks for the key that was pressed
// and see if any of them match
for (i = 0; i < self._callbacks[character].length; ++i) {
callback = self._callbacks[character][i];
// if a sequence name is not specified, but this is a sequence at
// the wrong level then move onto the next match
if (!sequenceName && callback.seq && _sequenceLevels[callback.seq] != callback.level) {
continue;
}
// if the action we are looking for doesn't match the action we got
// then we should keep going
if (action != callback.action) {
continue;
}
// if this is a keypress event and the meta key and control key
// are not pressed that means that we need to only look at the
// character, otherwise check the modifiers as well
//
// chrome will not fire a keypress if meta or control is down
// safari will fire a keypress if meta or meta+shift is down
// firefox will fire a keypress if meta or control is down
if ((action == 'keypress' && !e.metaKey && !e.ctrlKey) || _modifiersMatch(modifiers, callback.modifiers)) {
// when you bind a combination or sequence a second time it
// should overwrite the first one. if a sequenceName or
// combination is specified in this call it does just that
//
// @todo make deleting its own method?
var deleteCombo = !sequenceName && callback.combo == combination;
var deleteSequence = sequenceName && callback.seq == sequenceName && callback.level == level;
if (deleteCombo || deleteSequence) {
self._callbacks[character].splice(i, 1);
}
matches.push(callback);
}
}
return matches;
}
/**
* actually calls the callback function
*
* if your callback function returns false this will use the jquery
* convention - prevent default and stop propogation on the event
*
* @param {Function} callback
* @param {Event} e
* @returns void
*/
function _fireCallback(callback, e, combo, sequence) {
// if this event should not happen stop here
if (self.stopCallback(e, e.target || e.srcElement, combo, sequence)) {
return;
}
if (callback(e, combo) === false) {
_preventDefault(e);
_stopPropagation(e);
}
}
/**
* handles a character key event
*
* @param {string} character
* @param {Array} modifiers
* @param {Event} e
* @returns void
*/
self._handleKey = function(character, modifiers, e) {
var callbacks = _getMatches(character, modifiers, e);
var i;
var doNotReset = {};
var maxLevel = 0;
var processedSequenceCallback = false;
// Calculate the maxLevel for sequences so we can only execute the longest callback sequence
for (i = 0; i < callbacks.length; ++i) {
if (callbacks[i].seq) {
maxLevel = Math.max(maxLevel, callbacks[i].level);
}
}
// loop through matching callbacks for this key event
for (i = 0; i < callbacks.length; ++i) {
// fire for all sequence callbacks
// this is because if for example you have multiple sequences
// bound such as "g i" and "g t" they both need to fire the
// callback for matching g cause otherwise you can only ever
// match the first one
if (callbacks[i].seq) {
// only fire callbacks for the maxLevel to prevent
// subsequences from also firing
//
// for example 'a option b' should not cause 'option b' to fire
// even though 'option b' is part of the other sequence
//
// any sequences that do not match here will be discarded
// below by the _resetSequences call
if (callbacks[i].level != maxLevel) {
continue;
}
processedSequenceCallback = true;
// keep a list of which sequences were matches for later
doNotReset[callbacks[i].seq] = 1;
_fireCallback(callbacks[i].callback, e, callbacks[i].combo, callbacks[i].seq);
continue;
}
// if there were no sequence matches but we are still here
// that means this is a regular match so we should fire that
if (!processedSequenceCallback) {
_fireCallback(callbacks[i].callback, e, callbacks[i].combo);
}
}
// if the key you pressed matches the type of sequence without
// being a modifier (ie "keyup" or "keypress") then we should
// reset all sequences that were not matched by this event
//
// this is so, for example, if you have the sequence "h a t" and you
// type "h e a r t" it does not match. in this case the "e" will
// cause the sequence to reset
//
// modifier keys are ignored because you can have a sequence
// that contains modifiers such as "enter ctrl+space" and in most
// cases the modifier key will be pressed before the next key
//
// also if you have a sequence such as "ctrl+b a" then pressing the
// "b" key will trigger a "keypress" and a "keydown"
//
// the "keydown" is expected when there is a modifier, but the
// "keypress" ends up matching the _nextExpectedAction since it occurs
// after and that causes the sequence to reset
//
// we ignore keypresses in a sequence that directly follow a keydown
// for the same character
var ignoreThisKeypress = e.type == 'keypress' && _ignoreNextKeypress;
if (e.type == _nextExpectedAction && !_isModifier(character) && !ignoreThisKeypress) {
_resetSequences(doNotReset);
}
_ignoreNextKeypress = processedSequenceCallback && e.type == 'keydown';
};
/**
* handles a keydown event
*
* @param {Event} e
* @returns void
*/
function _handleKeyEvent(e) {
// normalize e.which for key events
// @see http://stackoverflow.com/questions/4285627/javascript-keycode-vs-charcode-utter-confusion
if (typeof e.which !== 'number') {
e.which = e.keyCode;
}
var character = _characterFromEvent(e);
// no character found then stop
if (!character) {
return;
}
// need to use === for the character check because the character can be 0
if (e.type == 'keyup' && _ignoreNextKeyup === character) {
_ignoreNextKeyup = false;
return;
}
self.handleKey(character, _eventModifiers(e), e);
}
/**
* called to set a 1 second timeout on the specified sequence
*
* this is so after each key press in the sequence you have 1 second
* to press the next key before you have to start over
*
* @returns void
*/
function _resetSequenceTimer() {
clearTimeout(_resetTimer);
_resetTimer = setTimeout(_resetSequences, 1000);
}
/**
* binds a key sequence to an event
*
* @param {string} combo - combo specified in bind call
* @param {Array} keys
* @param {Function} callback
* @param {string=} action
* @returns void
*/
function _bindSequence(combo, keys, callback, action) {
// start off by adding a sequence level record for this combination
// and setting the level to 0
_sequenceLevels[combo] = 0;
/**
* callback to increase the sequence level for this sequence and reset
* all other sequences that were active
*
* @param {string} nextAction
* @returns {Function}
*/
function _increaseSequence(nextAction) {
return function() {
_nextExpectedAction = nextAction;
++_sequenceLevels[combo];
_resetSequenceTimer();
};
}
/**
* wraps the specified callback inside of another function in order
* to reset all sequence counters as soon as this sequence is done
*
* @param {Event} e
* @returns void
*/
function _callbackAndReset(e) {
_fireCallback(callback, e, combo);
// we should ignore the next key up if the action is key down
// or keypress. this is so if you finish a sequence and
// release the key the final key will not trigger a keyup
if (action !== 'keyup') {
_ignoreNextKeyup = _characterFromEvent(e);
}
// weird race condition if a sequence ends with the key
// another sequence begins with
setTimeout(_resetSequences, 10);
}
// loop through keys one at a time and bind the appropriate callback
// function. for any key leading up to the final one it should
// increase the sequence. after the final, it should reset all sequences
//
// if an action is specified in the original bind call then that will
// be used throughout. otherwise we will pass the action that the
// next key in the sequence should match. this allows a sequence
// to mix and match keypress and keydown events depending on which
// ones are better suited to the key provided
for (var i = 0; i < keys.length; ++i) {
var isFinal = i + 1 === keys.length;
var wrappedCallback = isFinal ? _callbackAndReset : _increaseSequence(action || _getKeyInfo(keys[i + 1]).action);
_bindSingle(keys[i], wrappedCallback, action, combo, i);
}
}
/**
* binds a single keyboard combination
*
* @param {string} combination
* @param {Function} callback
* @param {string=} action
* @param {string=} sequenceName - name of sequence if part of sequence
* @param {number=} level - what part of the sequence the command is
* @returns void
*/
function _bindSingle(combination, callback, action, sequenceName, level) {
// store a direct mapped reference for use with Mousetrap.trigger
self._directMap[combination + ':' + action] = callback;
// make sure multiple spaces in a row become a single space
combination = combination.replace(/\s+/g, ' ');
var sequence = combination.split(' ');
var info;
// if this pattern is a sequence of keys then run through this method
// to reprocess each pattern one key at a time
if (sequence.length > 1) {
_bindSequence(combination, sequence, callback, action);
return;
}
info = _getKeyInfo(combination, action);
// make sure to initialize array if this is the first time
// a callback is added for this key
self._callbacks[info.key] = self._callbacks[info.key] || [];
// remove an existing match if there is one
_getMatches(info.key, info.modifiers, {type: info.action}, sequenceName, combination, level);
// add this call back to the array
// if it is a sequence put it at the beginning
// if not put it at the end
//
// this is important because the way these are processed expects
// the sequence ones to come first
self._callbacks[info.key][sequenceName ? 'unshift' : 'push']({
callback: callback,
modifiers: info.modifiers,
action: info.action,
seq: sequenceName,
level: level,
combo: combination
});
}
/**
* binds multiple combinations to the same callback
*
* @param {Array} combinations
* @param {Function} callback
* @param {string|undefined} action
* @returns void
*/
self._bindMultiple = function(combinations, callback, action) {
for (var i = 0; i < combinations.length; ++i) {
_bindSingle(combinations[i], callback, action);
}
};
// start!
_addEvent(targetElement, 'keypress', _handleKeyEvent);
_addEvent(targetElement, 'keydown', _handleKeyEvent);
_addEvent(targetElement, 'keyup', _handleKeyEvent);
}
/**
* binds an event to mousetrap
*
* can be a single key, a combination of keys separated with +,
* an array of keys, or a sequence of keys separated by spaces
*
* be sure to list the modifier keys first to make sure that the
* correct key ends up getting bound (the last key in the pattern)
*
* @param {string|Array} keys
* @param {Function} callback
* @param {string=} action - 'keypress', 'keydown', or 'keyup'
* @returns void
*/
Mousetrap.prototype.bind = function(keys, callback, action) {
var self = this;
keys = keys instanceof Array ? keys : [keys];
self._bindMultiple.call(self, keys, callback, action);
return self;
};
/**
* unbinds an event to mousetrap
*
* the unbinding sets the callback function of the specified key combo
* to an empty function and deletes the corresponding key in the
* _directMap dict.
*
* TODO: actually remove this from the _callbacks dictionary instead
* of binding an empty function
*
* the keycombo+action has to be exactly the same as
* it was defined in the bind method
*
* @param {string|Array} keys
* @param {string} action
* @returns void
*/
Mousetrap.prototype.unbind = function(keys, action) {
var self = this;
return self.bind.call(self, keys, function() {}, action);
};
/**
* triggers an event that has already been bound
*
* @param {string} keys
* @param {string=} action
* @returns void
*/
Mousetrap.prototype.trigger = function(keys, action) {
var self = this;
if (self._directMap[keys + ':' + action]) {
self._directMap[keys + ':' + action]({}, keys);
}
return self;
};
/**
* resets the library back to its initial state. this is useful
* if you want to clear out the current keyboard shortcuts and bind
* new ones - for example if you switch to another page
*
* @returns void
*/
Mousetrap.prototype.reset = function() {
var self = this;
self._callbacks = {};
self._directMap = {};
return self;
};
/**
* should we stop this event before firing off callbacks
*
* @param {Event} e
* @param {Element} element
* @return {boolean}
*/
Mousetrap.prototype.stopCallback = function(e, element) {
var self = this;
// if the element has the class "mousetrap" then no need to stop
if ((' ' + element.className + ' ').indexOf(' mousetrap ') > -1) {
return false;
}
if (_belongsTo(element, self.target)) {
return false;
}
// Events originating from a shadow DOM are re-targetted and `e.target` is the shadow host,
// not the initial event target in the shadow tree. Note that not all events cross the
// shadow boundary.
// For shadow trees with `mode: 'open'`, the initial event target is the first element in
// the event’s composed path. For shadow trees with `mode: 'closed'`, the initial event
// target cannot be obtained.
if ('composedPath' in e && typeof e.composedPath === 'function') {
// For open shadow trees, update `element` so that the following check works.
var initialEventTarget = e.composedPath()[0];
if (initialEventTarget !== e.target) {
element = initialEventTarget;
}
}
// stop for input, select, and textarea
return element.tagName == 'INPUT' || element.tagName == 'SELECT' || element.tagName == 'TEXTAREA' || element.isContentEditable;
};
/**
* exposes _handleKey publicly so it can be overwritten by extensions
*/
Mousetrap.prototype.handleKey = function() {
var self = this;
return self._handleKey.apply(self, arguments);
};
/**
* allow custom key mappings
*/
Mousetrap.addKeycodes = function(object) {
for (var key in object) {
if (object.hasOwnProperty(key)) {
_MAP[key] = object[key];
}
}
_REVERSE_MAP = null;
};
/**
* Init the global mousetrap functions
*
* This method is needed to allow the global mousetrap functions to work
* now that mousetrap is a constructor function.
*/
Mousetrap.init = function() {
var documentMousetrap = Mousetrap(document);
for (var method in documentMousetrap) {
if (method.charAt(0) !== '_') {
Mousetrap[method] = (function(method) {
return function() {
return documentMousetrap[method].apply(documentMousetrap, arguments);
};
} (method));
}
}
};
Mousetrap.init();
// expose mousetrap to the global object
window.Mousetrap = Mousetrap;
// expose as a common js module
if (typeof module !== 'undefined' && module.exports) {
module.exports = Mousetrap;
}
// expose mousetrap as an AMD module
if (typeof define === 'function' && define.amd) {
define(function() {
return Mousetrap;
});
}
}) (typeof window !== 'undefined' ? window : null, typeof window !== 'undefined' ? document : null);
================================================
FILE: mousetrap.sublime-project
================================================
{
"settings":
{
"detect_indentation": true,
"ensure_newline_at_eof_on_save": true,
"tab_size": 4,
"translate_tabs_to_spaces": true,
"trim_automatic_white_space": false,
"trim_trailing_white_space_on_save": true,
},
"folders":
[
{
"path": "./",
"folder_exclude_patterns": ["bin", "coverage", "node_modules"],
"file_exclude_patterns": ["*.sublime-workspace"]
}
]
}
================================================
FILE: package.json
================================================
{
"name": "mousetrap",
"version": "1.6.5",
"description": "Simple library for handling keyboard shortcuts",
"main": "mousetrap.js",
"directories": {
"test": "tests"
},
"scripts": {
"test": "mocha --reporter=nyan tests/test.mousetrap.js"
},
"repository": {
"type": "git",
"url": "git://github.com/ccampbell/mousetrap.git"
},
"keywords": [
"keyboard",
"shortcuts",
"events"
],
"author": "Craig Campbell",
"license": "Apache-2.0 WITH LLVM-exception",
"gitHead": "c202a0bd4967d5a3064f9cb376db51dec9345336",
"readmeFilename": "README.md",
"devDependencies": {
"chai": "^4.2.0",
"grunt": "~1.0.3",
"grunt-complexity": "~1.1.0",
"jsdom": "^13.1.0",
"jsdom-global": "^3.0.2",
"mocha": "^5.2.0",
"sinon": "^7.2.2"
}
}
================================================
FILE: plugins/README.md
================================================
# Plugins
Plugins extend the functionality of Mousetrap. To use a plugin just include the plugin after mousetrap.
```html
<script src="mousetrap.js"></script>
<script src="mousetrap-record.js"></script>
```
## Bind dictionary
Allows you to make multiple bindings in a single ``Mousetrap.bind`` call.
## Global bind
Allows you to set global bindings that work even inside of input fields.
## Pause/unpause
Allows you to temporarily prevent Mousetrap events from firing.
## Record
Allows you to capture a keyboard shortcut or sequence defined by a user.
================================================
FILE: plugins/bind-dictionary/README.md
================================================
# Bind Dictionary
This extension overwrites the default bind behavior and allows you to bind multiple combinations in a single bind call.
Usage looks like:
```javascript
Mousetrap.bind({
'a': function() { console.log('a'); },
'b': function() { console.log('b'); }
});
```
You can optionally pass in ``keypress``, ``keydown`` or ``keyup`` as a second argument.
Other bind calls work the same way as they do by default.
================================================
FILE: plugins/bind-dictionary/mousetrap-bind-dictionary.js
================================================
/**
* Overwrites default Mousetrap.bind method to optionally accept
* an object to bind multiple key events in a single call
*
* You can pass it in like:
*
* Mousetrap.bind({
* 'a': function() { console.log('a'); },
* 'b': function() { console.log('b'); }
* });
*
* And can optionally pass in 'keypress', 'keydown', or 'keyup'
* as a second argument
*
*/
/* global Mousetrap:true */
(function(Mousetrap) {
var _oldBind = Mousetrap.prototype.bind;
var args;
Mousetrap.prototype.bind = function() {
var self = this;
args = arguments;
// normal call
if (typeof args[0] == 'string' || args[0] instanceof Array) {
return _oldBind.call(self, args[0], args[1], args[2]);
}
// object passed in
for (var key in args[0]) {
if (args[0].hasOwnProperty(key)) {
_oldBind.call(self, key, args[0][key], args[1]);
}
}
};
Mousetrap.init();
}) (Mousetrap);
================================================
FILE: plugins/global-bind/README.md
================================================
# Global Bind
This extension allows you to specify keyboard events that will work anywhere including inside textarea/input fields.
Usage looks like:
```javascript
Mousetrap.bindGlobal('ctrl+s', function() {
_save();
});
```
This means that a keyboard event bound using ``Mousetrap.bind`` will only work outside of form input fields, but using ``Moustrap.bindGlobal`` will work in both places.
If you wanted to create keyboard shortcuts that only work when you are inside a specific textarea you can do that too by creating your own extension.
================================================
FILE: plugins/global-bind/mousetrap-global-bind.js
================================================
/**
* adds a bindGlobal method to Mousetrap that allows you to
* bind specific keyboard shortcuts that will still work
* inside a text input field
*
* usage:
* Mousetrap.bindGlobal('ctrl+s', _saveChanges);
*/
/* global Mousetrap:true */
(function(Mousetrap) {
if (! Mousetrap) {
return;
}
var _globalCallbacks = {};
var _originalStopCallback = Mousetrap.prototype.stopCallback;
Mousetrap.prototype.stopCallback = function(e, element, combo, sequence) {
var self = this;
if (self.paused) {
return true;
}
if (_globalCallbacks[combo] || _globalCallbacks[sequence]) {
return false;
}
return _originalStopCallback.call(self, e, element, combo);
};
Mousetrap.prototype.bindGlobal = function(keys, callback, action) {
var self = this;
self.bind(keys, callback, action);
if (keys instanceof Array) {
for (var i = 0; i < keys.length; i++) {
_globalCallbacks[keys[i]] = true;
}
return;
}
_globalCallbacks[keys] = true;
};
Mousetrap.init();
}) (typeof Mousetrap !== "undefined" ? Mousetrap : undefined);
================================================
FILE: plugins/pause/README.md
================================================
# Pause/unpause
This extension allows Mousetrap to be paused and unpaused without having to reset keyboard shortcuts and rebind them.
Usage looks like:
```javascript
// stop Mousetrap events from firing
Mousetrap.pause();
// allow Mousetrap events to fire again
Mousetrap.unpause();
```
================================================
FILE: plugins/pause/mousetrap-pause.js
================================================
/**
* adds a pause and unpause method to Mousetrap
* this allows you to enable or disable keyboard shortcuts
* without having to reset Mousetrap and rebind everything
*/
/* global Mousetrap:true */
(function(Mousetrap) {
var _originalStopCallback = Mousetrap.prototype.stopCallback;
Mousetrap.prototype.stopCallback = function(e, element, combo) {
var self = this;
if (self.paused) {
return true;
}
return _originalStopCallback.call(self, e, element, combo);
};
Mousetrap.prototype.pause = function() {
var self = this;
self.paused = true;
};
Mousetrap.prototype.unpause = function() {
var self = this;
self.paused = false;
};
Mousetrap.init();
}) (Mousetrap);
================================================
FILE: plugins/record/README.md
================================================
# Record
This extension lets you use Mousetrap to record keyboard sequences and play them back:
```html
<button onclick="recordSequence()">Record</button>
<script>
function recordSequence() {
Mousetrap.record(function(sequence) {
// sequence is an array like ['ctrl+k', 'c']
alert('You pressed: ' + sequence.join(' '));
});
}
</script>
```
================================================
FILE: plugins/record/mousetrap-record.js
================================================
/**
* This extension allows you to record a sequence using Mousetrap.
*
* @author Dan Tao <daniel.tao@gmail.com>
*/
(function(Mousetrap) {
/**
* the sequence currently being recorded
*
* @type {Array}
*/
var _recordedSequence = [],
/**
* a callback to invoke after recording a sequence
*
* @type {Function|null}
*/
_recordedSequenceCallback = null,
/**
* a list of all of the keys currently held down
*
* @type {Array}
*/
_currentRecordedKeys = [],
/**
* temporary state where we remember if we've already captured a
* character key in the current combo
*
* @type {boolean}
*/
_recordedCharacterKey = false,
/**
* a handle for the timer of the current recording
*
* @type {null|number}
*/
_recordTimer = null,
/**
* the original handleKey method to override when Mousetrap.record() is
* called
*
* @type {Function}
*/
_origHandleKey = Mousetrap.prototype.handleKey;
/**
* handles a character key event
*
* @param {string} character
* @param {Array} modifiers
* @param {Event} e
* @returns void
*/
function _handleKey(character, modifiers, e) {
var self = this;
if (!self.recording) {
_origHandleKey.apply(self, arguments);
return;
}
// remember this character if we're currently recording a sequence
if (e.type == 'keydown') {
if (character.length === 1 && _recordedCharacterKey) {
_recordCurrentCombo();
}
for (i = 0; i < modifiers.length; ++i) {
_recordKey(modifiers[i]);
}
_recordKey(character);
// once a key is released, all keys that were held down at the time
// count as a keypress
} else if (e.type == 'keyup' && _currentRecordedKeys.length > 0) {
_recordCurrentCombo();
}
}
/**
* marks a character key as held down while recording a sequence
*
* @param {string} key
* @returns void
*/
function _recordKey(key) {
var i;
// one-off implementation of Array.indexOf, since IE6-9 don't support it
for (i = 0; i < _currentRecordedKeys.length; ++i) {
if (_currentRecordedKeys[i] === key) {
return;
}
}
_currentRecordedKeys.push(key);
if (key.length === 1) {
_recordedCharacterKey = true;
}
}
/**
* marks whatever key combination that's been recorded so far as finished
* and gets ready for the next combo
*
* @returns void
*/
function _recordCurrentCombo() {
_recordedSequence.push(_currentRecordedKeys);
_currentRecordedKeys = [];
_recordedCharacterKey = false;
_restartRecordTimer();
}
/**
* ensures each combo in a sequence is in a predictable order and formats
* key combos to be '+'-delimited
*
* modifies the sequence in-place
*
* @param {Array} sequence
* @returns void
*/
function _normalizeSequence(sequence) {
var i;
for (i = 0; i < sequence.length; ++i) {
sequence[i].sort(function(x, y) {
// modifier keys always come first, in alphabetical order
if (x.length > 1 && y.length === 1) {
return -1;
} else if (x.length === 1 && y.length > 1) {
return 1;
}
// character keys come next (list should contain no duplicates,
// so no need for equality check)
return x > y ? 1 : -1;
});
sequence[i] = sequence[i].join('+');
}
}
/**
* finishes the current recording, passes the recorded sequence to the stored
* callback, and sets Mousetrap.handleKey back to its original function
*
* @returns void
*/
function _finishRecording() {
if (_recordedSequenceCallback) {
_normalizeSequence(_recordedSequence);
_recordedSequenceCallback(_recordedSequence);
}
// reset all recorded state
_recordedSequence = [];
_recordedSequenceCallback = null;
_currentRecordedKeys = [];
}
/**
* called to set a 1 second timeout on the current recording
*
* this is so after each key press in the sequence the recording will wait for
* 1 more second before executing the callback
*
* @returns void
*/
function _restartRecordTimer() {
clearTimeout(_recordTimer);
_recordTimer = setTimeout(_finishRecording, 1000);
}
/**
* records the next sequence and passes it to a callback once it's
* completed
*
* @param {Function} callback
* @returns void
*/
Mousetrap.prototype.record = function(callback) {
var self = this;
self.recording = true;
_recordedSequenceCallback = function() {
self.recording = false;
callback.apply(self, arguments);
};
};
Mousetrap.prototype.handleKey = function() {
var self = this;
_handleKey.apply(self, arguments);
};
Mousetrap.init();
})(Mousetrap);
================================================
FILE: plugins/record/tests/index.html
================================================
<!DOCTYPE html>
<html>
<head>
<title>Jelly</title>
<meta charset=utf-8>
<link href="jelly.css" rel="stylesheet">
</head>
<body>
<h1>Jelly</h1>
<h2>For testing the <strong>record</strong> extension</h2>
<p>Click "Record" to test recording a sequence.</p>
<button class="test-record">Record</button>
<div class="test-record-result"></div>
<script type="text/javascript" src="../../../tests/libs/jquery-1.7.2.min.js"></script>
<script type="text/javascript" src="../../../mousetrap.js"></script>
<script type="text/javascript" src="../mousetrap-record.js"></script>
<script type="text/javascript" src="jelly.js"></script>
<script type="text/javascript">
Jelly.spread();
</script>
</body>
</html>
================================================
FILE: plugins/record/tests/jelly.css
================================================
body {
font-family: helvetica, arial, sans-serif;
line-height: 20px;
}
kbd {
background-color: #ccc;
display: inline-block;
padding: 0.5ex 1em;
}
.test-record-result {
margin-top: 20px;
}
.test-record-result span:nth-child(n+2) {
margin-left: 10px;
}
================================================
FILE: plugins/record/tests/jelly.js
================================================
/**
* Peanut butter goes great with jelly.
*
* @author Dan Tao <daniel.tao@gmail.com>
*/
var Jelly = (function() {
var recordButton = $("button.test-record"),
recordResult = $("div.test-record-result");
function _formatSequenceAsHtml(sequence) {
var combos = [],
i;
for (i = 0; i < sequence.length; ++i) {
combos.push('<span>' + _formatKeysAsHtml(sequence[i].split('+')) + '</span>');
}
return combos.join(' ');
}
function _formatKeysAsHtml(keys) {
var htmlKeys = [],
i;
for (i = 0; i < keys.length; ++i) {
htmlKeys.push('<kbd>' + keys[i] + '</kbd>');
}
return htmlKeys.join('+');
}
function _prepareRecordTest() {
recordButton.prop('disabled', true);
recordButton.text('Recording');
Mousetrap.record(function(sequence) {
recordResult.html(_formatSequenceAsHtml(sequence));
recordButton.prop('disabled', false);
recordButton.text('Record');
});
// take focus away from the button so that Mousetrap will actually
// capture keystrokes
recordButton.blur();
}
return {
spread: function() {
recordButton.click(_prepareRecordTest);
}
};
})();
================================================
FILE: tests/libs/key-event.js
================================================
(function(window, document) {
var KeyEvent = function(data, type) {
this.keyCode = 'keyCode' in data ? data.keyCode : 0;
this.charCode = 'charCode' in data ? data.charCode : 0;
var modifiers = 'modifiers' in data ? data.modifiers : [];
this.ctrlKey = false;
this.metaKey = false;
this.altKey = false;
this.shiftKey = false;
for (var i = 0; i < modifiers.length; i++) {
this[modifiers[i] + 'Key'] = true;
}
this.type = type || 'keypress';
};
KeyEvent.prototype.toNative = function() {
var event = document.createEventObject ? document.createEventObject() : document.createEvent('Events');
if (event.initEvent) {
event.initEvent(this.type, true, true);
}
event.keyCode = this.keyCode;
event.which = this.charCode || this.keyCode;
event.shiftKey = this.shiftKey;
event.metaKey = this.metaKey;
event.altKey = this.altKey;
event.ctrlKey = this.ctrlKey;
return event;
};
KeyEvent.prototype.fire = function(element) {
var event = this.toNative();
if (element.dispatchEvent) {
element.dispatchEvent(event);
return;
}
element.fireEvent('on' + this.type, event);
};
// simulates complete key event as if the user pressed the key in the browser
// triggers a keydown, then a keypress, then a keyup
KeyEvent.simulate = function(charCode, keyCode, modifiers, element, repeat, options) {
if (modifiers === undefined) {
modifiers = [];
}
if (element === undefined) {
element = document;
}
if (repeat === undefined) {
repeat = 1;
}
if (options === undefined) {
options = {};
}
// Re-target the element so that `event.target` becomes the shadow host. See:
// https://developers.google.com/web/fundamentals/web-components/shadowdom#events
// This is a bit of a lie because true events would re-target the event target both for
// closed and open shadow trees. `KeyEvent` is not a true event and will fire the event
// directly from the shadow host for closed shadow trees. For open trees, this would make
// the tests fail as the actual event that will be eventually dispatched would have an
// incorrect `Event.composedPath()` starting with the shadow host instead of the
// initial event target.
if (options.shadowHost && options.shadowHost.shadowRoot === null) {
// closed shadow dom
element = options.shadowHost;
}
var modifierToKeyCode = {
'shift': 16,
'ctrl': 17,
'alt': 18,
'meta': 91
};
// if the key is a modifier then take it out of the regular
// keypress/keydown
if (keyCode == 16 || keyCode == 17 || keyCode == 18 || keyCode == 91) {
repeat = 0;
}
var modifiersToInclude = [];
var keyEvents = [];
// modifiers would go down first
for (var i = 0; i < modifiers.length; i++) {
modifiersToInclude.push(modifiers[i]);
keyEvents.push(new KeyEvent({
charCode: 0,
keyCode: modifierToKeyCode[modifiers[i]],
modifiers: modifiersToInclude
}, 'keydown'));
}
// @todo factor in duration for these
while (repeat > 0) {
keyEvents.push(new KeyEvent({
charCode: 0,
keyCode: keyCode,
modifiers: modifiersToInclude
}, 'keydown'));
keyEvents.push(new KeyEvent({
charCode: charCode,
keyCode: charCode,
modifiers: modifiersToInclude
}, 'keypress'));
repeat--;
}
keyEvents.push(new KeyEvent({
charCode: 0,
keyCode: keyCode,
modifiers: modifiersToInclude
}, 'keyup'));
// now lift up the modifier keys
for (i = 0; i < modifiersToInclude.length; i++) {
var modifierKeyCode = modifierToKeyCode[modifiersToInclude[i]];
modifiersToInclude.splice(i, 1);
keyEvents.push(new KeyEvent({
charCode: 0,
keyCode: modifierKeyCode,
modifiers: modifiersToInclude
}, 'keyup'));
}
for (i = 0; i < keyEvents.length; i++) {
// console.log('firing', keyEvents[i].type, keyEvents[i].keyCode, keyEvents[i].charCode);
keyEvents[i].fire(element);
}
};
window.KeyEvent = KeyEvent;
// expose as a common js module
if (typeof module !== 'undefined' && module.exports) {
module.exports = KeyEvent;
}
// expose KeyEvent as an AMD module
if (typeof define === 'function' && define.amd) {
define(function() {
return KeyEvent;
});
}
}) (typeof window !== 'undefined' ? window : null, typeof window !== 'undefined' ? document : null);
================================================
FILE: tests/mousetrap.html
================================================
<!DOCTYPE html>
<head>
<meta charset="utf-8">
<title>Mousetrap Tests</title>
<link rel="stylesheet" href="../node_modules/mocha/mocha.css">
</head>
<body>
<div id="mocha"></div>
<script src="../node_modules/chai/chai.js"></script>
<script src="../node_modules/sinon/pkg/sinon.js"></script>
<script src="../node_modules/mocha/mocha.js"></script>
<script src="../mousetrap.js"></script>
<script src="libs/key-event.js"></script>
<script>mocha.setup('bdd')</script>
<script src="test.mousetrap.js"></script>
<script>
mocha.checkLeaks();
mocha.globals(['Mousetrap']);
mocha.run();
</script>
</body>
================================================
FILE: tests/test.mousetrap.js
================================================
/**
* The following strategy of importing modules allows the tests to be run in a browser environment.
* Test libraries like `mocha`, `sinon`, etc. are expected to be loaded before this file.
*/
var sinon = sinon || require('sinon');
var chai = chai || require('chai');
var expect = chai.expect;
if (typeof window === 'undefined') {
require('mocha');
require('jsdom-global')();
}
// Load libraries that require access to the DOM after `jsdom-global`
var Mousetrap = Mousetrap || require('./../mousetrap');
var KeyEvent = KeyEvent || require('./libs/key-event');
// Reset Mousetrap after each test
afterEach(function () {
Mousetrap.reset();
});
describe('Mousetrap.bind', function () {
describe('basic', function () {
it('z key fires when pressing z', function () {
var spy = sinon.spy();
Mousetrap.bind('z', spy);
KeyEvent.simulate('Z'.charCodeAt(0), 90);
// really slow for some reason
// expect(spy).to.have.been.calledOnce;
expect(spy.callCount).to.equal(1, 'callback should fire once');
expect(spy.args[0][0]).to.be.an.instanceOf(Event, 'first argument should be Event');
expect(spy.args[0][1]).to.equal('z', 'second argument should be key combo');
});
it('z key fires from keydown', function () {
var spy = sinon.spy();
Mousetrap.bind('z', spy, 'keydown');
KeyEvent.simulate('Z'.charCodeAt(0), 90);
// really slow for some reason
// expect(spy).to.have.been.calledOnce;
expect(spy.callCount).to.equal(1, 'callback should fire once');
expect(spy.args[0][0]).to.be.an.instanceOf(Event, 'first argument should be Event');
expect(spy.args[0][1]).to.equal('z', 'second argument should be key combo');
});
it('z key does not fire when pressing b', function () {
var spy = sinon.spy();
Mousetrap.bind('z', spy);
KeyEvent.simulate('B'.charCodeAt(0), 66);
expect(spy.callCount).to.equal(0);
});
it('z key does not fire when holding a modifier key', function () {
var spy = sinon.spy();
var modifiers = ['ctrl', 'alt', 'meta', 'shift'];
var charCode;
var modifier;
Mousetrap.bind('z', spy);
for (var i = 0; i < 4; i++) {
modifier = modifiers[i];
charCode = 'Z'.charCodeAt(0);
// character code is different when alt is pressed
if (modifier == 'alt') {
charCode = 'Ω'.charCodeAt(0);
}
spy.resetHistory();
KeyEvent.simulate(charCode, 90, [modifier]);
expect(spy.callCount).to.equal(0);
}
});
it('z key does not fire when inside an input element in an open shadow dom', function() {
var spy = sinon.spy();
var shadowHost = document.createElement('div');
var shadowRoot = shadowHost.attachShadow({ mode: 'open' });
document.body.appendChild(shadowHost);
var inputElement = document.createElement('input');
shadowRoot.appendChild(inputElement);
expect(shadowHost.shadowRoot).to.equal(shadowRoot, 'shadow root accessible');
Mousetrap.bind('z', spy);
KeyEvent.simulate('Z'.charCodeAt(0), 90, [], inputElement, 1, { shadowHost: shadowHost });
document.body.removeChild(shadowHost);
expect(spy.callCount).to.equal(0, 'callback should not have fired');
});
it('z key does fire when inside an input element in a closed shadow dom', function() {
var spy = sinon.spy();
var shadowHost = document.createElement('div');
var shadowRoot = shadowHost.attachShadow({ mode: 'closed' });
document.body.appendChild(shadowHost);
var inputElement = document.createElement('input');
shadowRoot.appendChild(inputElement);
expect(shadowHost.shadowRoot).to.equal(null, 'shadow root unaccessible');
Mousetrap.bind('z', spy);
KeyEvent.simulate('Z'.charCodeAt(0), 90, [], inputElement, 1, { shadowHost: shadowHost });
document.body.removeChild(shadowHost);
expect(spy.callCount).to.equal(1, 'callback should have fired once');
});
it('keyup events should fire', function() {
var spy = sinon.spy();
Mousetrap.bind('z', spy, 'keyup');
KeyEvent.simulate('Z'.charCodeAt(0), 90);
expect(spy.callCount).to.equal(1, 'keyup event for "z" should fire');
// for key held down we should only get one key up
KeyEvent.simulate('Z'.charCodeAt(0), 90, [], document, 10);
expect(spy.callCount).to.equal(2, 'keyup event for "z" should fire once for held down key');
});
it('keyup event for 0 should fire', function () {
var spy = sinon.spy();
Mousetrap.bind('0', spy, 'keyup');
KeyEvent.simulate(0, 48);
expect(spy.callCount).to.equal(1, 'keyup event for "0" should fire');
});
it('rebinding a key overwrites the callback for that key', function () {
var spy1 = sinon.spy();
var spy2 = sinon.spy();
Mousetrap.bind('x', spy1);
Mousetrap.bind('x', spy2);
KeyEvent.simulate('X'.charCodeAt(0), 88);
expect(spy1.callCount).to.equal(0, 'original callback should not fire');
expect(spy2.callCount).to.equal(1, 'new callback should fire');
});
it('binding an array of keys', function () {
var spy = sinon.spy();
Mousetrap.bind(['a', 'b', 'c'], spy);
KeyEvent.simulate('A'.charCodeAt(0), 65);
expect(spy.callCount).to.equal(1, 'new callback was called');
expect(spy.args[0][1]).to.equal('a', 'callback should match "a"');
KeyEvent.simulate('B'.charCodeAt(0), 66);
expect(spy.callCount).to.equal(2, 'new callback was called twice');
expect(spy.args[1][1]).to.equal('b', 'callback should match "b"');
KeyEvent.simulate('C'.charCodeAt(0), 67);
expect(spy.callCount).to.equal(3, 'new callback was called three times');
expect(spy.args[2][1]).to.equal('c', 'callback should match "c"');
});
it('return false should prevent default and stop propagation', function () {
var spy = sinon.spy(function () {
return false;
});
Mousetrap.bind('command+s', spy);
KeyEvent.simulate('S'.charCodeAt(0), 83, ['meta']);
expect(spy.callCount).to.equal(1, 'callback should fire');
expect(spy.args[0][0]).to.be.an.instanceOf(Event, 'first argument should be Event');
expect(spy.args[0][0].defaultPrevented).to.be.true;
// cancelBubble is not correctly set to true in webkit/blink
//
// @see https://code.google.com/p/chromium/issues/detail?id=162270
// expect(spy.args[0][0].cancelBubble).to.be.true;
// try without return false
spy = sinon.spy();
Mousetrap.bind('command+s', spy);
KeyEvent.simulate('S'.charCodeAt(0), 83, ['meta']);
expect(spy.callCount).to.equal(1, 'callback should fire');
expect(spy.args[0][0]).to.be.an.instanceOf(Event, 'first argument should be Event');
expect(spy.args[0][0].cancelBubble).to.be.false;
expect(spy.args[0][0].defaultPrevented).to.be.false;
});
it('capslock key is ignored', function () {
var spy = sinon.spy();
Mousetrap.bind('a', spy);
KeyEvent.simulate('a'.charCodeAt(0), 65);
expect(spy.callCount).to.equal(1, 'callback should fire for lowercase a');
spy.resetHistory();
KeyEvent.simulate('A'.charCodeAt(0), 65);
expect(spy.callCount).to.equal(1, 'callback should fire for capslock A');
spy.resetHistory();
KeyEvent.simulate('A'.charCodeAt(0), 65, ['shift']);
expect(spy.callCount).to.equal(0, 'callback should not fire fort shift+a');
});
});
describe('special characters', function () {
it('binding special characters', function () {
var spy = sinon.spy();
Mousetrap.bind('*', spy);
KeyEvent.simulate('*'.charCodeAt(0), 56, ['shift']);
expect(spy.callCount).to.equal(1, 'callback should fire');
expect(spy.args[0][1]).to.equal('*', 'callback should match *');
});
it('binding special characters keyup', function () {
var spy = sinon.spy();
Mousetrap.bind('*', spy, 'keyup');
KeyEvent.simulate('*'.charCodeAt(0), 56, ['shift']);
expect(spy.callCount).to.equal(1, 'callback should fire');
expect(spy.args[0][1]).to.equal('*', 'callback should match "*"');
});
it('binding keys with no associated charCode', function () {
var spy = sinon.spy();
Mousetrap.bind('left', spy);
KeyEvent.simulate(0, 37);
expect(spy.callCount).to.equal(1, 'callback should fire');
expect(spy.args[0][1]).to.equal('left', 'callback should match "left"');
});
it('binding plus key alone should work', function () {
var spy = sinon.spy();
Mousetrap.bind('+', spy);
// fires for regular + character
KeyEvent.simulate('+'.charCodeAt(0), 43);
// and for shift+=
KeyEvent.simulate(43, 187, ['shift']);
expect(spy.callCount).to.equal(2, 'callback should fire');
expect(spy.args[0][1]).to.equal('+', 'callback should match "+"');
});
it('binding plus key as "plus" should work', function () {
var spy = sinon.spy();
Mousetrap.bind('plus', spy);
// fires for regular + character
KeyEvent.simulate('+'.charCodeAt(0), 43);
// and for shift+=
KeyEvent.simulate(43, 187, ['shift']);
expect(spy.callCount).to.equal(2, 'callback should fire');
expect(spy.args[0][1]).to.equal('plus', 'callback should match "plus"');
});
it('binding to alt++ should work', function () {
var spy = sinon.spy();
Mousetrap.bind('alt++', spy);
KeyEvent.simulate('+'.charCodeAt(0), 43, ['alt']);
expect(spy.callCount).to.equal(1, 'callback should fire');
expect(spy.args[0][1]).to.equal('alt++', 'callback should match "alt++"');
});
it('binding to alt+shift++ should work as well', function () {
var spy = sinon.spy();
Mousetrap.bind('alt+shift++', spy);
KeyEvent.simulate('+'.charCodeAt(0), 43, ['shift', 'alt']);
expect(spy.callCount).to.equal(1, 'callback should fire');
expect(spy.args[0][1]).to.equal('alt+shift++', 'callback should match "alt++"');
});
});
describe('combos with modifiers', function () {
it('binding key combinations', function () {
var spy = sinon.spy();
Mousetrap.bind('command+o', spy);
KeyEvent.simulate('O'.charCodeAt(0), 79, ['meta']);
expect(spy.callCount).to.equal(1, 'command+o callback should fire');
expect(spy.args[0][1]).to.equal('command+o', 'keyboard string returned is correct');
});
it('binding key combos with multiple modifiers', function () {
var spy = sinon.spy();
Mousetrap.bind('command+shift+o', spy);
KeyEvent.simulate('O'.charCodeAt(0), 79, ['meta']);
expect(spy.callCount).to.equal(0, 'command+o callback should not fire');
KeyEvent.simulate('O'.charCodeAt(0), 79, ['meta', 'shift']);
expect(spy.callCount).to.equal(1, 'command+o callback should fire');
});
it('should fire callback when ctrl+numpad 0 is pressed', function () {
var spy = sinon.spy();
Mousetrap.bind('ctrl+0', spy);
// numpad 0 keycode
KeyEvent.simulate(96, 96, ['ctrl']);
expect(spy.callCount).to.equal(1, 'callback should fire once');
expect(spy.args[0][0]).to.be.an.instanceOf(Event, 'first argument should be Event');
expect(spy.args[0][1]).to.equal('ctrl+0', 'second argument should be key combo');
});
});
describe('sequences', function () {
it('binding sequences', function () {
var spy = sinon.spy();
Mousetrap.bind('g i', spy);
KeyEvent.simulate('G'.charCodeAt(0), 71);
expect(spy.callCount).to.equal(0, 'callback should not fire');
KeyEvent.simulate('I'.charCodeAt(0), 73);
expect(spy.callCount).to.equal(1, 'callback should fire');
});
it('binding sequences with mixed types', function () {
var spy = sinon.spy();
Mousetrap.bind('g o enter', spy);
KeyEvent.simulate('G'.charCodeAt(0), 71);
expect(spy.callCount).to.equal(0, 'callback should not fire');
KeyEvent.simulate('O'.charCodeAt(0), 79);
expect(spy.callCount).to.equal(0, 'callback should not fire');
KeyEvent.simulate(0, 13);
expect(spy.callCount).to.equal(1, 'callback should fire');
});
it('binding sequences starting with modifier keys', function () {
var spy = sinon.spy();
Mousetrap.bind('option enter', spy);
KeyEvent.simulate(0, 18, ['alt']);
KeyEvent.simulate(0, 13);
expect(spy.callCount).to.equal(1, 'callback should fire');
spy = sinon.spy();
Mousetrap.bind('command enter', spy);
KeyEvent.simulate(0, 91, ['meta']);
KeyEvent.simulate(0, 13);
expect(spy.callCount).to.equal(1, 'callback should fire');
spy = sinon.spy();
Mousetrap.bind('escape enter', spy);
KeyEvent.simulate(0, 27);
KeyEvent.simulate(0, 13);
expect(spy.callCount).to.equal(1, 'callback should fire');
});
it('key within sequence should not fire', function () {
var spy1 = sinon.spy();
var spy2 = sinon.spy();
Mousetrap.bind('a', spy1);
Mousetrap.bind('c a t', spy2);
KeyEvent.simulate('A'.charCodeAt(0), 65);
expect(spy1.callCount).to.equal(1, 'callback 1 should fire');
spy1.resetHistory();
KeyEvent.simulate('C'.charCodeAt(0), 67);
KeyEvent.simulate('A'.charCodeAt(0), 65);
KeyEvent.simulate('T'.charCodeAt(0), 84);
expect(spy1.callCount).to.equal(0, 'callback for "a" key should not fire');
expect(spy2.callCount).to.equal(1, 'callback for "c a t" sequence should fire');
});
it('keyup at end of sequence should not fire', function () {
var spy1 = sinon.spy();
var spy2 = sinon.spy();
Mousetrap.bind('t', spy1, 'keyup');
Mousetrap.bind('b a t', spy2);
KeyEvent.simulate('B'.charCodeAt(0), 66);
KeyEvent.simulate('A'.charCodeAt(0), 65);
KeyEvent.simulate('T'.charCodeAt(0), 84);
expect(spy1.callCount).to.equal(0, 'callback for "t" keyup should not fire');
expect(spy2.callCount).to.equal(1, 'callback for "b a t" sequence should fire');
});
it('keyup sequences should work', function () {
var spy = sinon.spy();
Mousetrap.bind('b a t', spy, 'keyup');
KeyEvent.simulate('b'.charCodeAt(0), 66);
KeyEvent.simulate('a'.charCodeAt(0), 65);
// hold the last key down for a while
KeyEvent.simulate('t'.charCodeAt(0), 84, [], document, 10);
expect(spy.callCount).to.equal(1, 'callback for "b a t" sequence should fire on keyup');
});
it('extra spaces in sequences should be ignored', function () {
var spy = sinon.spy();
Mousetrap.bind('b a t', spy);
KeyEvent.simulate('b'.charCodeAt(0), 66);
KeyEvent.simulate('a'.charCodeAt(0), 65);
KeyEvent.simulate('t'.charCodeAt(0), 84);
expect(spy.callCount).to.equal(1, 'callback for "b a t" sequence should fire');
});
it('modifiers and sequences play nicely', function () {
var spy1 = sinon.spy();
var spy2 = sinon.spy();
Mousetrap.bind('ctrl a', spy1);
Mousetrap.bind('ctrl+b', spy2);
KeyEvent.simulate(0, 17, ['ctrl']);
KeyEvent.simulate('A'.charCodeAt(0), 65);
expect(spy1.callCount).to.equal(1, '"ctrl a" should fire');
KeyEvent.simulate('B'.charCodeAt(0), 66, ['ctrl']);
expect(spy2.callCount).to.equal(1, '"ctrl+b" should fire');
});
it('sequences that start the same work', function () {
var spy1 = sinon.spy();
var spy2 = sinon.spy();
Mousetrap.bind('g g l', spy2);
Mousetrap.bind('g g o', spy1);
KeyEvent.simulate('g'.charCodeAt(0), 71);
KeyEvent.simulate('g'.charCodeAt(0), 71);
KeyEvent.simulate('o'.charCodeAt(0), 79);
expect(spy1.callCount).to.equal(1, '"g g o" should fire');
expect(spy2.callCount).to.equal(0, '"g g l" should not fire');
spy1.resetHistory();
spy2.resetHistory();
KeyEvent.simulate('g'.charCodeAt(0), 71);
KeyEvent.simulate('g'.charCodeAt(0), 71);
KeyEvent.simulate('l'.charCodeAt(0), 76);
expect(spy1.callCount).to.equal(0, '"g g o" should not fire');
expect(spy2.callCount).to.equal(1, '"g g l" should fire');
});
it('sequences should not fire subsequences', function () {
var spy1 = sinon.spy();
var spy2 = sinon.spy();
Mousetrap.bind('a b c', spy1);
Mousetrap.bind('b c', spy2);
KeyEvent.simulate('A'.charCodeAt(0), 65);
KeyEvent.simulate('B'.charCodeAt(0), 66);
KeyEvent.simulate('C'.charCodeAt(0), 67);
expect(spy1.callCount).to.equal(1, '"a b c" should fire');
expect(spy2.callCount).to.equal(0, '"b c" should not fire');
spy1.resetHistory();
spy2.resetHistory();
Mousetrap.bind('option b', spy1);
Mousetrap.bind('a option b', spy2);
KeyEvent.simulate('A'.charCodeAt(0), 65);
KeyEvent.simulate(0, 18, ['alt']);
KeyEvent.simulate('B'.charCodeAt(0), 66);
expect(spy1.callCount).to.equal(0, '"option b" should not fire');
expect(spy2.callCount).to.equal(1, '"a option b" should fire');
});
it('rebinding same sequence should override previous', function () {
var spy1 = sinon.spy();
var spy2 = sinon.spy();
Mousetrap.bind('a b c', spy1);
Mousetrap.bind('a b c', spy2);
KeyEvent.simulate('a'.charCodeAt(0), 65);
KeyEvent.simulate('b'.charCodeAt(0), 66);
KeyEvent.simulate('c'.charCodeAt(0), 67);
expect(spy1.callCount).to.equal(0, 'first callback should not fire');
expect(spy2.callCount).to.equal(1, 'second callback should fire');
});
it('broken sequences', function () {
var spy = sinon.spy();
Mousetrap.bind('h a t', spy);
KeyEvent.simulate('h'.charCodeAt(0), 72);
KeyEvent.simulate('e'.charCodeAt(0), 69);
KeyEvent.simulate('a'.charCodeAt(0), 65);
KeyEvent.simulate('r'.charCodeAt(0), 82);
KeyEvent.simulate('t'.charCodeAt(0), 84);
expect(spy.callCount).to.equal(0, 'sequence for "h a t" should not fire for "h e a r t"');
});
it('sequences containing combos should work', function () {
var spy = sinon.spy();
Mousetrap.bind('a ctrl+b', spy);
KeyEvent.simulate('a'.charCodeAt(0), 65);
KeyEvent.simulate('B'.charCodeAt(0), 66, ['ctrl']);
expect(spy.callCount).to.equal(1, '"a ctrl+b" should fire');
Mousetrap.unbind('a ctrl+b');
spy = sinon.spy();
Mousetrap.bind('ctrl+b a', spy);
KeyEvent.simulate('b'.charCodeAt(0), 66, ['ctrl']);
KeyEvent.simulate('a'.charCodeAt(0), 65);
expect(spy.callCount).to.equal(1, '"ctrl+b a" should fire');
});
it('sequences starting with spacebar should work', function () {
var spy = sinon.spy();
Mousetrap.bind('a space b c', spy);
KeyEvent.simulate('a'.charCodeAt(0), 65);
KeyEvent.simulate(32, 32);
KeyEvent.simulate('b'.charCodeAt(0), 66);
KeyEvent.simulate('c'.charCodeAt(0), 67);
expect(spy.callCount).to.equal(1, '"a space b c" should fire');
});
it('konami code', function () {
var spy = sinon.spy();
Mousetrap.bind('up up down down left right left right b a enter', spy);
KeyEvent.simulate(0, 38);
KeyEvent.simulate(0, 38);
KeyEvent.simulate(0, 40);
KeyEvent.simulate(0, 40);
KeyEvent.simulate(0, 37);
KeyEvent.simulate(0, 39);
KeyEvent.simulate(0, 37);
KeyEvent.simulate(0, 39);
KeyEvent.simulate('b'.charCodeAt(0), 66);
KeyEvent.simulate('a'.charCodeAt(0), 65);
KeyEvent.simulate(0, 13);
expect(spy.callCount).to.equal(1, 'konami code should fire');
});
it('sequence timer resets', function () {
var spy = sinon.spy();
var clock = sinon.useFakeTimers();
Mousetrap.bind('h a t', spy);
KeyEvent.simulate('h'.charCodeAt(0), 72);
clock.tick(600);
KeyEvent.simulate('a'.charCodeAt(0), 65);
clock.tick(900);
KeyEvent.simulate('t'.charCodeAt(0), 84);
expect(spy.callCount).to.equal(1, 'sequence should fire after waiting');
clock.restore();
});
it('sequences timeout', function () {
var spy = sinon.spy();
var clock = sinon.useFakeTimers();
Mousetrap.bind('g t', spy);
KeyEvent.simulate('g'.charCodeAt(0), 71);
clock.tick(1000);
KeyEvent.simulate('t'.charCodeAt(0), 84);
expect(spy.callCount).to.equal(0, 'sequence callback should not fire');
clock.restore();
});
});
describe('default actions', function () {
var keys = {
keypress: [
['a', 65],
['A', 65, ['shift']],
['7', 55],
['?', 191],
['*', 56],
['+', 187],
['$', 52],
['[', 219],
['.', 190]
],
keydown: [
['shift+\'', 222, ['shift']],
['shift+a', 65, ['shift']],
['shift+5', 53, ['shift']],
['command+shift+p', 80, ['meta', 'shift']],
['space', 32],
['left', 37]
]
};
function getCallback(key, keyCode, type, modifiers) {
return function () {
var spy = sinon.spy();
Mousetrap.bind(key, spy);
KeyEvent.simulate(key.charCodeAt(0), keyCode, modifiers);
expect(spy.callCount).to.equal(1);
expect(spy.args[0][0].type).to.equal(type);
};
}
for (var type in keys) {
for (var i = 0; i < keys[type].length; i++) {
var key = keys[type][i][0];
var keyCode = keys[type][i][1];
var modifiers = keys[type][i][2] || [];
it('"' + key + '" uses "' + type + '"', getCallback(key, keyCode, type, modifiers));
}
}
});
});
describe('Mousetrap.unbind', function () {
it('unbind works', function () {
var spy = sinon.spy();
Mousetrap.bind('a', spy);
KeyEvent.simulate('a'.charCodeAt(0), 65);
expect(spy.callCount).to.equal(1, 'callback for a should fire');
Mousetrap.unbind('a');
KeyEvent.simulate('a'.charCodeAt(0), 65);
expect(spy.callCount).to.equal(1, 'callback for a should not fire after unbind');
});
it('unbind accepts an array', function () {
var spy = sinon.spy();
Mousetrap.bind(['a', 'b', 'c'], spy);
KeyEvent.simulate('a'.charCodeAt(0), 65);
KeyEvent.simulate('b'.charCodeAt(0), 66);
KeyEvent.simulate('c'.charCodeAt(0), 67);
expect(spy.callCount).to.equal(3, 'callback should have fired 3 times');
Mousetrap.unbind(['a', 'b', 'c']);
KeyEvent.simulate('a'.charCodeAt(0), 65);
KeyEvent.simulate('b'.charCodeAt(0), 66);
KeyEvent.simulate('c'.charCodeAt(0), 67);
expect(spy.callCount).to.equal(3, 'callback should not fire after unbind');
});
});
describe('wrapping a specific element', function () {
// Prepare the DOM for these tests.
document.body.insertAdjacentHTML('afterbegin', `
<form style="display: none;">
<textarea></textarea>
</form>
`);
var form = document.querySelector('form');
var textarea = form.querySelector('textarea');
it('z key fires when pressing z in the target element', function () {
var spy = sinon.spy();
Mousetrap(form).bind('z', spy);
KeyEvent.simulate('Z'.charCodeAt(0), 90, [], form);
expect(spy.callCount).to.equal(1, 'callback should fire once');
expect(spy.args[0][0]).to.be.an.instanceOf(Event, 'first argument should be Event');
expect(spy.args[0][1]).to.equal('z', 'second argument should be key combo');
});
it('z key fires when pressing z in a child of the target element', function () {
var spy = sinon.spy();
Mousetrap(form).bind('z', spy);
KeyEvent.simulate('Z'.charCodeAt(0), 90, [], textarea);
expect(spy.callCount).to.equal(1, 'callback should fire once');
expect(spy.args[0][0]).to.be.an.instanceOf(Event, 'first argument should be Event');
expect(spy.args[0][1]).to.equal('z', 'second argument should be key combo');
});
it('z key does not fire when pressing z outside the target element', function () {
var spy = sinon.spy();
Mousetrap(textarea).bind('z', spy);
KeyEvent.simulate('Z'.charCodeAt(0), 90);
expect(spy.callCount).to.equal(0, 'callback should not have fired');
});
it('should work when constructing a new mousetrap object', function () {
var spy = sinon.spy();
var mousetrap = new Mousetrap(form);
mousetrap.bind('a', spy);
KeyEvent.simulate('a'.charCodeAt(0), 65, [], textarea);
expect(spy.callCount).to.equal(1, 'callback should fire once');
expect(spy.args[0][0]).to.be.an.instanceOf(Event, 'first argument should be Event');
expect(spy.args[0][1]).to.equal('a', 'second argument should be key combo');
});
it('should allow you to create an empty mousetrap constructor', function () {
var spy = sinon.spy();
var mousetrap = new Mousetrap();
mousetrap.bind('a', spy);
KeyEvent.simulate('a'.charCodeAt(0), 65);
expect(spy.callCount).to.equal(1, 'callback should fire once');
expect(spy.args[0][0]).to.be.an.instanceOf(Event, 'first argument should be Event');
expect(spy.args[0][1]).to.equal('a', 'second argument should be key combo');
});
});
describe('Mouestrap.addKeycodes', function () {
it('should properly recognize non-default mapping', function () {
const spy = sinon.spy();
Mousetrap.addKeycodes({
144: 'num',
});
Mousetrap.bind('num', spy);
KeyEvent.simulate(144, 144);
expect(spy.callCount).to.equal(1, 'callback should fire for num');
spy.resetHistory();
});
});
gitextract_1hmozx3b/
├── .gitignore
├── Gruntfile.js
├── LICENSE
├── README.md
├── mousetrap.js
├── mousetrap.sublime-project
├── package.json
├── plugins/
│ ├── README.md
│ ├── bind-dictionary/
│ │ ├── README.md
│ │ └── mousetrap-bind-dictionary.js
│ ├── global-bind/
│ │ ├── README.md
│ │ └── mousetrap-global-bind.js
│ ├── pause/
│ │ ├── README.md
│ │ └── mousetrap-pause.js
│ └── record/
│ ├── README.md
│ ├── mousetrap-record.js
│ └── tests/
│ ├── index.html
│ ├── jelly.css
│ └── jelly.js
└── tests/
├── libs/
│ └── key-event.js
├── mousetrap.html
└── test.mousetrap.js
SYMBOL INDEX (23 symbols across 4 files)
FILE: mousetrap.js
function _addEvent (line 176) | function _addEvent(object, type, callback) {
function _characterFromEvent (line 191) | function _characterFromEvent(e) {
function _modifiersMatch (line 237) | function _modifiersMatch(modifiers1, modifiers2) {
function _eventModifiers (line 247) | function _eventModifiers(e) {
function _preventDefault (line 275) | function _preventDefault(e) {
function _stopPropagation (line 290) | function _stopPropagation(e) {
function _isModifier (line 305) | function _isModifier(key) {
function _getReverseMap (line 315) | function _getReverseMap() {
function _pickBestAction (line 341) | function _pickBestAction(key, modifiers, action) {
function _keysFromString (line 364) | function _keysFromString(combination) {
function _getKeyInfo (line 380) | function _getKeyInfo(combination, action) {
function _belongsTo (line 423) | function _belongsTo(element, ancestor) {
function Mousetrap (line 435) | function Mousetrap(targetElement) {
FILE: plugins/record/mousetrap-record.js
function _handleKey (line 59) | function _handleKey(character, modifiers, e) {
function _recordKey (line 91) | function _recordKey(key) {
function _recordCurrentCombo (line 114) | function _recordCurrentCombo() {
function _normalizeSequence (line 130) | function _normalizeSequence(sequence) {
function _finishRecording (line 157) | function _finishRecording() {
function _restartRecordTimer (line 177) | function _restartRecordTimer() {
FILE: plugins/record/tests/jelly.js
function _formatSequenceAsHtml (line 10) | function _formatSequenceAsHtml(sequence) {
function _formatKeysAsHtml (line 21) | function _formatKeysAsHtml(keys) {
function _prepareRecordTest (line 32) | function _prepareRecordTest() {
FILE: tests/test.mousetrap.js
function getCallback (line 635) | function getCallback(key, keyCode, type, modifiers) {
Condensed preview — 22 files, each showing path, character count, and a content snippet. Download the .json file or copy for the full structured content (99K chars).
[
{
"path": ".gitignore",
"chars": 33,
"preview": "node_modules\n*.sublime-workspace\n"
},
{
"path": "Gruntfile.js",
"chars": 793,
"preview": "/*jshint node:true */\nmodule.exports = function(grunt) {\n 'use strict';\n\n grunt.initConfig({\n pkg: grunt.fi"
},
{
"path": "LICENSE",
"chars": 10602,
"preview": "\n Apache License\n Version 2.0, January 2004\n htt"
},
{
"path": "README.md",
"chars": 3260,
"preview": "# Mousetrap\n[](https://cdnjs.com/libraries/mousetrap)\n\nMousetrap i"
},
{
"path": "mousetrap.js",
"chars": 33853,
"preview": "/*global define:false */\n/**\n * Copyright 2012-2017 Craig Campbell\n *\n * Licensed under the Apache License, Version 2.0 "
},
{
"path": "mousetrap.sublime-project",
"chars": 488,
"preview": "{\n \"settings\":\n {\n \"detect_indentation\": true,\n \"ensure_newline_at_eof_on_save\": true,\n \"tab_"
},
{
"path": "package.json",
"chars": 801,
"preview": "{\n \"name\": \"mousetrap\",\n \"version\": \"1.6.5\",\n \"description\": \"Simple library for handling keyboard shortcuts\",\n \"mai"
},
{
"path": "plugins/README.md",
"chars": 563,
"preview": "# Plugins\n\nPlugins extend the functionality of Mousetrap. To use a plugin just include the plugin after mousetrap.\n\n```"
},
{
"path": "plugins/bind-dictionary/README.md",
"chars": 431,
"preview": "# Bind Dictionary\n\nThis extension overwrites the default bind behavior and allows you to bind multiple combinations in a"
},
{
"path": "plugins/bind-dictionary/mousetrap-bind-dictionary.js",
"chars": 1000,
"preview": "/**\n * Overwrites default Mousetrap.bind method to optionally accept\n * an object to bind multiple key events in a singl"
},
{
"path": "plugins/global-bind/README.md",
"chars": 552,
"preview": "# Global Bind\n\nThis extension allows you to specify keyboard events that will work anywhere including inside textarea/in"
},
{
"path": "plugins/global-bind/mousetrap-global-bind.js",
"chars": 1217,
"preview": "/**\n * adds a bindGlobal method to Mousetrap that allows you to\n * bind specific keyboard shortcuts that will still work"
},
{
"path": "plugins/pause/README.md",
"chars": 291,
"preview": "# Pause/unpause\n\nThis extension allows Mousetrap to be paused and unpaused without having to reset keyboard shortcuts an"
},
{
"path": "plugins/pause/mousetrap-pause.js",
"chars": 779,
"preview": "/**\n * adds a pause and unpause method to Mousetrap\n * this allows you to enable or disable keyboard shortcuts\n * withou"
},
{
"path": "plugins/record/README.md",
"chars": 391,
"preview": "# Record\n\nThis extension lets you use Mousetrap to record keyboard sequences and play them back:\n\n```html\n<button onclic"
},
{
"path": "plugins/record/mousetrap-record.js",
"chars": 5515,
"preview": "/**\n * This extension allows you to record a sequence using Mousetrap.\n *\n * @author Dan Tao <daniel.tao@gmail.com>\n */\n"
},
{
"path": "plugins/record/tests/index.html",
"chars": 840,
"preview": "<!DOCTYPE html>\n<html>\n\n <head>\n <title>Jelly</title>\n <meta charset=utf-8>\n <link href=\"jelly.c"
},
{
"path": "plugins/record/tests/jelly.css",
"chars": 280,
"preview": "body {\n font-family: helvetica, arial, sans-serif;\n line-height: 20px;\n}\n\nkbd {\n background-color: #ccc;\n di"
},
{
"path": "plugins/record/tests/jelly.js",
"chars": 1329,
"preview": "/**\n * Peanut butter goes great with jelly.\n *\n * @author Dan Tao <daniel.tao@gmail.com>\n */\nvar Jelly = (function() {\n "
},
{
"path": "tests/libs/key-event.js",
"chars": 5187,
"preview": "(function(window, document) {\n var KeyEvent = function(data, type) {\n this.keyCode = 'keyCode' in data ? data."
},
{
"path": "tests/mousetrap.html",
"chars": 674,
"preview": "<!DOCTYPE html>\n<head>\n <meta charset=\"utf-8\">\n <title>Mousetrap Tests</title>\n <link rel=\"stylesheet\" href=\".."
},
{
"path": "tests/test.mousetrap.js",
"chars": 25654,
"preview": "/**\n * The following strategy of importing modules allows the tests to be run in a browser environment.\n * Test librarie"
}
]
About this extraction
This page contains the full source code of the ccampbell/mousetrap GitHub repository, extracted and formatted as plain text for AI agents and large language models (LLMs). The extraction includes 22 files (92.3 KB), approximately 23.3k tokens, and a symbol index with 23 extracted functions, classes, methods, constants, and types. Use this with OpenClaw, Claude, ChatGPT, Cursor, Windsurf, or any other AI tool that accepts text input. You can copy the full output to your clipboard or download it as a .txt file.
Extracted by GitExtract — free GitHub repo to text converter for AI. Built by Nikandr Surkov.