Path: blob/trunk/third_party/closure/goog/events/keyhandler.js
4062 views
/**1* @license2* Copyright The Closure Library Authors.3* SPDX-License-Identifier: Apache-2.04*/56/**7* @fileoverview This file contains a class for working with keyboard events8* that repeat consistently across browsers and platforms. It also unifies the9* key code so that it is the same in all browsers and platforms.10*11* Different web browsers have very different keyboard event handling. Most12* importantly is that only certain browsers repeat keydown events:13* IE, Opera, FF/Win32, and Safari 3 repeat keydown events.14* FF/Mac and Safari 2 do not.15*16* For the purposes of this code, "Safari 3" means WebKit 525+, when WebKit17* decided that they should try to match IE's key handling behavior.18* Safari 3.0.4, which shipped with Leopard (WebKit 523), has the19* Safari 2 behavior.20*21* Firefox, Safari, Opera prevent on keypress22*23* IE prevents on keydown24*25* Firefox does not fire keypress for shift, ctrl, alt26* Firefox does fire keydown for shift, ctrl, alt, meta27* Firefox does not repeat keydown for shift, ctrl, alt, meta28*29* Firefox does not fire keypress for up and down in an input30*31* Opera fires keypress for shift, ctrl, alt, meta32* Opera does not repeat keypress for shift, ctrl, alt, meta33*34* Safari 2 and 3 do not fire keypress for shift, ctrl, alt35* Safari 2 does not fire keydown for shift, ctrl, alt36* Safari 3 *does* fire keydown for shift, ctrl, alt37*38* IE provides the keycode for keyup/down events and the charcode (in the39* keycode field) for keypress.40*41* Mozilla provides the keycode for keyup/down and the charcode for keypress42* unless it's a non text modifying key in which case the keycode is provided.43*44* Safari 3 provides the keycode and charcode for all events.45*46* Opera provides the keycode for keyup/down event and either the charcode or47* the keycode (in the keycode field) for keypress events.48*49* Firefox x11 doesn't fire keydown events if a another key is already held down50* until the first key is released. This can cause a key event to be fired with51* a keyCode for the first key and a charCode for the second key.52*53* Safari in keypress54*55* charCode keyCode which56* ENTER: 13 13 1357* F1: 63236 63236 6323658* F8: 63243 63243 6324359* ...60* p: 112 112 11261* P: 80 80 8062*63* Firefox, keypress:64*65* charCode keyCode which66* ENTER: 0 13 1367* F1: 0 112 068* F8: 0 119 069* ...70* p: 112 0 11271* P: 80 0 8072*73* Opera, Mac+Win32, keypress:74*75* charCode keyCode which76* ENTER: undefined 13 1377* F1: undefined 112 078* F8: undefined 119 079* ...80* p: undefined 112 11281* P: undefined 80 8082*83* IE7, keydown84*85* charCode keyCode which86* ENTER: undefined 13 undefined87* F1: undefined 112 undefined88* F8: undefined 119 undefined89* ...90* p: undefined 80 undefined91* P: undefined 80 undefined92*93* @see ../demos/keyhandler.html94*/959697goog.provide('goog.events.KeyHandler');98goog.provide('goog.events.KeyHandler.EventType');99100goog.require('goog.events');101goog.require('goog.events.BrowserEvent');102goog.require('goog.events.EventTarget');103goog.require('goog.events.EventType');104goog.require('goog.events.KeyCodes');105goog.require('goog.events.KeyEvent');106goog.require('goog.userAgent');107108109110/**111* A wrapper around an element that you want to listen to keyboard events on.112* @param {Element|Document=} opt_element The element or document to listen on.113* @param {boolean=} opt_capture Whether to listen for browser events in114* capture phase (defaults to false).115* @constructor116* @extends {goog.events.EventTarget}117* @final118*/119goog.events.KeyHandler = function(opt_element, opt_capture) {120'use strict';121goog.events.EventTarget.call(this);122123if (opt_element) {124this.attach(opt_element, opt_capture);125}126};127goog.inherits(goog.events.KeyHandler, goog.events.EventTarget);128129130/**131* This is the element that we will listen to the real keyboard events on.132* @type {?Element|?Document|null}133* @private134*/135goog.events.KeyHandler.prototype.element_ = null;136137138/**139* The key for the key press listener.140* @type {?goog.events.Key}141* @private142*/143goog.events.KeyHandler.prototype.keyPressKey_ = null;144145146/**147* The key for the key down listener.148* @type {?goog.events.Key}149* @private150*/151goog.events.KeyHandler.prototype.keyDownKey_ = null;152153154/**155* The key for the key up listener.156* @type {?goog.events.Key}157* @private158*/159goog.events.KeyHandler.prototype.keyUpKey_ = null;160161162/**163* Used to detect keyboard repeat events.164* @private165* @type {number}166*/167goog.events.KeyHandler.prototype.lastKey_ = -1;168169170/**171* Keycode recorded for key down events. As most browsers don't report the172* keycode in the key press event we need to record it in the key down phase.173* @private174* @type {number}175*/176goog.events.KeyHandler.prototype.keyCode_ = -1;177178179/**180* Alt key recorded for key down events. FF on Mac does not report the alt key181* flag in the key press event, we need to record it in the key down phase.182* @type {boolean}183* @private184*/185goog.events.KeyHandler.prototype.altKey_ = false;186187188/**189* Enum type for the events fired by the key handler190* @const191* @deprecated use `goog.events.KeyEvent.EventType` instead.192*/193goog.events.KeyHandler.EventType = goog.events.KeyEvent.EventType;194195196/**197* An enumeration of key codes that Safari 2 does incorrectly198* @type {Object}199* @private200*/201goog.events.KeyHandler.safariKey_ = {202'3': goog.events.KeyCodes.ENTER, // 13203'12': goog.events.KeyCodes.NUMLOCK, // 144204'63232': goog.events.KeyCodes.UP, // 38205'63233': goog.events.KeyCodes.DOWN, // 40206'63234': goog.events.KeyCodes.LEFT, // 37207'63235': goog.events.KeyCodes.RIGHT, // 39208'63236': goog.events.KeyCodes.F1, // 112209'63237': goog.events.KeyCodes.F2, // 113210'63238': goog.events.KeyCodes.F3, // 114211'63239': goog.events.KeyCodes.F4, // 115212'63240': goog.events.KeyCodes.F5, // 116213'63241': goog.events.KeyCodes.F6, // 117214'63242': goog.events.KeyCodes.F7, // 118215'63243': goog.events.KeyCodes.F8, // 119216'63244': goog.events.KeyCodes.F9, // 120217'63245': goog.events.KeyCodes.F10, // 121218'63246': goog.events.KeyCodes.F11, // 122219'63247': goog.events.KeyCodes.F12, // 123220'63248': goog.events.KeyCodes.PRINT_SCREEN, // 44221'63272': goog.events.KeyCodes.DELETE, // 46222'63273': goog.events.KeyCodes.HOME, // 36223'63275': goog.events.KeyCodes.END, // 35224'63276': goog.events.KeyCodes.PAGE_UP, // 33225'63277': goog.events.KeyCodes.PAGE_DOWN, // 34226'63289': goog.events.KeyCodes.NUMLOCK, // 144227'63302': goog.events.KeyCodes.INSERT // 45228};229230231/**232* An enumeration of key identifiers currently part of the W3C draft for DOM3233* and their mappings to keyCodes.234* http://www.w3.org/TR/DOM-Level-3-Events/keyset.html#KeySet-Set235* This is currently supported in Safari and should be platform independent.236* @type {Object}237* @private238*/239goog.events.KeyHandler.keyIdentifier_ = {240'Up': goog.events.KeyCodes.UP, // 38241'Down': goog.events.KeyCodes.DOWN, // 40242'Left': goog.events.KeyCodes.LEFT, // 37243'Right': goog.events.KeyCodes.RIGHT, // 39244'Enter': goog.events.KeyCodes.ENTER, // 13245'F1': goog.events.KeyCodes.F1, // 112246'F2': goog.events.KeyCodes.F2, // 113247'F3': goog.events.KeyCodes.F3, // 114248'F4': goog.events.KeyCodes.F4, // 115249'F5': goog.events.KeyCodes.F5, // 116250'F6': goog.events.KeyCodes.F6, // 117251'F7': goog.events.KeyCodes.F7, // 118252'F8': goog.events.KeyCodes.F8, // 119253'F9': goog.events.KeyCodes.F9, // 120254'F10': goog.events.KeyCodes.F10, // 121255'F11': goog.events.KeyCodes.F11, // 122256'F12': goog.events.KeyCodes.F12, // 123257'U+007F': goog.events.KeyCodes.DELETE, // 46258'Home': goog.events.KeyCodes.HOME, // 36259'End': goog.events.KeyCodes.END, // 35260'PageUp': goog.events.KeyCodes.PAGE_UP, // 33261'PageDown': goog.events.KeyCodes.PAGE_DOWN, // 34262'Insert': goog.events.KeyCodes.INSERT // 45263};264265266267268/**269* If true, the alt key flag is saved during the key down and reused when270* handling the key press. FF on Mac does not set the alt flag in the key press271* event.272* @type {boolean}273* @private274*/275goog.events.KeyHandler.SAVE_ALT_FOR_KEYPRESS_ =276goog.userAgent.MAC && goog.userAgent.GECKO;277278279/**280* Records the keycode for browsers that only returns the keycode for key up/281* down events. For browser/key combinations that doesn't trigger a key pressed282* event it also fires the patched key event.283* @param {goog.events.BrowserEvent} e The key down event.284* @private285*/286goog.events.KeyHandler.prototype.handleKeyDown_ = function(e) {287'use strict';288// Ctrl-Tab and Alt-Tab can cause the focus to be moved to another window289// before we've caught a key-up event. If the last-key was one of these we290// reset the state.291if (goog.userAgent.WEBKIT || goog.userAgent.EDGE) {292if (this.lastKey_ == goog.events.KeyCodes.CTRL && !e.ctrlKey ||293this.lastKey_ == goog.events.KeyCodes.ALT && !e.altKey ||294goog.userAgent.MAC && this.lastKey_ == goog.events.KeyCodes.META &&295!e.metaKey) {296this.resetState();297}298}299300if (this.lastKey_ == -1) {301if (e.ctrlKey && e.keyCode != goog.events.KeyCodes.CTRL) {302this.lastKey_ = goog.events.KeyCodes.CTRL;303} else if (e.altKey && e.keyCode != goog.events.KeyCodes.ALT) {304this.lastKey_ = goog.events.KeyCodes.ALT;305} else if (e.metaKey && e.keyCode != goog.events.KeyCodes.META) {306this.lastKey_ = goog.events.KeyCodes.META;307}308}309310if (!goog.events.KeyCodes.firesKeyPressEvent(311e.keyCode, this.lastKey_, e.shiftKey, e.ctrlKey, e.altKey,312e.metaKey)) {313this.handleEvent(e);314} else {315this.keyCode_ = goog.events.KeyCodes.normalizeKeyCode(e.keyCode);316if (goog.events.KeyHandler.SAVE_ALT_FOR_KEYPRESS_) {317this.altKey_ = e.altKey;318}319}320};321322323/**324* Resets the stored previous values. Needed to be called for webkit which will325* not generate a key up for meta key operations. This should only be called326* when having finished with repeat key possibilities.327*/328goog.events.KeyHandler.prototype.resetState = function() {329'use strict';330this.lastKey_ = -1;331this.keyCode_ = -1;332};333334335/**336* Clears the stored previous key value, resetting the key repeat status. Uses337* -1 because the Safari 3 Windows beta reports 0 for certain keys (like Home338* and End.)339* @param {goog.events.BrowserEvent} e The keyup event.340* @private341*/342goog.events.KeyHandler.prototype.handleKeyup_ = function(e) {343'use strict';344this.resetState();345this.altKey_ = e.altKey;346};347348349/**350* Handles the events on the element.351* @param {goog.events.BrowserEvent} e The keyboard event sent from the352* browser.353* @suppress {strictMissingProperties} Added to tighten compiler checks354*/355goog.events.KeyHandler.prototype.handleEvent = function(e) {356'use strict';357var be = e.getBrowserEvent();358var keyCode, charCode;359var altKey = be.altKey;360361// IE reports the character code in the keyCode field for keypress events.362// There are two exceptions however, Enter and Escape.363if (goog.userAgent.IE && e.type == goog.events.EventType.KEYPRESS) {364keyCode = this.keyCode_;365charCode = keyCode != goog.events.KeyCodes.ENTER &&366keyCode != goog.events.KeyCodes.ESC ?367be.keyCode :3680;369370// Safari reports the character code in the keyCode field for keypress371// events but also has a charCode field.372} else if (373(goog.userAgent.WEBKIT || goog.userAgent.EDGE) &&374e.type == goog.events.EventType.KEYPRESS) {375keyCode = this.keyCode_;376charCode = be.charCode >= 0 && be.charCode < 63232 &&377goog.events.KeyCodes.isCharacterKey(keyCode) ?378be.charCode :3790;380381// Opera reports the keycode or the character code in the keyCode field.382} else {383if (e.type == goog.events.EventType.KEYPRESS) {384if (goog.events.KeyHandler.SAVE_ALT_FOR_KEYPRESS_) {385altKey = this.altKey_;386}387388// Newer versions of Firefox will set the keyCode of non-function keys to389// be the same as charCode. We need to account for this and update the390// key event values accordingly. See391// https://github.com/google/closure-library/issues/932 for more details.392if (be.keyCode == be.charCode) {393// Adjust any function key (ie. non-printable, such as ESC or394// backspace) to not have a charCode. We don't want these keys to395// accidentally be interpreted as insertable characters.396if (be.keyCode < 0x20) {397keyCode = be.keyCode;398charCode = 0;399} else {400// For character keys, we want to use the preserved key code rather401// than the keyCode on the browser event, which now uses the charCode.402// These differ (eg. pressing 'a' gives keydown with keyCode = 65,403// keypress with keyCode = charCode = 97) and so we need to account404// for this.405keyCode = this.keyCode_;406charCode = be.charCode;407}408} else {409keyCode = be.keyCode || this.keyCode_;410charCode = be.charCode || 0;411}412} else {413keyCode = be.keyCode || this.keyCode_;414charCode = be.charCode || 0;415}416417// On the Mac, shift-/ triggers a question mark char code and no key code418// (WIN_KEY_FF_LINUX), so we synthesize the latter.419if (goog.userAgent.MAC && charCode == goog.events.KeyCodes.QUESTION_MARK &&420keyCode == goog.events.KeyCodes.WIN_KEY) {421keyCode = goog.events.KeyCodes.SLASH;422}423}424425keyCode = goog.events.KeyCodes.normalizeKeyCode(keyCode);426var key = keyCode;427428// Correct the key value for certain browser-specific quirks.429if (keyCode) {430if (keyCode >= 63232 && keyCode in goog.events.KeyHandler.safariKey_) {431// NOTE(nicksantos): Safari 3 has fixed this problem,432// this is only needed for Safari 2.433key = goog.events.KeyHandler.safariKey_[keyCode];434} else {435// Safari returns 25 for Shift+Tab instead of 9.436if (keyCode == 25 && e.shiftKey) {437key = 9;438}439}440} else if (441be.keyIdentifier &&442be.keyIdentifier in goog.events.KeyHandler.keyIdentifier_) {443// This is needed for Safari Windows because it currently doesn't give a444// keyCode/which for non printable keys.445key = goog.events.KeyHandler.keyIdentifier_[be.keyIdentifier];446}447448// If this was a redundant keypress event, we ignore it to avoid double-firing449// an event as the event would've been handled by KEYDOWN. Gecko is currently450// in the process of removing keypress events for non-printable characters451// (https://bugzilla.mozilla.org/show_bug.cgi?id=968056) so we simulate this452// logic here for older Gecko versions which still fire the events.453if (goog.userAgent.GECKO && e.type == goog.events.EventType.KEYPRESS &&454!goog.events.KeyCodes.firesKeyPressEvent(455key, this.lastKey_, e.shiftKey, e.ctrlKey, altKey, e.metaKey)) {456return;457}458459// If we get the same keycode as a keydown/keypress without having seen a460// keyup event, then this event was caused by key repeat.461var repeat = key == this.lastKey_;462this.lastKey_ = key;463464var event = new goog.events.KeyEvent(key, charCode, repeat, be);465event.altKey = altKey;466this.dispatchEvent(event);467};468469470/**471* Returns the element listened on for the real keyboard events.472* @return {Element|Document|null} The element listened on for the real473* keyboard events.474*/475goog.events.KeyHandler.prototype.getElement = function() {476'use strict';477return this.element_;478};479480481/**482* Adds the proper key event listeners to the element.483* @param {Element|Document} element The element to listen on.484* @param {boolean=} opt_capture Whether to listen for browser events in485* capture phase (defaults to false).486*/487goog.events.KeyHandler.prototype.attach = function(element, opt_capture) {488'use strict';489if (this.keyUpKey_) {490this.detach();491}492493this.element_ = element;494495this.keyPressKey_ = goog.events.listen(496this.element_, goog.events.EventType.KEYPRESS, this, opt_capture);497498// Most browsers (Safari 2 being the notable exception) doesn't include the499// keyCode in keypress events (IE has the char code in the keyCode field and500// Mozilla only included the keyCode if there's no charCode). Thus we have to501// listen for keydown to capture the keycode.502this.keyDownKey_ = goog.events.listen(503this.element_, goog.events.EventType.KEYDOWN, this.handleKeyDown_,504opt_capture, this);505506507this.keyUpKey_ = goog.events.listen(508this.element_, goog.events.EventType.KEYUP, this.handleKeyup_,509opt_capture, this);510};511512513/**514* Removes the listeners that may exist.515*/516goog.events.KeyHandler.prototype.detach = function() {517'use strict';518if (this.keyPressKey_) {519goog.events.unlistenByKey(this.keyPressKey_);520goog.events.unlistenByKey(this.keyDownKey_);521goog.events.unlistenByKey(this.keyUpKey_);522this.keyPressKey_ = null;523this.keyDownKey_ = null;524this.keyUpKey_ = null;525}526this.element_ = null;527this.lastKey_ = -1;528this.keyCode_ = -1;529};530531532/** @override */533goog.events.KeyHandler.prototype.disposeInternal = function() {534'use strict';535goog.events.KeyHandler.superClass_.disposeInternal.call(this);536this.detach();537};538539540