-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathkeycode.js
More file actions
301 lines (275 loc) · 8.48 KB
/
keycode.js
File metadata and controls
301 lines (275 loc) · 8.48 KB
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
(function() {
/**
* Library to normalize key codes across browsers. This works with keydown
* events; keypress events are not fired for all keys, and the codes are
* different for them. It returns an object with the following fields:
* { int code, bool shift, bool alt, bool ctrl }. The normalized keycodes
* obey the following rules:
*
* For alphabetic characters, the ASCII code of the uppercase version
*
* For codes that are identical across all browsers (this includes all
* modifiers, esc, delete, arrows, etc.), the common keycode
*
* For numeric keypad keys, the value returned by numkey().
* (Usually 96 + the number)
*
* For symbols, the ASCII code of the character that appears when shift
* is not held down, EXCEPT for '" => 222 (conflicts with right-arrow/pagedown),
* .> => 190 (conflicts with Delete) and `~ => 126 (conflicts with Num0).
*
* Basic usage:
* document.onkeydown = function(e) {
* do_something_with(KeyCode.translateEvent(e)
* };
*
* The naming conventions for functions use 'code' to represent an integer
* keycode, 'key' to represent a key description (specified above), and 'e'
* to represent an event object.
*
* There's also functionality to track and detect which keys are currently
* being held down: install 'key_up' and 'key_down' on their respective event
* handlers, and then check with 'is_down'.
*
* @fileoverview
* @author Jonathan Tang
* @version 0.9
* @license BSD
*/
var modifiers = ['ctrl', 'alt', 'shift'],
KEY_MAP = {},
shifted_symbols = {
58: 59, // : -> ;
43: 61, // = -> +
60: 44, // < -> ,
95: 45, // _ -> -
62: 46, // > -> .
63: 47, // ? -> /
96: 192, // ` -> ~
124: 92, // | -> \
39: 222, // ' -> 222
34: 222, // " -> 222
33: 49, // ! -> 1
64: 50, // @ -> 2
35: 51, // # -> 3
36: 52, // $ -> 4
37: 53, // % -> 5
94: 54, // ^ -> 6
38: 55, // & -> 7
42: 56, // * -> 8
40: 57, // ( -> 9
41: 58, // ) -> 0
123: 91, // { -> [
125: 93 // } -> ]
};
function isLower(ascii) {
return ascii >= 97 && ascii <= 122;
};
function capitalize(str) {
return str.substr(0,1).toUpperCase() + str.substr(1).toLowerCase();
};
var is_gecko = navigator.userAgent.indexOf('Gecko') != -1,
is_ie = navigator.userAgent.indexOf('MSIE') != -1,
is_windows = navigator.platform.indexOf('Win') != -1,
is_opera = window.opera && window.opera.version() < '9.5',
is_konqueror = navigator.vendor && navigator.vendor.indexOf('KDE') != -1,
is_icab = navigator.vendor && navigator.vendor.indexOf('iCab') != -1;
var GECKO_IE_KEYMAP = {
186: 59, // ;: in IE
187: 61, // =+ in IE
188: 44, // ,<
109: 95, // -_ in Mozilla
107: 61, // =+ in Mozilla
189: 95, // -_ in IE
190: 62, // .>
191: 47, // /?
192: 126, // `~
219: 91, // {[
220: 92, // \|
221: 93 // }]
};
var OPERA_KEYMAP = {};
// Browser detection taken from quirksmode.org
if(is_opera && is_windows) {
KEY_MAP = OPERA_KEYMAP;
} else if(is_opera || is_konqueror || is_icab) {
var unshift = [33, 64, 35, 36, 37, 94, 38, 42, 40, 41,
58, 43, 60, 95, 62, 63, 124, 34];
KEY_MAP = OPERA_KEYMAP;
for(var i = 0; i < unshift.length; ++i) {
KEY_MAP[unshift[i]] = shifted_symbols[unshift[i]];
}
} else {
// IE and Gecko are close enough that we can use the same map for both,
// and the rest of the world (eg. Opera 9.50) seems to be standardizing
// on them
KEY_MAP = GECKO_IE_KEYMAP;
}
if(is_konqueror) {
KEY_MAP[0] = 45;
KEY_MAP[127] = 46;
KEY_MAP[45] = 95;
}
var key_names = {
32: 'SPACE',
13: 'ENTER',
9: 'TAB',
8: 'BACKSPACE',
16: 'SHIFT',
17: 'CTRL',
18: 'ALT',
20: 'CAPS_LOCK',
144: 'NUM_LOCK',
145: 'SCROLL_LOCK',
37: 'LEFT',
38: 'UP',
39: 'RIGHT',
40: 'DOWN',
33: 'PAGE_UP',
34: 'PAGE_DOWN',
36: 'HOME',
35: 'END',
45: 'INSERT',
46: 'DELETE',
27: 'ESCAPE',
19: 'PAUSE',
222: "'"
};
function fn_name(code) {
if(code >= 112 && code <= 123) return 'F' + (code - 111);
return false;
};
function num_name(code) {
if(code >= 96 && code < 106) return 'Num' + (code - 96);
switch(code) {
case 106: return 'Num*';
case 111: return 'Num/';
case 110: return 'Num.';
default: return false;
}
};
var current_keys = {
codes: {},
ctrl: false,
alt: false,
shift: false
};
function update_current_modifiers(key) {
current_keys.ctrl = key.ctrl;
current_keys.alt = key.alt;
current_keys.shift = key.shift;
};
function same_modifiers(key1, key2) {
return key1.ctrl === key2.ctrl
&& key1.alt === key2.alt
&& key1.shift === key2.shift;
};
if(typeof window.KeyCode != "undefined") {
var _KeyCode = window.KeyCode;
}
var KeyCode = window.KeyCode = {
no_conflict: function() {
window.KeyCode = _KeyCode;
return KeyCode;
},
/** Generates a function key code from a number between 1 and 12 */
fkey: function(num) { return 111 + num; },
/**
* Generates a numeric keypad code from a number between 0 and 9.
* Also works for (some) arithmetic operators. The mappings are:
*
* *: 106, /: 111, .: 110
*
* + and - are not supported because the keycodes generated by Mozilla
* conflict with the non-keypad codes. The same applies to all the
* arithmetic keypad keys on Konqueror and early Opera.
*/
numkey: function(num) {
switch(num) {
case '*': return 106;
case '/': return 111;
case '.': return 110;
default: return 96 + num;
}
},
/** Generates a key code from the ASCII code of (the first character of) a
* string.
*/
key: function(str) {
var c = str.charCodeAt(0);
if(isLower(c)) return c - 32;
return shifted_symbols[c] || c;
},
/** Checks if two key objects are equal. */
key_equals: function(key1, key2) {
return key1.code == key2.code && same_modifiers(key1, key2);
},
/** Translates a keycode to its normalized value. */
translate_key_code: function(code) {
return KEY_MAP[code] || code;
},
/** Translates a keyDown event to a normalized key event object. The
* object has the following fields:
* { int code; boolean shift, boolean alt, boolean ctrl }
*/
translate_event: function(e) {
e = e || window.event;
var code = e.which || e.keyCode;
return {
code: KeyCode.translate_key_code(code),
shift: e.shiftKey,
alt: e.altKey,
ctrl: e.ctrlKey
};
},
/**
* Keydown event listener to update internal state of which keys are
* currently pressed.
*/
key_down: function(e) {
var key = KeyCode.translate_event(e);
current_keys.codes[key.code] = key.code;
update_current_modifiers(key);
},
/**
* Keyup event listener to update internal state.
*/
key_up: function(e) {
var key = KeyCode.translate_event(e);
delete current_keys.codes[key.code];
update_current_modifiers(key);
},
/**
* Returns true if the key spec (as returned by translate_event) is
* currently held down.
*/
is_down: function(key) {
var code = key.code;
if(code == KeyCode.CTRL) return current_keys.ctrl;
if(code == KeyCode.ALT) return current_keys.alt;
if(code == KeyCode.SHIFT) return current_keys.shift;
return current_keys.codes[code] !== undefined
&& same_modifiers(key, current_keys);
},
/** Returns a string representation of a key event suitable for the
* shortcut.js or JQuery HotKeys plugins. Also makes a decent UI display.
*/
hot_key: function(key) {
var pieces = [];
for(var i = 0; i < modifiers.length; ++i) {
var modifier = modifiers[i];
if(key[modifier] && modifier.toUpperCase() != key_names[key.code]) {
pieces.push(capitalize(modifier));
}
}
var c = key.code;
var key_name = key_names[c] || fn_name(c) || num_name(c) || String.fromCharCode(c);
pieces.push(capitalize(key_name))
return pieces.join('+');
}
};
// Add key constants
for(var code in key_names) {
KeyCode[key_names[code]] = code;
}
})();