Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
SeleniumHQ
GitHub Repository: SeleniumHQ/Selenium
Path: blob/trunk/third_party/closure/goog/events/keyhandler.js
4062 views
1
/**
2
* @license
3
* Copyright The Closure Library Authors.
4
* SPDX-License-Identifier: Apache-2.0
5
*/
6
7
/**
8
* @fileoverview This file contains a class for working with keyboard events
9
* that repeat consistently across browsers and platforms. It also unifies the
10
* key code so that it is the same in all browsers and platforms.
11
*
12
* Different web browsers have very different keyboard event handling. Most
13
* importantly is that only certain browsers repeat keydown events:
14
* IE, Opera, FF/Win32, and Safari 3 repeat keydown events.
15
* FF/Mac and Safari 2 do not.
16
*
17
* For the purposes of this code, "Safari 3" means WebKit 525+, when WebKit
18
* decided that they should try to match IE's key handling behavior.
19
* Safari 3.0.4, which shipped with Leopard (WebKit 523), has the
20
* Safari 2 behavior.
21
*
22
* Firefox, Safari, Opera prevent on keypress
23
*
24
* IE prevents on keydown
25
*
26
* Firefox does not fire keypress for shift, ctrl, alt
27
* Firefox does fire keydown for shift, ctrl, alt, meta
28
* Firefox does not repeat keydown for shift, ctrl, alt, meta
29
*
30
* Firefox does not fire keypress for up and down in an input
31
*
32
* Opera fires keypress for shift, ctrl, alt, meta
33
* Opera does not repeat keypress for shift, ctrl, alt, meta
34
*
35
* Safari 2 and 3 do not fire keypress for shift, ctrl, alt
36
* Safari 2 does not fire keydown for shift, ctrl, alt
37
* Safari 3 *does* fire keydown for shift, ctrl, alt
38
*
39
* IE provides the keycode for keyup/down events and the charcode (in the
40
* keycode field) for keypress.
41
*
42
* Mozilla provides the keycode for keyup/down and the charcode for keypress
43
* unless it's a non text modifying key in which case the keycode is provided.
44
*
45
* Safari 3 provides the keycode and charcode for all events.
46
*
47
* Opera provides the keycode for keyup/down event and either the charcode or
48
* the keycode (in the keycode field) for keypress events.
49
*
50
* Firefox x11 doesn't fire keydown events if a another key is already held down
51
* until the first key is released. This can cause a key event to be fired with
52
* a keyCode for the first key and a charCode for the second key.
53
*
54
* Safari in keypress
55
*
56
* charCode keyCode which
57
* ENTER: 13 13 13
58
* F1: 63236 63236 63236
59
* F8: 63243 63243 63243
60
* ...
61
* p: 112 112 112
62
* P: 80 80 80
63
*
64
* Firefox, keypress:
65
*
66
* charCode keyCode which
67
* ENTER: 0 13 13
68
* F1: 0 112 0
69
* F8: 0 119 0
70
* ...
71
* p: 112 0 112
72
* P: 80 0 80
73
*
74
* Opera, Mac+Win32, keypress:
75
*
76
* charCode keyCode which
77
* ENTER: undefined 13 13
78
* F1: undefined 112 0
79
* F8: undefined 119 0
80
* ...
81
* p: undefined 112 112
82
* P: undefined 80 80
83
*
84
* IE7, keydown
85
*
86
* charCode keyCode which
87
* ENTER: undefined 13 undefined
88
* F1: undefined 112 undefined
89
* F8: undefined 119 undefined
90
* ...
91
* p: undefined 80 undefined
92
* P: undefined 80 undefined
93
*
94
* @see ../demos/keyhandler.html
95
*/
96
97
98
goog.provide('goog.events.KeyHandler');
99
goog.provide('goog.events.KeyHandler.EventType');
100
101
goog.require('goog.events');
102
goog.require('goog.events.BrowserEvent');
103
goog.require('goog.events.EventTarget');
104
goog.require('goog.events.EventType');
105
goog.require('goog.events.KeyCodes');
106
goog.require('goog.events.KeyEvent');
107
goog.require('goog.userAgent');
108
109
110
111
/**
112
* A wrapper around an element that you want to listen to keyboard events on.
113
* @param {Element|Document=} opt_element The element or document to listen on.
114
* @param {boolean=} opt_capture Whether to listen for browser events in
115
* capture phase (defaults to false).
116
* @constructor
117
* @extends {goog.events.EventTarget}
118
* @final
119
*/
120
goog.events.KeyHandler = function(opt_element, opt_capture) {
121
'use strict';
122
goog.events.EventTarget.call(this);
123
124
if (opt_element) {
125
this.attach(opt_element, opt_capture);
126
}
127
};
128
goog.inherits(goog.events.KeyHandler, goog.events.EventTarget);
129
130
131
/**
132
* This is the element that we will listen to the real keyboard events on.
133
* @type {?Element|?Document|null}
134
* @private
135
*/
136
goog.events.KeyHandler.prototype.element_ = null;
137
138
139
/**
140
* The key for the key press listener.
141
* @type {?goog.events.Key}
142
* @private
143
*/
144
goog.events.KeyHandler.prototype.keyPressKey_ = null;
145
146
147
/**
148
* The key for the key down listener.
149
* @type {?goog.events.Key}
150
* @private
151
*/
152
goog.events.KeyHandler.prototype.keyDownKey_ = null;
153
154
155
/**
156
* The key for the key up listener.
157
* @type {?goog.events.Key}
158
* @private
159
*/
160
goog.events.KeyHandler.prototype.keyUpKey_ = null;
161
162
163
/**
164
* Used to detect keyboard repeat events.
165
* @private
166
* @type {number}
167
*/
168
goog.events.KeyHandler.prototype.lastKey_ = -1;
169
170
171
/**
172
* Keycode recorded for key down events. As most browsers don't report the
173
* keycode in the key press event we need to record it in the key down phase.
174
* @private
175
* @type {number}
176
*/
177
goog.events.KeyHandler.prototype.keyCode_ = -1;
178
179
180
/**
181
* Alt key recorded for key down events. FF on Mac does not report the alt key
182
* flag in the key press event, we need to record it in the key down phase.
183
* @type {boolean}
184
* @private
185
*/
186
goog.events.KeyHandler.prototype.altKey_ = false;
187
188
189
/**
190
* Enum type for the events fired by the key handler
191
* @const
192
* @deprecated use `goog.events.KeyEvent.EventType` instead.
193
*/
194
goog.events.KeyHandler.EventType = goog.events.KeyEvent.EventType;
195
196
197
/**
198
* An enumeration of key codes that Safari 2 does incorrectly
199
* @type {Object}
200
* @private
201
*/
202
goog.events.KeyHandler.safariKey_ = {
203
'3': goog.events.KeyCodes.ENTER, // 13
204
'12': goog.events.KeyCodes.NUMLOCK, // 144
205
'63232': goog.events.KeyCodes.UP, // 38
206
'63233': goog.events.KeyCodes.DOWN, // 40
207
'63234': goog.events.KeyCodes.LEFT, // 37
208
'63235': goog.events.KeyCodes.RIGHT, // 39
209
'63236': goog.events.KeyCodes.F1, // 112
210
'63237': goog.events.KeyCodes.F2, // 113
211
'63238': goog.events.KeyCodes.F3, // 114
212
'63239': goog.events.KeyCodes.F4, // 115
213
'63240': goog.events.KeyCodes.F5, // 116
214
'63241': goog.events.KeyCodes.F6, // 117
215
'63242': goog.events.KeyCodes.F7, // 118
216
'63243': goog.events.KeyCodes.F8, // 119
217
'63244': goog.events.KeyCodes.F9, // 120
218
'63245': goog.events.KeyCodes.F10, // 121
219
'63246': goog.events.KeyCodes.F11, // 122
220
'63247': goog.events.KeyCodes.F12, // 123
221
'63248': goog.events.KeyCodes.PRINT_SCREEN, // 44
222
'63272': goog.events.KeyCodes.DELETE, // 46
223
'63273': goog.events.KeyCodes.HOME, // 36
224
'63275': goog.events.KeyCodes.END, // 35
225
'63276': goog.events.KeyCodes.PAGE_UP, // 33
226
'63277': goog.events.KeyCodes.PAGE_DOWN, // 34
227
'63289': goog.events.KeyCodes.NUMLOCK, // 144
228
'63302': goog.events.KeyCodes.INSERT // 45
229
};
230
231
232
/**
233
* An enumeration of key identifiers currently part of the W3C draft for DOM3
234
* and their mappings to keyCodes.
235
* http://www.w3.org/TR/DOM-Level-3-Events/keyset.html#KeySet-Set
236
* This is currently supported in Safari and should be platform independent.
237
* @type {Object}
238
* @private
239
*/
240
goog.events.KeyHandler.keyIdentifier_ = {
241
'Up': goog.events.KeyCodes.UP, // 38
242
'Down': goog.events.KeyCodes.DOWN, // 40
243
'Left': goog.events.KeyCodes.LEFT, // 37
244
'Right': goog.events.KeyCodes.RIGHT, // 39
245
'Enter': goog.events.KeyCodes.ENTER, // 13
246
'F1': goog.events.KeyCodes.F1, // 112
247
'F2': goog.events.KeyCodes.F2, // 113
248
'F3': goog.events.KeyCodes.F3, // 114
249
'F4': goog.events.KeyCodes.F4, // 115
250
'F5': goog.events.KeyCodes.F5, // 116
251
'F6': goog.events.KeyCodes.F6, // 117
252
'F7': goog.events.KeyCodes.F7, // 118
253
'F8': goog.events.KeyCodes.F8, // 119
254
'F9': goog.events.KeyCodes.F9, // 120
255
'F10': goog.events.KeyCodes.F10, // 121
256
'F11': goog.events.KeyCodes.F11, // 122
257
'F12': goog.events.KeyCodes.F12, // 123
258
'U+007F': goog.events.KeyCodes.DELETE, // 46
259
'Home': goog.events.KeyCodes.HOME, // 36
260
'End': goog.events.KeyCodes.END, // 35
261
'PageUp': goog.events.KeyCodes.PAGE_UP, // 33
262
'PageDown': goog.events.KeyCodes.PAGE_DOWN, // 34
263
'Insert': goog.events.KeyCodes.INSERT // 45
264
};
265
266
267
268
269
/**
270
* If true, the alt key flag is saved during the key down and reused when
271
* handling the key press. FF on Mac does not set the alt flag in the key press
272
* event.
273
* @type {boolean}
274
* @private
275
*/
276
goog.events.KeyHandler.SAVE_ALT_FOR_KEYPRESS_ =
277
goog.userAgent.MAC && goog.userAgent.GECKO;
278
279
280
/**
281
* Records the keycode for browsers that only returns the keycode for key up/
282
* down events. For browser/key combinations that doesn't trigger a key pressed
283
* event it also fires the patched key event.
284
* @param {goog.events.BrowserEvent} e The key down event.
285
* @private
286
*/
287
goog.events.KeyHandler.prototype.handleKeyDown_ = function(e) {
288
'use strict';
289
// Ctrl-Tab and Alt-Tab can cause the focus to be moved to another window
290
// before we've caught a key-up event. If the last-key was one of these we
291
// reset the state.
292
if (goog.userAgent.WEBKIT || goog.userAgent.EDGE) {
293
if (this.lastKey_ == goog.events.KeyCodes.CTRL && !e.ctrlKey ||
294
this.lastKey_ == goog.events.KeyCodes.ALT && !e.altKey ||
295
goog.userAgent.MAC && this.lastKey_ == goog.events.KeyCodes.META &&
296
!e.metaKey) {
297
this.resetState();
298
}
299
}
300
301
if (this.lastKey_ == -1) {
302
if (e.ctrlKey && e.keyCode != goog.events.KeyCodes.CTRL) {
303
this.lastKey_ = goog.events.KeyCodes.CTRL;
304
} else if (e.altKey && e.keyCode != goog.events.KeyCodes.ALT) {
305
this.lastKey_ = goog.events.KeyCodes.ALT;
306
} else if (e.metaKey && e.keyCode != goog.events.KeyCodes.META) {
307
this.lastKey_ = goog.events.KeyCodes.META;
308
}
309
}
310
311
if (!goog.events.KeyCodes.firesKeyPressEvent(
312
e.keyCode, this.lastKey_, e.shiftKey, e.ctrlKey, e.altKey,
313
e.metaKey)) {
314
this.handleEvent(e);
315
} else {
316
this.keyCode_ = goog.events.KeyCodes.normalizeKeyCode(e.keyCode);
317
if (goog.events.KeyHandler.SAVE_ALT_FOR_KEYPRESS_) {
318
this.altKey_ = e.altKey;
319
}
320
}
321
};
322
323
324
/**
325
* Resets the stored previous values. Needed to be called for webkit which will
326
* not generate a key up for meta key operations. This should only be called
327
* when having finished with repeat key possibilities.
328
*/
329
goog.events.KeyHandler.prototype.resetState = function() {
330
'use strict';
331
this.lastKey_ = -1;
332
this.keyCode_ = -1;
333
};
334
335
336
/**
337
* Clears the stored previous key value, resetting the key repeat status. Uses
338
* -1 because the Safari 3 Windows beta reports 0 for certain keys (like Home
339
* and End.)
340
* @param {goog.events.BrowserEvent} e The keyup event.
341
* @private
342
*/
343
goog.events.KeyHandler.prototype.handleKeyup_ = function(e) {
344
'use strict';
345
this.resetState();
346
this.altKey_ = e.altKey;
347
};
348
349
350
/**
351
* Handles the events on the element.
352
* @param {goog.events.BrowserEvent} e The keyboard event sent from the
353
* browser.
354
* @suppress {strictMissingProperties} Added to tighten compiler checks
355
*/
356
goog.events.KeyHandler.prototype.handleEvent = function(e) {
357
'use strict';
358
var be = e.getBrowserEvent();
359
var keyCode, charCode;
360
var altKey = be.altKey;
361
362
// IE reports the character code in the keyCode field for keypress events.
363
// There are two exceptions however, Enter and Escape.
364
if (goog.userAgent.IE && e.type == goog.events.EventType.KEYPRESS) {
365
keyCode = this.keyCode_;
366
charCode = keyCode != goog.events.KeyCodes.ENTER &&
367
keyCode != goog.events.KeyCodes.ESC ?
368
be.keyCode :
369
0;
370
371
// Safari reports the character code in the keyCode field for keypress
372
// events but also has a charCode field.
373
} else if (
374
(goog.userAgent.WEBKIT || goog.userAgent.EDGE) &&
375
e.type == goog.events.EventType.KEYPRESS) {
376
keyCode = this.keyCode_;
377
charCode = be.charCode >= 0 && be.charCode < 63232 &&
378
goog.events.KeyCodes.isCharacterKey(keyCode) ?
379
be.charCode :
380
0;
381
382
// Opera reports the keycode or the character code in the keyCode field.
383
} else {
384
if (e.type == goog.events.EventType.KEYPRESS) {
385
if (goog.events.KeyHandler.SAVE_ALT_FOR_KEYPRESS_) {
386
altKey = this.altKey_;
387
}
388
389
// Newer versions of Firefox will set the keyCode of non-function keys to
390
// be the same as charCode. We need to account for this and update the
391
// key event values accordingly. See
392
// https://github.com/google/closure-library/issues/932 for more details.
393
if (be.keyCode == be.charCode) {
394
// Adjust any function key (ie. non-printable, such as ESC or
395
// backspace) to not have a charCode. We don't want these keys to
396
// accidentally be interpreted as insertable characters.
397
if (be.keyCode < 0x20) {
398
keyCode = be.keyCode;
399
charCode = 0;
400
} else {
401
// For character keys, we want to use the preserved key code rather
402
// than the keyCode on the browser event, which now uses the charCode.
403
// These differ (eg. pressing 'a' gives keydown with keyCode = 65,
404
// keypress with keyCode = charCode = 97) and so we need to account
405
// for this.
406
keyCode = this.keyCode_;
407
charCode = be.charCode;
408
}
409
} else {
410
keyCode = be.keyCode || this.keyCode_;
411
charCode = be.charCode || 0;
412
}
413
} else {
414
keyCode = be.keyCode || this.keyCode_;
415
charCode = be.charCode || 0;
416
}
417
418
// On the Mac, shift-/ triggers a question mark char code and no key code
419
// (WIN_KEY_FF_LINUX), so we synthesize the latter.
420
if (goog.userAgent.MAC && charCode == goog.events.KeyCodes.QUESTION_MARK &&
421
keyCode == goog.events.KeyCodes.WIN_KEY) {
422
keyCode = goog.events.KeyCodes.SLASH;
423
}
424
}
425
426
keyCode = goog.events.KeyCodes.normalizeKeyCode(keyCode);
427
var key = keyCode;
428
429
// Correct the key value for certain browser-specific quirks.
430
if (keyCode) {
431
if (keyCode >= 63232 && keyCode in goog.events.KeyHandler.safariKey_) {
432
// NOTE(nicksantos): Safari 3 has fixed this problem,
433
// this is only needed for Safari 2.
434
key = goog.events.KeyHandler.safariKey_[keyCode];
435
} else {
436
// Safari returns 25 for Shift+Tab instead of 9.
437
if (keyCode == 25 && e.shiftKey) {
438
key = 9;
439
}
440
}
441
} else if (
442
be.keyIdentifier &&
443
be.keyIdentifier in goog.events.KeyHandler.keyIdentifier_) {
444
// This is needed for Safari Windows because it currently doesn't give a
445
// keyCode/which for non printable keys.
446
key = goog.events.KeyHandler.keyIdentifier_[be.keyIdentifier];
447
}
448
449
// If this was a redundant keypress event, we ignore it to avoid double-firing
450
// an event as the event would've been handled by KEYDOWN. Gecko is currently
451
// in the process of removing keypress events for non-printable characters
452
// (https://bugzilla.mozilla.org/show_bug.cgi?id=968056) so we simulate this
453
// logic here for older Gecko versions which still fire the events.
454
if (goog.userAgent.GECKO && e.type == goog.events.EventType.KEYPRESS &&
455
!goog.events.KeyCodes.firesKeyPressEvent(
456
key, this.lastKey_, e.shiftKey, e.ctrlKey, altKey, e.metaKey)) {
457
return;
458
}
459
460
// If we get the same keycode as a keydown/keypress without having seen a
461
// keyup event, then this event was caused by key repeat.
462
var repeat = key == this.lastKey_;
463
this.lastKey_ = key;
464
465
var event = new goog.events.KeyEvent(key, charCode, repeat, be);
466
event.altKey = altKey;
467
this.dispatchEvent(event);
468
};
469
470
471
/**
472
* Returns the element listened on for the real keyboard events.
473
* @return {Element|Document|null} The element listened on for the real
474
* keyboard events.
475
*/
476
goog.events.KeyHandler.prototype.getElement = function() {
477
'use strict';
478
return this.element_;
479
};
480
481
482
/**
483
* Adds the proper key event listeners to the element.
484
* @param {Element|Document} element The element to listen on.
485
* @param {boolean=} opt_capture Whether to listen for browser events in
486
* capture phase (defaults to false).
487
*/
488
goog.events.KeyHandler.prototype.attach = function(element, opt_capture) {
489
'use strict';
490
if (this.keyUpKey_) {
491
this.detach();
492
}
493
494
this.element_ = element;
495
496
this.keyPressKey_ = goog.events.listen(
497
this.element_, goog.events.EventType.KEYPRESS, this, opt_capture);
498
499
// Most browsers (Safari 2 being the notable exception) doesn't include the
500
// keyCode in keypress events (IE has the char code in the keyCode field and
501
// Mozilla only included the keyCode if there's no charCode). Thus we have to
502
// listen for keydown to capture the keycode.
503
this.keyDownKey_ = goog.events.listen(
504
this.element_, goog.events.EventType.KEYDOWN, this.handleKeyDown_,
505
opt_capture, this);
506
507
508
this.keyUpKey_ = goog.events.listen(
509
this.element_, goog.events.EventType.KEYUP, this.handleKeyup_,
510
opt_capture, this);
511
};
512
513
514
/**
515
* Removes the listeners that may exist.
516
*/
517
goog.events.KeyHandler.prototype.detach = function() {
518
'use strict';
519
if (this.keyPressKey_) {
520
goog.events.unlistenByKey(this.keyPressKey_);
521
goog.events.unlistenByKey(this.keyDownKey_);
522
goog.events.unlistenByKey(this.keyUpKey_);
523
this.keyPressKey_ = null;
524
this.keyDownKey_ = null;
525
this.keyUpKey_ = null;
526
}
527
this.element_ = null;
528
this.lastKey_ = -1;
529
this.keyCode_ = -1;
530
};
531
532
533
/** @override */
534
goog.events.KeyHandler.prototype.disposeInternal = function() {
535
'use strict';
536
goog.events.KeyHandler.superClass_.disposeInternal.call(this);
537
this.detach();
538
};
539
540