forked from smfoote/kkjjhlhlba
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkkjjhlhlba.js
372 lines (338 loc) · 11.1 KB
/
kkjjhlhlba.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
function Kkjjhlhlba() {
var keyCodes = {
'8': 'delete',
'9': 'tab',
'13': 'enter',
'16': 'shift',
'20': 'capslock',
'27': 'esc',
'32': 'space',
'37': 'left',
'38': 'up',
'39': 'right',
'40': 'down',
'48': '0',
'49': '1',
'50': '2',
'51': '3',
'52': '4',
'53': '5',
'54': '6',
'55': '7',
'56': '8',
'57': '9',
'65': 'a',
'66': 'b',
'67': 'c',
'68': 'd',
'69': 'e',
'70': 'f',
'71': 'g',
'72': 'h',
'73': 'i',
'74': 'j',
'75': 'k',
'76': 'l',
'77': 'm',
'78': 'n',
'79': 'o',
'80': 'p',
'81': 'q',
'82': 'r',
'83': 's',
'84': 't',
'85': 'u',
'86': 'v',
'87': 'w',
'88': 'x',
'89': 'y',
'90': 'z',
'112': 'f1',
'113': 'f2',
'114': 'f3',
'115': 'f4',
'116': 'f5',
'117': 'f6',
'118': 'f7',
'119': 'f8',
'120': 'f9',
'121': 'f10',
'122': 'f11',
'123': 'f12',
'186': ';',
'187': '=',
'189': '-',
'192': '`',
'219': '[',
'220': '\\',
'221': ']',
'222': '\'',
'188': ',',
'190': '.',
'191': '/',
// Mac specific
'17': 'ctrl',
'18': 'opt',
'91': 'lcommand',
'93': 'rcommand',
// uppercase for non letters
'1048': ')',
'1049': '!',
'1050': '@',
'1051': '#',
'1052': '$',
'1053': '%',
'1054': '^',
'1055': '&',
'1056': '*',
'1057': '(',
'1186': ':',
'1187': '+',
'1189': '_',
'1192': '~',
'1219': '{',
'1219': '|',
'1221': '}',
'1188': '<',
'1190': '>',
'1191': '?'
};
// {Object} containing all shortcuts generated by the developer using kkjjhlhlba.start().
// Each shortcut is an object that looks like this:
// 'k,k,j,j,h,l,h,l,b,a': {
// 'description': 'Konami code',
// 'method': function() {
// // Your code goes here
// }
// }
// The function in 'method' can be replaced by a URL string, in which case the window will be
// redirected to the given URL using window.location
var shortcuts;
// The current sequence of keys that have been entered by the user. This variable is cleared
// out each time a function is exectued, or the keyboard is idle for 3/4 of a second.
var currentCode = '';
// This array is used to track the keys that are currently down. Keys are removed from this
// array on keyup.
var pressedKeys = [];
// To make 'lcommand+shift+f' (or any sequence containing a '+') possible, we need to check
// if the same value is being pressed twice, or if the key is still down. This probably needs
// a better solution, so you can hold down a single key and make an action repeat.
var previousVal = '';
// Used to check if the shift key is being held down. If it is, we convert other keys to
// thier uppercase version.
var isShiftDown = false;
// Used with isShiftDown to convert non-letter keys to uppercase
var capitalKeyOffset = 1000;
// Used to clear out currentCode after an extended period of idle time.
var inputTimer;
// Utility function for cross browser events
var addEventListener = function (el, evt, callback) {
if (document.addEventListener) {
addEventListener = function(el, evt, callback) {
el.addEventListener(evt, callback, false);
};
} else if (document.attachEvent) {
addEventListener = function(el, evt, callback) {
el.attachEvent('on' + evt, callback);
};
} else {
addEventListener = function () {
return false;
};
}
addEventListener(el, evt, callback);
};
// Utility function to prevent default cross browser
var preventDefault = function (e) {
if (e.preventDefault) {
preventDefault = function(e) {
e.preventDefault();
};
} else {
preventDefault = function(e) {
e.returnValue = false;
};
}
};
// Util function to get key vals cross browser
function getKeycode(e) {
e = (window.event) ? window.event : e;
key = e.which ? e.which : e.keyCode;
return key;
}
// Run on each keydown.
function handleKeyDown (e) {
var key, val, shortcut, method;
var keydisplay = document.getElementById("key-display");
var activeElementTag = document.activeElement.tagName.toUpperCase();
// Do nothing if the focus is in an input or textarea.
// TODO: check if the focus is in input[type="text"]. all the others are probably okay
if (activeElementTag === 'INPUT' || activeElementTag === 'TEXTAREA') {
return;
}
// Convert keycode into the corresponding key's value
key = getKeycode(e);
val = keyCodes[key];
if(val === "shift") {
isShiftDown = true;
} else {
if(isShiftDown) {
// If shift is pressed, convert the pressed key to uppercase, or the shifted val
val = keyCodes[key+capitalKeyOffset] || val.toUpperCase();
}
// Build the string of key sequences
if (currentCode === '') {
// The first key pressed in the sequence
currentCode = val;
} else if (pressedKeys.length) {
// If more than one key is currently pressed, and the current value is not the same
// as the previous value
if (val !== previousVal) {
currentCode += '+' + val;
}
} else {
// If no other keys are currently pressed, separate with commas.
currentCode += ',' + val;
}
// Add the currently pressed key to the pressedKeys array, if it is not the same key as
// the previously pressed key.
if (val !== previousVal) {
pressedKeys.push(val);
}
previousVal = val;
// Display the current code in the page [for presentation purposes only - remove on
// Nov. 29, 2012]
keydisplay.className = document.getElementById("key-display").className.replace( /(?:^|\s)hidden(?!\S)/g , '' );
keydisplay.innerHTML = currentCode;
}
}
// Run on each key up
function handleKeyUp (e) {
var key, val, shortcut, method;
var keydisplay = document.getElementById("key-display");
var activeElementTag = document.activeElement.tagName.toUpperCase();
// Do nothing if focus is in an input or textarea
if (activeElementTag === 'INPUT' || activeElementTag === 'TEXTAREA') {
return;
}
// Convert keycode to corresponding key's value
key = getKeycode(e);
val = keyCodes[key];
// Update isShiftDown flag
if(val === "shift") {
isShiftDown = false;
}
// Remove current key from pressedKeys array
pressedKeys.splice(pressedKeys.indexOf(val), 1);
// Reset timer
window.clearTimeout(inputTimer);
// Check if the currentCode is a member of the shortcuts object
shortcut = shortcuts[currentCode];
if (shortcut && shortcut.method) {
// If the shortcut object is found, and it has a 'method' attribute, run the method.
method = shortcut.method;
// TODO: regex to make sure the string is a valid URL
if (typeof method === 'string') {
// Redirect to the URL if 'method' is a string.
window.location = method;
} else {
if (shortcut.preventDefault) {
// Prevent default, if necessary
preventDefault();
}
// call the method, passing the event as an argument
method.call(this, [e]);
// Reset the currentCode
currentCode = '';
// And hide the keydisplay [remove after Nov. 29, 2012]
window.setTimeout(function() {
if (keydisplay.className.search('hidden') < 0) {
keydisplay.className += ("hidden");
}
}, 750);
}
} else {
// If no shortcut is found, start a new timer.
inputTimer = window.setTimeout(function() {
// After 750 ms (3/4 of a second) inactivity, reset the currentCode
currentCode = '';
// And hide the keydisplay [remove after Nov. 29, 2012]
if (keydisplay.className.search('hidden') < 0) {
keydisplay.className += ("hidden");
}
}, 750);
}
}
// Loop through the shortcuts object and create a cheatsheet with all of shortcuts
function createCheatSheet(shortcuts) {
// This element is appended the first time kkjjhlhlba.js is initialized.
var container = document.getElementById('keyboard-shortcuts');
var cheatsheet = '<h2>Keyboard Shortcuts</h2><ul class="shortcuts">';
var shortcut, closeButton;
for (var method in shortcuts) {
if (shortcuts.hasOwnProperty(method)) {
shortcut = shortcuts[method];
cheatsheet += '<li class="shortcut">' + method + ' <span class="description">' + shortcut.description + '<span></li>';
}
}
cheatsheet += '</ul><button type="button" id="close-cheatsheet">Close</button></div>';
// Put created HTML into the container
container.innerHTML = cheatsheet;
// Add event listener on the close button.
closeButton = document.getElementById('close-cheatsheet');
addEventListener(closeButton, 'click', function() {
container.className = container.className.replace(/\s*active/, '');
});
}
// Show the cheatsheet by adding the class "active"
function showCheatsheet() {
var cheatsheet = document.getElementById('keyboard-shortcuts');
if (!cheatsheet.className.indexOf('active') > -1) {
cheatsheet.className += ' active';
}
}
return {
// The only public method: start
// Used to add new keyboard shortcuts to the page. Start will merge the new shortcuts
// object with the existing shortcuts object. New instantiations will overwrite in the case
// of a conflict.
start: function(config) {
var method;
var cheatsheet;
var keydisplay;
if (!shortcuts) {
// If the shortcuts object doesn't exist yet, no mergin is necessary.
shortcuts = config.shortcuts;
// Add the showCheatsheet shortcut
shortcuts['?'] = {
method: showCheatsheet,
description: 'Show this keyboard shortcuts cheatsheet'
};
// Set up the event listeners
addEventListener(document, 'keydown', handleKeyDown, false);
addEventListener(document, 'keyup', handleKeyUp, false);
// Create the cheatsheet container, and append it to the end of the <body>
cheatsheet = document.createElement('div');
cheatsheet.id = 'keyboard-shortcuts';
document.body.appendChild(cheatsheet);
// Create the keydisplay container, and append it to the end of the <body>
// TODO: remove this after Nov. 29, 2012
keydisplay = document.createElement('div');
keydisplay.id = 'key-display';
keydisplay.className += 'hidden';
document.body.appendChild(keydisplay);
} else {
// If the shortcuts object already exists, merge
for (method in config.shortcuts) {
if (config.shortcuts.hasOwnProperty(method)) {
shortcuts[method] = config.shortcuts[method];
}
}
}
// Recreate cheatsheet each time start is run, so the final cheatsheet is accurate.
createCheatSheet(shortcuts);
}
};
}
// Create single instance of Kkjjhlhlba.
var kkjjhlhlba = new Kkjjhlhlba();