| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
(function(Mousetrap) { |
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
var _recordedSequence = [], |
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
_recordedSequenceCallback = null, |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
_currentRecordedKeys = [], |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
_recordedCharacterKey = false, |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
_recordTimer = null, |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
_origHandleKey = Mousetrap.prototype.handleKey; |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
function _handleKey(character, modifiers, e) { |
| 60 |
var self = this; |
| 61 |
|
| 62 |
if (!self.recording) { |
| 63 |
_origHandleKey.apply(self, arguments); |
| 64 |
return; |
| 65 |
} |
| 66 |
|
| 67 |
|
| 68 |
if (e.type == 'keydown') { |
| 69 |
if (character.length === 1 && _recordedCharacterKey) { |
| 70 |
_recordCurrentCombo(); |
| 71 |
} |
| 72 |
|
| 73 |
for (i = 0; i < modifiers.length; ++i) { |
| 74 |
_recordKey(modifiers[i]); |
| 75 |
} |
| 76 |
_recordKey(character); |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
} else if (e.type == 'keyup' && _currentRecordedKeys.length > 0) { |
| 81 |
_recordCurrentCombo(); |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
function _recordKey(key) { |
| 92 |
var i; |
| 93 |
|
| 94 |
|
| 95 |
for (i = 0; i < _currentRecordedKeys.length; ++i) { |
| 96 |
if (_currentRecordedKeys[i] === key) { |
| 97 |
return; |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
_currentRecordedKeys.push(key); |
| 102 |
|
| 103 |
if (key.length === 1) { |
| 104 |
_recordedCharacterKey = true; |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
function _recordCurrentCombo() { |
| 115 |
_recordedSequence.push(_currentRecordedKeys); |
| 116 |
_currentRecordedKeys = []; |
| 117 |
_recordedCharacterKey = false; |
| 118 |
_restartRecordTimer(); |
| 119 |
} |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
function _normalizeSequence(sequence) { |
| 131 |
var i; |
| 132 |
|
| 133 |
for (i = 0; i < sequence.length; ++i) { |
| 134 |
sequence[i].sort(function(x, y) { |
| 135 |
|
| 136 |
if (x.length > 1 && y.length === 1) { |
| 137 |
return -1; |
| 138 |
} else if (x.length === 1 && y.length > 1) { |
| 139 |
return 1; |
| 140 |
} |
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
return x > y ? 1 : -1; |
| 145 |
}); |
| 146 |
|
| 147 |
sequence[i] = sequence[i].join('+'); |
| 148 |
} |
| 149 |
} |
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
function _finishRecording() { |
| 158 |
if (_recordedSequenceCallback) { |
| 159 |
_normalizeSequence(_recordedSequence); |
| 160 |
_recordedSequenceCallback(_recordedSequence); |
| 161 |
} |
| 162 |
|
| 163 |
|
| 164 |
_recordedSequence = []; |
| 165 |
_recordedSequenceCallback = null; |
| 166 |
_currentRecordedKeys = []; |
| 167 |
} |
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
function _restartRecordTimer() { |
| 178 |
clearTimeout(_recordTimer); |
| 179 |
_recordTimer = setTimeout(_finishRecording, 1000); |
| 180 |
} |
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
Mousetrap.prototype.record = function(callback) { |
| 190 |
var self = this; |
| 191 |
self.recording = true; |
| 192 |
_recordedSequenceCallback = function() { |
| 193 |
self.recording = false; |
| 194 |
callback.apply(self, arguments); |
| 195 |
}; |
| 196 |
}; |
| 197 |
|
| 198 |
Mousetrap.prototype.handleKey = function() { |
| 199 |
var self = this; |
| 200 |
_handleKey.apply(self, arguments); |
| 201 |
}; |
| 202 |
|
| 203 |
Mousetrap.init(); |
| 204 |
|
| 205 |
})(Mousetrap); |