-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathjquery.keycombinator.js
391 lines (346 loc) · 11.6 KB
/
jquery.keycombinator.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
// The plugin logic hasn't been nested in a jQuery plugin. Instead, we just use
// jQuery for its instantiation.
;(function($, window, document, undefined){
// our plugin constructor
var KeyCombinator = function( elem, options ){
this.elem = elem;
this.$elem = $(elem);
this.options = options;
// This next line takes advantage of HTML5 data attributes
// to support customization of the plugin on a per-element
// basis. For example,
// <div class=item' data-plugin-options='{"message":"Goodbye World!"}'></div>
this.metadata = this.$elem.data( 'plugin-options' );
var cached = this.$elem.data('keycombinator-config');
// Introduce defaults that can be extended either
// globally or using an object literal.
this.config = $.extend( {},
this.defaults,
cached,
this.options,
this.metadata
);
this.defaultCombo = this.config.defaultCombos;
this.onComplete = this.config.onComplete;
this.comboData = new ComboData();
this.completed = false;
this.keydowns = 0;
this.keyups = 0;
};
// ====================== globals ===========================
////// Private
////var _modProps = { 16: 'shiftKey', 17: 'ctrlKey', 18: 'altKey', 91: 'metaKey' };
var keys = {
// Shift key, ⇧
// '⇧': 16, shift: 16,
16: {mac: '⇧', win: 'Shift', unix: 'Shift'},
// CTRL key, on Mac: ⌃
17: {mac: '⌃', win: 'Ctrl', unix: 'Ctrl'},
// ALT key, on Mac: ⌥ (Alt)
18: {mac: '⌥', win: 'Alt', unix: 'Alt'},
// META, on Mac: ⌘ (CMD), on Windows (Win), on Linux (Super)
91: {mac: '⌘', win: 'Win', unix: 'Super'},
224: {mac: '⌘', win: 'Win', unix: 'Super'}, // FF
// Backspace key, on Mac: ⌫ (Backspace)
8: {mac: '⌫', win: 'Backspace', unix: 'Backspace'},
// Tab Key, on Mac: ⇥ (Tab), on Windows ⇆
9: {mac: '⇥', win: 'Tab', unix: 'Tab'},
// Return key, ↩
13: {mac: '↩', win: 'Enter', unix: 'Enter'},
// Pause/Break key
19: {all: 'Pause'},
// Caps Lock key, ⇪
20: {mac: '⇪', win: 'Caps Lock', unix: 'Caps Lock'},
// Escape key, on Mac: ⎋, on Windows: Esc
27: {mac: '⎋', win: 'Esc', unix: 'Esc'},
// Space key
32: {all: 'Space'},
// Page-Up key, or pgup, on Mac: ↖
33: {mac: '↖', win: 'Page Up', unix: 'Page Up'},
// Page-Down key, or pgdown, on Mac: ↘
34: {mac: '↘', win: 'Page Down', unix: 'Page Down'},
// END key, on Mac: ⇟
35: {mac: '⇟', win: 'End', unix: 'End'},
// HOME key, on Mac: ⇞
36: {mac: '⇞', win: 'Home', unix: 'Home'},
// Insert key, or ins
45: {all: 'Ins'},
// Delete key, on Mac: ⌫ (Delete)
46: {mac: '⌫ ', win: 'Del', unix: 'Del'},
// Left Arrow Key, or ←
37: {mac: '←', win: 'Left Arrow', unix: 'Left Arrow'},
// Up Arrow Key, or ↑
38: {mac: '↑', win: 'Up Arrow', unix: 'Up Arrow'},
// Right Arrow Key, or →
39: {mac: '→', win: 'Right Arrow', unix: 'Right Arrow'},
// Up Arrow Key, or ↓
40: {mac: '↓', win: 'Down Arrow', unix: 'Down Arrow'},
// odities, printing characters that come out wrong:
// Num-Multiply, or *
106: {all: '*'},
// Num-Plus or +
107: {all: '+'},
// Num-Subtract, or -
109: {all: '-'},
// Semi-colon, or ;
59: {all: ';'}, // firefox
186: {all: ';'}, // IE & chrome
// = or equals
61: {all: '='}, // FF
187: {all: '='}, // IE & Chrome
// Comma, or ,
188: {all: ','},
// Dash, or -
109: {all: '-'}, // FF
189: {all: '-'}, // IE & Chrome
// Period, or ., or full-stop
190: {all: '.'},
// Slash, or /, or forward-slash
191: {all: '/'},
// Tick, or `, or back-quote
192: {all: '`'},
// Open bracket, or [
219: {all: '['},
// Back slash, or \
220: {all: '\\'},
// Close backet, or ]
221: {all: ']'},
// Apostraphe, or Quote, or '
222: {all: '\''}
}
// To minimise code bloat, add all of the NUMPAD 0-9 keys in a loop
i = 95, n = 0;
while(++i < 106) {
// keys['num-' + n] = i;
keys[i] = {all: 'Num-' + n};
++n;
}
// To minimise code bloat, add all of the top row 0-9 keys in a loop
i = 47, n = 0;
while(++i < 58) {
// _keys.keys[n] = i;
keys[i] = {all: n};
++n;
}
// To minimise code bloat, add all of the F1-F25 keys in a loop
i = 111, n = 1;
while(++i < 136) {
// _keys.keys['f' + n] = i;
keys[i] = {all: 'F' + n};
++n;
}
// To minimise code bloat, add all of the letters of the alphabet in a loop
var i = 64;
while(++i < 91) {
// _keys.keys[String.fromCharCode(i).toLowerCase()] = i;
keys[i] = {all: String.fromCharCode(i).toUpperCase()};
}
var accents = {
'¨': 85, // U
'´': 69, // E
'`': 192, // `
'ˆ': 73, // I
'˜': 78 // N
};
function getKeyChar(keyCode){
if (key = keys[keyCode]){
if (key.all != undefined){return key.all;}
else{ return key[platform]; }
}
////// MOD aka toggleable keys
////mods: {
////// Shift key, ⇧
////// '⇧': 16, shift: 16,
////16: '⇧': shift: ,
////// CTRL key, on Mac: ⌃
////'⌃': 17, ctrl: 17,
////// ALT key, on Mac: ⌥ (Alt)
////'⌥': 18, alt: 18, option: 18,
////// META, on Mac: ⌘ (CMD), on Windows (Win), on Linux (Super)
////'⌘': 91, meta: 91, cmd: 91, 'super': 91, win: 91
////},
}
var delimiter = '+';
var shift_sign = 'shift';
var meta_sign = 'super';
var ctrl_sign = 'ctrl';
var alt_sign = 'alt';
var platform;
var rawPlatform = navigator.platform.toLowerCase();
if (rawPlatform.indexOf('mac') >= 0){
platform = 'mac';
delimiter = '';
}
else if (rawPlatform.indexOf('win') >= 0){
platform = 'win';
}
else{
platform = 'unix';
}
var modifiers = ['ctrlKey', 'altKey', 'shiftKey', 'metaKey'];
var modKeyCodes = [17, 18, 16, 91, 224];
function isModifier(keyCode){
return ($.inArray(keyCode, modKeyCodes) >= 0)
}
function ComboPart(keyCode){
if (keyCode !== undefined){
this.keyCode = keyCode;
this.keyChar = getKeyChar(keyCode);
}
}
function ComboData(){
this.comboParts= [];
this.ctrlKey = false;
this.altKey = false;
this.metaKey = false;
this.shiftKey = false;
this.comboString = '';
}
// if 'key' is passed in, it will be used as a key to match objects' uniqueness
function set_insert(array, value, key){
var alreadyPresent = false;
if (key === undefined){
if ($.inArray(value, array) >= 0){ alreadyPresent = true; }
}
else if ($.grep(array,
function(obj){ return obj[key] == value[key]; }).length){
alreadyPresent = true;
}
if (!alreadyPresent){ array.push(value); }
}
var loopingTimer = {
run: function(task, interval, duration){
if (!loopingTimer.startTime){
loopingTimer.startTime = (new Date().getTime());
loopingTimer.running = setInterval(function(){
loopingTimer.run(task, interval, duration);
}, interval);
}
else if (((new Date()).getTime() - loopingTimer.startTime) < duration){
task();
}
else {
loopingTimer.stop();
}
},
running: null,
stop: function(){
clearInterval(loopingTimer.running);
loopingTimer.startTime = null;
},
startTime: null
}
// ============ /"globals" ============================================
KeyCombinator.prototype = {
defaults: {
},
eval_key_event: function(e, $textbox, callback){
// e.stopPropagation();
// e.preventDefault();
var comboData = this.comboData;
loopingTimer.stop();
var startComboLength = comboData.comboString.length;
if (getKeyChar(e.keyCode) != undefined){
set_insert(comboData.comboParts, new ComboPart(e.keyCode), 'keyCode');
}
if (e.metaKey){ comboData.metaKey = true; }
if (e.ctrlKey){ comboData.ctrlKey = true; }
if (e.altKey){ comboData.altKey = true; }
if (e.shiftKey){ comboData.shiftKey = true; }
comboData.comboString = $.map(comboData.comboParts, function(comboPart, i){
return comboPart.keyChar;
}).join(delimiter);
if (comboData.comboString.length > startComboLength){
$textbox.blur(); // needed for FF mac accent key hack
$textbox.val(comboData.comboString);
$textbox.focus();
if (!isModifier(e.keyCode)){
$textbox.select();
this.completed = true;
this.keyups = 0;
this.keydowns = 0;
loopingTimer.stop();
if(callback){ callback(comboData); }
this.comboData = new ComboData();
}
}
else if (this.keyups == this.keydowns){ this.reset($textbox); }
},
reset: function($textbox){
$textbox.val('');
this.completed = false;
this.comboData = new ComboData();
loopingTimer.stop();
this.keydowns = 0;
this.keyups = 0;
},
clear: function(){
this.reset(this.$elem);
if(this.onComplete){ this.onComplete(this.comboData); }
},
revertToDefault: function(){
var defaultCombo = this.defaultCombo;
this.reset(this.$elem);
for (var i = 0; i < defaultCombo[platform].length; i++){
var comboPart = new ComboPart();
comboPart.keyChar = defaultCombo[platform][i];
set_insert(this.comboData.comboParts, comboPart, 'keyChar');
}
this.comboData.comboString = defaultCombo[platform].join(delimiter);
this.$elem.val(defaultCombo[platform].join(delimiter));
if(this.onComplete){ this.onComplete(this.comboData); }
this.comboData = new ComboData();
},
init: function(){
// attach configuration to element for later retrieval
this.$elem.data('keycombinator-config', this.config);
var onComplete = this.onComplete;
var $elem = this.$elem;
var self = this;
$elem.keydown(function(e){
self.completed = false;
self.keydowns += 1;
var $textbox = $elem;
self.eval_key_event(e, $textbox, onComplete);
if (e.keyCode == 18 && self.comboData.comboString == getKeyChar(18)){
var $textbox = $elem;
loopingTimer.run(function(){
var contents = $textbox.val();
if (undetected_key = accents[contents[contents.length - 1]]){
loopingTimer.stop();
self.eval_key_event(new $.Event('keydown', {keyCode: undetected_key}),
$textbox,
onComplete);
}
}, 10, 10000);
}
return false;
});
$elem.keyup(function(e){
if (!self.completed){
self.keyups += 1;
self.eval_key_event(e, $elem, onComplete);
}
return false;
});
$elem.click(function(e){ $elem.select(); });
}
}
// ================= /KeyCombinator.prototype ==================
KeyCombinator.defaults = KeyCombinator.prototype.defaults;
$.fn.makeKeyCombinator = function(options) {
return this.each(function() {
new KeyCombinator(this, options).init();
});
};
$.fn.clearKeyCombinator = function(){
return this.each(function(){
new KeyCombinator(this).clear();
});
}
$.fn.defaultKeyCombinator = function(){
return this.each(function(){
new KeyCombinator(this).revertToDefault();
});
}
})(jQuery, window, document);