-
Notifications
You must be signed in to change notification settings - Fork 1
/
vanilla.typesequence.js
119 lines (100 loc) · 2.71 KB
/
vanilla.typesequence.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
/* By Arjan Eising, 2012. Licence: MIT. See <https://github.com/arjaneising/TypeSequence> */
;(function(window) {
if (typeof window.typesequence != 'undefined') return;
function typesequence(settings) {
var elm = settings.elm || window,
once = settings.once || false,
callback = settings.callback || false,
sequence = settings.sequence || [],
executed = false,
codedSequence = [],
goingWell = 0;
if (sequence.length === 0 || !callback) return;
if (elm.addEventListener) {
elm.addEventListener('keydown', testForSequence, false);
}
else if (window.attachEvent) {
elm.attachEvent('onkeydown', testForSequence);
}
for (var i = 0, l = sequence.length, key, codedKey = null; i < l; ++i) {
key = sequence[i];
if (cache[key]) {
codedKey = cache[key];
}
else {
if (typeof key == 'number') codedKey = key;
else if (/^[0-9]$/.test(key)) codedKey = parseInt(key, 10) + 48;
else if (/^F[0-9]{1,2}$/.test(key)) codedKey = parseInt(key.substring(1), 10) + 111;
else if (/^[a-zA-Z]$/.test(key)) codedKey = parseInt(key, 36) + 55;
else if (keycodes[key]) codedKey = keycodes[key];
if (!codedKey) continue;
cache[key] = codedKey;
}
if (codedKey) {
codedSequence.push(codedKey);
codedKey = null;
}
}
if (settings.enter) {
codedSequence.push(13);
}
function testForSequence(e) {
if (settings.once && executed) {
return;
}
var which = e.which || window.event.keyCode;
if (which == codedSequence[goingWell]) {
++goingWell;
}
else {
goingWell = 0;
}
if (goingWell == codedSequence.length) {
goingWell = 0;
if (settings.once == 'depends') {
executed = callback(elm);
}
else {
executed = true;
callback(elm);
}
}
}
}
var cache = {},
keycodes = {
'BACKSPACE': 8,
'TAB': 9,
'ENTER': 13,
'↵': 13,
'SHIFT': 16,
'⇧': 16,
'CONTROL': 17,
'ALT': 18,
'CAPSLOCK': 20,
'ESC': 27,
'SPACE': 32,
' ': 32,
'PAGEUP': 33,
'PAGEDOWN': 34,
'END': 35,
'HOME': 36,
'LEFT': 37,
'←': 37,
'⇐': 37,
'UP': 38,
'↑': 38,
'⇑': 38,
'RIGHT': 39,
'→': 39,
'⇒': 39,
'DOWN': 40,
'↓': 40,
'⇓': 40,
'INSERT': 45,
'DELETE': 46,
'DEL': 46,
'NUMLOCK': 144
};
window.typesequence = typesequence;
})(window);