Skip to content
Browse files

Merge branch 'cleanup'

  • Loading branch information...
2 parents f236b95 + 144f9e9 commit 2d76fd0572043f8b3d80dcdf5cb7f33899ea81b5 @ccampbell committed Jul 11, 2012
Showing with 25 additions and 24 deletions.
  1. +25 −24 mousetrap.js
View
49 mousetrap.js
@@ -16,7 +16,7 @@
* Mousetrap is a simple keyboard shortcut library for Javascript with
* no external dependencies
*
- * @preserve @version 1.1.1
+ * @version 1.1.1
* @url craig.is/killing/mice
*/
window.Mousetrap = (function() {
@@ -88,7 +88,7 @@ window.Mousetrap = (function() {
*
* this is so you can use keyup events with these keys
*
- * note that this will only work on US keyboards
+ * note that this will only work reliably on US keyboards
*
* @type {Object}
*/
@@ -191,8 +191,8 @@ window.Mousetrap = (function() {
/**
* loop through to map numbers on the numeric keypad
*/
- for (i = 96; i < 106; ++i) {
- _MAP[i] = i - 96;
+ for (i = 0; i <= 9; ++i) {
+ _MAP[i + 96] = i;
}
/**
@@ -212,7 +212,7 @@ window.Mousetrap = (function() {
}
/**
- * takes the event and returns the keycode
+ * takes the event and returns the key character
*
* @param {Event} e
* @return {string}
@@ -276,14 +276,15 @@ window.Mousetrap = (function() {
function _resetSequences(do_not_reset) {
do_not_reset = do_not_reset || {};
- var active_sequences = false;
+ var active_sequences = false,
+ key;
- for (var key in _sequence_levels) {
- if (!do_not_reset[key]) {
- _sequence_levels[key] = 0;
+ for (key in _sequence_levels) {
+ if (do_not_reset[key]) {
+ active_sequences = true;
continue;
}
- active_sequences = true;
+ _sequence_levels[key] = 0;
}
if (!active_sequences) {
@@ -337,7 +338,7 @@ window.Mousetrap = (function() {
// if this is a keypress event that means that we need to only
// look at the character, otherwise check the modifiers as
// well
- if (action === 'keypress' || _modifiersMatch(modifiers, callback.modifiers)) {
+ if (action == 'keypress' || _modifiersMatch(modifiers, callback.modifiers)) {
// remove is used so if you change your mind and call bind a
// second time with a new function the first one is overwritten
@@ -390,7 +391,7 @@ window.Mousetrap = (function() {
* @param {Event}
* @returns void
*/
- function _callback(callback, e) {
+ function _fireCallback(callback, e) {
if (callback(e) === false) {
if (e.preventDefault) {
e.preventDefault();
@@ -406,13 +407,13 @@ window.Mousetrap = (function() {
}
/**
- * fires a callback for a matching keycode
+ * handles a character key event
*
* @param {string} character
* @param {Event} e
* @returns void
*/
- function _fireCallback(character, e) {
+ function _handleCharacter(character, e) {
// if this event should not happen stop here
if (_stop(e)) {
@@ -437,14 +438,14 @@ window.Mousetrap = (function() {
// keep a list of which sequences were matches for later
do_not_reset[callbacks[i]['seq']] = 1;
- _callback(callbacks[i].callback, e);
+ _fireCallback(callbacks[i].callback, e);
continue;
}
// if there were no sequence matches but we are still here
- // that means this is a regular match so we should fire then break
+ // that means this is a regular match so we should fire that
if (!processed_sequence_callback && !_inside_sequence) {
- _callback(callbacks[i].callback, e);
+ _fireCallback(callbacks[i].callback, e);
}
}
@@ -464,7 +465,7 @@ window.Mousetrap = (function() {
*/
function _handleKey(e) {
- // add which for key events
+ // normalize e.which for key events
// @see http://stackoverflow.com/questions/4285627/javascript-keycode-vs-charcode-utter-confusion
e.which = typeof e.which == "number" ? e.which : e.keyCode;
@@ -475,12 +476,12 @@ window.Mousetrap = (function() {
return;
}
- if (e.type === 'keyup' && _ignore_next_keyup === character) {
+ if (e.type == 'keyup' && _ignore_next_keyup == character) {
_ignore_next_keyup = false;
return;
}
- _fireCallback(character, e);
+ _handleCharacter(character, e);
}
/**
@@ -501,7 +502,7 @@ window.Mousetrap = (function() {
*
* @returns void
*/
- function _resetSequence() {
+ function _resetSequenceTimer() {
clearTimeout(_reset_timer);
_reset_timer = setTimeout(_resetSequences, 1000);
}
@@ -548,7 +549,7 @@ window.Mousetrap = (function() {
// modifier keys don't work as expected with keypress,
// switch to keydown
- if (action === 'keypress' && modifiers.length) {
+ if (action == 'keypress' && modifiers.length) {
action = 'keydown';
}
@@ -586,7 +587,7 @@ window.Mousetrap = (function() {
var _increaseSequence = function(e) {
_inside_sequence = action;
++_sequence_levels[combo];
- _resetSequence();
+ _resetSequenceTimer();
},
/**
@@ -597,7 +598,7 @@ window.Mousetrap = (function() {
* @returns void
*/
_callbackAndReset = function(e) {
- _callback(callback, e);
+ _fireCallback(callback, e);
// we should ignore the next key up if the action is key down
// or keypress. this is so if you finish a sequence and

0 comments on commit 2d76fd0

Please sign in to comment.
Something went wrong with that request. Please try again.