Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
SeleniumHQ
GitHub Repository: SeleniumHQ/Selenium
Path: blob/trunk/third_party/closure/goog/events/keycodes.js
4102 views
1
/**
2
* @license
3
* Copyright The Closure Library Authors.
4
* SPDX-License-Identifier: Apache-2.0
5
*/
6
7
/**
8
* @fileoverview Constant declarations for common key codes.
9
*
10
* @see ../demos/keyhandler.html
11
*/
12
13
goog.provide('goog.events.KeyCodes');
14
15
goog.require('goog.userAgent');
16
goog.requireType('goog.events.BrowserEvent');
17
18
19
/**
20
* Key codes for common characters.
21
*
22
* This list is not localized and therefore some of the key codes are not
23
* correct for non US keyboard layouts. See comments below.
24
*
25
* @enum {number}
26
*/
27
goog.events.KeyCodes = {
28
WIN_KEY_FF_LINUX: 0,
29
MAC_ENTER: 3,
30
BACKSPACE: 8,
31
TAB: 9,
32
NUM_CENTER: 12, // NUMLOCK on FF/Safari Mac
33
ENTER: 13,
34
SHIFT: 16,
35
CTRL: 17,
36
ALT: 18,
37
PAUSE: 19,
38
CAPS_LOCK: 20,
39
ESC: 27,
40
SPACE: 32,
41
PAGE_UP: 33, // also NUM_NORTH_EAST
42
PAGE_DOWN: 34, // also NUM_SOUTH_EAST
43
END: 35, // also NUM_SOUTH_WEST
44
HOME: 36, // also NUM_NORTH_WEST
45
LEFT: 37, // also NUM_WEST
46
UP: 38, // also NUM_NORTH
47
RIGHT: 39, // also NUM_EAST
48
DOWN: 40, // also NUM_SOUTH
49
PLUS_SIGN: 43, // NOT numpad plus
50
PRINT_SCREEN: 44,
51
INSERT: 45, // also NUM_INSERT
52
DELETE: 46, // also NUM_DELETE
53
ZERO: 48,
54
ONE: 49,
55
TWO: 50,
56
THREE: 51,
57
FOUR: 52,
58
FIVE: 53,
59
SIX: 54,
60
SEVEN: 55,
61
EIGHT: 56,
62
NINE: 57,
63
FF_SEMICOLON: 59, // Firefox (Gecko) fires this for semicolon instead of 186
64
FF_EQUALS: 61, // Firefox (Gecko) fires this for equals instead of 187
65
FF_DASH: 173, // Firefox (Gecko) fires this for dash instead of 189
66
// Firefox (Gecko) fires this for # on UK keyboards, rather than
67
// Shift+SINGLE_QUOTE.
68
FF_HASH: 163,
69
// Firefox (Gecko) fires this for ' (:) on JP keyboards, rather than
70
// SINGLE_QUOTE (US keyboard layout) or SEMICOLON (JP keyboard layout in
71
// chrome)
72
FF_JP_QUOTE: 58,
73
QUESTION_MARK: 63, // needs localization
74
AT_SIGN: 64,
75
A: 65,
76
B: 66,
77
C: 67,
78
D: 68,
79
E: 69,
80
F: 70,
81
G: 71,
82
H: 72,
83
I: 73,
84
J: 74,
85
K: 75,
86
L: 76,
87
M: 77,
88
N: 78,
89
O: 79,
90
P: 80,
91
Q: 81,
92
R: 82,
93
S: 83,
94
T: 84,
95
U: 85,
96
V: 86,
97
W: 87,
98
X: 88,
99
Y: 89,
100
Z: 90,
101
META: 91, // WIN_KEY_LEFT
102
WIN_KEY_RIGHT: 92,
103
CONTEXT_MENU: 93,
104
NUM_ZERO: 96,
105
NUM_ONE: 97,
106
NUM_TWO: 98,
107
NUM_THREE: 99,
108
NUM_FOUR: 100,
109
NUM_FIVE: 101,
110
NUM_SIX: 102,
111
NUM_SEVEN: 103,
112
NUM_EIGHT: 104,
113
NUM_NINE: 105,
114
NUM_MULTIPLY: 106,
115
NUM_PLUS: 107,
116
NUM_MINUS: 109,
117
NUM_PERIOD: 110,
118
NUM_DIVISION: 111,
119
F1: 112,
120
F2: 113,
121
F3: 114,
122
F4: 115,
123
F5: 116,
124
F6: 117,
125
F7: 118,
126
F8: 119,
127
F9: 120,
128
F10: 121,
129
F11: 122,
130
F12: 123,
131
NUMLOCK: 144,
132
SCROLL_LOCK: 145,
133
134
// OS-specific media keys like volume controls and browser controls.
135
FIRST_MEDIA_KEY: 166,
136
LAST_MEDIA_KEY: 183,
137
138
SEMICOLON: 186, // needs localization
139
DASH: 189, // needs localization
140
EQUALS: 187, // needs localization
141
COMMA: 188, // needs localization
142
PERIOD: 190, // needs localization
143
SLASH: 191, // needs localization
144
APOSTROPHE: 192, // needs localization
145
TILDE: 192, // needs localization
146
SINGLE_QUOTE: 222, // needs localization
147
OPEN_SQUARE_BRACKET: 219, // needs localization
148
BACKSLASH: 220, // needs localization
149
CLOSE_SQUARE_BRACKET: 221, // needs localization
150
WIN_KEY: 224,
151
MAC_FF_META:
152
224, // Firefox (Gecko) fires this for the meta key instead of 91
153
MAC_WK_CMD_LEFT: 91, // WebKit Left Command key fired, same as META
154
MAC_WK_CMD_RIGHT: 93, // WebKit Right Command key fired, different from META
155
WIN_IME: 229,
156
157
// "Reserved for future use". Some programs (e.g. the SlingPlayer 2.4 ActiveX
158
// control) fire this as a hacky way to disable screensavers.
159
VK_NONAME: 252,
160
161
// We've seen users whose machines fire this keycode at regular one
162
// second intervals. The common thread among these users is that
163
// they're all using Dell Inspiron laptops, so we suspect that this
164
// indicates a hardware/bios problem.
165
// http://en.community.dell.com/support-forums/laptop/f/3518/p/19285957/19523128.aspx
166
PHANTOM: 255
167
};
168
169
170
/**
171
* Returns false if the event does not contain a text modifying key.
172
*
173
* When it returns true, the event might be text modifying. It is infeasible to
174
* say for sure because of the many different keyboard layouts, so this method
175
* errs on the side of assuming a key event is text-modifiable if we cannot be
176
* certain it is not. As an example, it will return true for ctrl+a, though in
177
* many standard keyboard layouts that key combination would mean "select all",
178
* and not actually modify the text.
179
*
180
* @param {goog.events.BrowserEvent} e A key event.
181
* @return {boolean} Whether it's a text modifying key.
182
*/
183
goog.events.KeyCodes.isTextModifyingKeyEvent = function(e) {
184
'use strict';
185
if (e.altKey && !e.ctrlKey || e.metaKey ||
186
// Function keys don't generate text
187
e.keyCode >= goog.events.KeyCodes.F1 &&
188
e.keyCode <= goog.events.KeyCodes.F12) {
189
return false;
190
}
191
192
if (goog.events.KeyCodes.isCharacterKey(e.keyCode)) {
193
return true;
194
}
195
196
switch (e.keyCode) {
197
// The following keys are quite harmless, even in combination with
198
// CTRL, ALT or SHIFT.
199
case goog.events.KeyCodes.ALT:
200
case goog.events.KeyCodes.CAPS_LOCK:
201
case goog.events.KeyCodes.CONTEXT_MENU:
202
case goog.events.KeyCodes.CTRL:
203
case goog.events.KeyCodes.DOWN:
204
case goog.events.KeyCodes.END:
205
case goog.events.KeyCodes.ESC:
206
case goog.events.KeyCodes.HOME:
207
case goog.events.KeyCodes.INSERT:
208
case goog.events.KeyCodes.LEFT:
209
case goog.events.KeyCodes.MAC_FF_META:
210
case goog.events.KeyCodes.META:
211
case goog.events.KeyCodes.NUMLOCK:
212
case goog.events.KeyCodes.NUM_CENTER:
213
case goog.events.KeyCodes.PAGE_DOWN:
214
case goog.events.KeyCodes.PAGE_UP:
215
case goog.events.KeyCodes.PAUSE:
216
case goog.events.KeyCodes.PHANTOM:
217
case goog.events.KeyCodes.PRINT_SCREEN:
218
case goog.events.KeyCodes.RIGHT:
219
case goog.events.KeyCodes.SCROLL_LOCK:
220
case goog.events.KeyCodes.SHIFT:
221
case goog.events.KeyCodes.UP:
222
case goog.events.KeyCodes.VK_NONAME:
223
case goog.events.KeyCodes.WIN_KEY:
224
case goog.events.KeyCodes.WIN_KEY_RIGHT:
225
return false;
226
case goog.events.KeyCodes.WIN_KEY_FF_LINUX:
227
return !goog.userAgent.GECKO;
228
default:
229
return e.keyCode < goog.events.KeyCodes.FIRST_MEDIA_KEY ||
230
e.keyCode > goog.events.KeyCodes.LAST_MEDIA_KEY;
231
}
232
};
233
234
235
/**
236
* Returns true if the key fires a keypress event in the current browser.
237
*
238
* Accoridng to MSDN [1] IE only fires keypress events for the following keys:
239
* - Letters: A - Z (uppercase and lowercase)
240
* - Numerals: 0 - 9
241
* - Symbols: ! @ # $ % ^ & * ( ) _ - + = < [ ] { } , . / ? \ | ' ` " ~
242
* - System: ESC, SPACEBAR, ENTER
243
*
244
* That's not entirely correct though, for instance there's no distinction
245
* between upper and lower case letters.
246
*
247
* [1] http://msdn2.microsoft.com/en-us/library/ms536939(VS.85).aspx)
248
*
249
* Safari is similar to IE, but does not fire keypress for ESC.
250
*
251
* Additionally, IE6 does not fire keydown or keypress events for letters when
252
* the control or alt keys are held down and the shift key is not. IE7 does
253
* fire keydown in these cases, though, but not keypress.
254
*
255
* @param {number} keyCode A key code.
256
* @param {number=} opt_heldKeyCode Key code of a currently-held key.
257
* @param {boolean=} opt_shiftKey Whether the shift key is held down.
258
* @param {boolean=} opt_ctrlKey Whether the control key is held down.
259
* @param {boolean=} opt_altKey Whether the alt key is held down.
260
* @param {boolean=} opt_metaKey Whether the meta key is held down.
261
* @return {boolean} Whether it's a key that fires a keypress event.
262
*/
263
goog.events.KeyCodes.firesKeyPressEvent = function(
264
keyCode, opt_heldKeyCode, opt_shiftKey, opt_ctrlKey, opt_altKey,
265
opt_metaKey) {
266
'use strict';
267
if (goog.userAgent.MAC && opt_altKey) {
268
return goog.events.KeyCodes.isCharacterKey(keyCode);
269
}
270
271
// Alt but not AltGr which is represented as Alt+Ctrl.
272
if (opt_altKey && !opt_ctrlKey) {
273
return false;
274
}
275
276
// Saves Ctrl or Alt + key for IE and WebKit 525+, which won't fire keypress.
277
// WebKit prior to 525 won't get this far so no need to check the user agent.
278
// Gecko doesn't need to use the held key for modifiers, it just checks the
279
// ctrl/meta/alt/shiftKey fields.
280
if (!goog.userAgent.GECKO) {
281
if (typeof opt_heldKeyCode === 'number') {
282
opt_heldKeyCode = goog.events.KeyCodes.normalizeKeyCode(opt_heldKeyCode);
283
}
284
var heldKeyIsModifier = opt_heldKeyCode == goog.events.KeyCodes.CTRL ||
285
opt_heldKeyCode == goog.events.KeyCodes.ALT ||
286
goog.userAgent.MAC && opt_heldKeyCode == goog.events.KeyCodes.META;
287
// The Shift key blocks keypresses on Mac iff accompanied by another
288
// modifier.
289
var modifiedShiftKey = opt_heldKeyCode == goog.events.KeyCodes.SHIFT &&
290
(opt_ctrlKey || opt_metaKey);
291
if ((!opt_shiftKey || goog.userAgent.MAC) && heldKeyIsModifier ||
292
goog.userAgent.MAC && modifiedShiftKey) {
293
return false;
294
}
295
}
296
297
// Some keys with Ctrl/Shift do not issue keypress in WEBKIT.
298
if ((goog.userAgent.WEBKIT || goog.userAgent.EDGE) && opt_ctrlKey &&
299
opt_shiftKey) {
300
switch (keyCode) {
301
case goog.events.KeyCodes.BACKSLASH:
302
case goog.events.KeyCodes.OPEN_SQUARE_BRACKET:
303
case goog.events.KeyCodes.CLOSE_SQUARE_BRACKET:
304
case goog.events.KeyCodes.TILDE:
305
case goog.events.KeyCodes.SEMICOLON:
306
case goog.events.KeyCodes.DASH:
307
case goog.events.KeyCodes.EQUALS:
308
case goog.events.KeyCodes.COMMA:
309
case goog.events.KeyCodes.PERIOD:
310
case goog.events.KeyCodes.SLASH:
311
case goog.events.KeyCodes.APOSTROPHE:
312
case goog.events.KeyCodes.SINGLE_QUOTE:
313
return false;
314
}
315
}
316
317
// When Ctrl+<somekey> is held in IE, it only fires a keypress once, but it
318
// continues to fire keydown events as the event repeats.
319
if (goog.userAgent.IE && opt_ctrlKey && opt_heldKeyCode == keyCode) {
320
return false;
321
}
322
323
switch (keyCode) {
324
case goog.events.KeyCodes.ENTER:
325
if (goog.userAgent.GECKO) {
326
// Only Enter, Shift + Enter, Ctrl + Enter causes keypress event on
327
// Firefox.
328
if (opt_metaKey || opt_altKey) {
329
return false;
330
}
331
return !(opt_shiftKey && opt_ctrlKey);
332
} else {
333
return true;
334
}
335
case goog.events.KeyCodes.ESC:
336
return !(
337
goog.userAgent.WEBKIT || goog.userAgent.EDGE || goog.userAgent.GECKO);
338
}
339
340
// Gecko won't fire a keypress event even when the key is a character key if
341
// ctrl, meta or alt are pressed. In all other cases, a keypress event is
342
// only fired when the key is a character.
343
if (goog.userAgent.GECKO && (opt_ctrlKey || opt_altKey || opt_metaKey)) {
344
return false;
345
} else {
346
return goog.events.KeyCodes.isCharacterKey(keyCode);
347
}
348
};
349
350
351
/**
352
* Returns true if the key produces a character.
353
* This does not cover characters on non-US keyboards (Russian, Hebrew, etc.).
354
*
355
* @param {number} keyCode A key code.
356
* @return {boolean} Whether it's a character key.
357
*/
358
goog.events.KeyCodes.isCharacterKey = function(keyCode) {
359
'use strict';
360
if (keyCode >= goog.events.KeyCodes.ZERO &&
361
keyCode <= goog.events.KeyCodes.NINE) {
362
return true;
363
}
364
365
if (keyCode >= goog.events.KeyCodes.NUM_ZERO &&
366
keyCode <= goog.events.KeyCodes.NUM_MULTIPLY) {
367
return true;
368
}
369
370
if (keyCode >= goog.events.KeyCodes.A && keyCode <= goog.events.KeyCodes.Z) {
371
return true;
372
}
373
374
// Safari sends zero key code for non-latin characters.
375
if ((goog.userAgent.WEBKIT || goog.userAgent.EDGE) && keyCode == 0) {
376
return true;
377
}
378
379
switch (keyCode) {
380
case goog.events.KeyCodes.SPACE:
381
case goog.events.KeyCodes.PLUS_SIGN:
382
case goog.events.KeyCodes.QUESTION_MARK:
383
case goog.events.KeyCodes.AT_SIGN:
384
case goog.events.KeyCodes.NUM_PLUS:
385
case goog.events.KeyCodes.NUM_MINUS:
386
case goog.events.KeyCodes.NUM_PERIOD:
387
case goog.events.KeyCodes.NUM_DIVISION:
388
case goog.events.KeyCodes.SEMICOLON:
389
case goog.events.KeyCodes.FF_SEMICOLON:
390
case goog.events.KeyCodes.DASH:
391
case goog.events.KeyCodes.EQUALS:
392
case goog.events.KeyCodes.FF_EQUALS:
393
case goog.events.KeyCodes.COMMA:
394
case goog.events.KeyCodes.PERIOD:
395
case goog.events.KeyCodes.SLASH:
396
case goog.events.KeyCodes.APOSTROPHE:
397
case goog.events.KeyCodes.SINGLE_QUOTE:
398
case goog.events.KeyCodes.OPEN_SQUARE_BRACKET:
399
case goog.events.KeyCodes.BACKSLASH:
400
case goog.events.KeyCodes.CLOSE_SQUARE_BRACKET:
401
case goog.events.KeyCodes.FF_HASH:
402
case goog.events.KeyCodes.FF_JP_QUOTE:
403
return true;
404
case goog.events.KeyCodes.FF_DASH:
405
return goog.userAgent.GECKO;
406
default:
407
return false;
408
}
409
};
410
411
412
/**
413
* Normalizes key codes from OS/Browser-specific value to the general one.
414
* @param {number} keyCode The native key code.
415
* @return {number} The normalized key code.
416
*/
417
goog.events.KeyCodes.normalizeKeyCode = function(keyCode) {
418
'use strict';
419
if (goog.userAgent.GECKO) {
420
return goog.events.KeyCodes.normalizeGeckoKeyCode(keyCode);
421
} else if (goog.userAgent.MAC && goog.userAgent.WEBKIT) {
422
return goog.events.KeyCodes.normalizeMacWebKitKeyCode(keyCode);
423
} else {
424
return keyCode;
425
}
426
};
427
428
429
/**
430
* Normalizes key codes from their Gecko-specific value to the general one.
431
* @param {number} keyCode The native key code.
432
* @return {number} The normalized key code.
433
*/
434
goog.events.KeyCodes.normalizeGeckoKeyCode = function(keyCode) {
435
'use strict';
436
switch (keyCode) {
437
case goog.events.KeyCodes.FF_EQUALS:
438
return goog.events.KeyCodes.EQUALS;
439
case goog.events.KeyCodes.FF_SEMICOLON:
440
return goog.events.KeyCodes.SEMICOLON;
441
case goog.events.KeyCodes.FF_DASH:
442
return goog.events.KeyCodes.DASH;
443
case goog.events.KeyCodes.MAC_FF_META:
444
return goog.events.KeyCodes.META;
445
case goog.events.KeyCodes.WIN_KEY_FF_LINUX:
446
return goog.events.KeyCodes.WIN_KEY;
447
default:
448
return keyCode;
449
}
450
};
451
452
453
/**
454
* Normalizes key codes from their Mac WebKit-specific value to the general one.
455
* @param {number} keyCode The native key code.
456
* @return {number} The normalized key code.
457
*/
458
goog.events.KeyCodes.normalizeMacWebKitKeyCode = function(keyCode) {
459
'use strict';
460
switch (keyCode) {
461
case goog.events.KeyCodes.MAC_WK_CMD_RIGHT: // 93
462
return goog.events.KeyCodes.META; // 91
463
default:
464
return keyCode;
465
}
466
};
467
468