Path: blob/trunk/third_party/closure/goog/events/keycodes.js
4102 views
/**1* @license2* Copyright The Closure Library Authors.3* SPDX-License-Identifier: Apache-2.04*/56/**7* @fileoverview Constant declarations for common key codes.8*9* @see ../demos/keyhandler.html10*/1112goog.provide('goog.events.KeyCodes');1314goog.require('goog.userAgent');15goog.requireType('goog.events.BrowserEvent');161718/**19* Key codes for common characters.20*21* This list is not localized and therefore some of the key codes are not22* correct for non US keyboard layouts. See comments below.23*24* @enum {number}25*/26goog.events.KeyCodes = {27WIN_KEY_FF_LINUX: 0,28MAC_ENTER: 3,29BACKSPACE: 8,30TAB: 9,31NUM_CENTER: 12, // NUMLOCK on FF/Safari Mac32ENTER: 13,33SHIFT: 16,34CTRL: 17,35ALT: 18,36PAUSE: 19,37CAPS_LOCK: 20,38ESC: 27,39SPACE: 32,40PAGE_UP: 33, // also NUM_NORTH_EAST41PAGE_DOWN: 34, // also NUM_SOUTH_EAST42END: 35, // also NUM_SOUTH_WEST43HOME: 36, // also NUM_NORTH_WEST44LEFT: 37, // also NUM_WEST45UP: 38, // also NUM_NORTH46RIGHT: 39, // also NUM_EAST47DOWN: 40, // also NUM_SOUTH48PLUS_SIGN: 43, // NOT numpad plus49PRINT_SCREEN: 44,50INSERT: 45, // also NUM_INSERT51DELETE: 46, // also NUM_DELETE52ZERO: 48,53ONE: 49,54TWO: 50,55THREE: 51,56FOUR: 52,57FIVE: 53,58SIX: 54,59SEVEN: 55,60EIGHT: 56,61NINE: 57,62FF_SEMICOLON: 59, // Firefox (Gecko) fires this for semicolon instead of 18663FF_EQUALS: 61, // Firefox (Gecko) fires this for equals instead of 18764FF_DASH: 173, // Firefox (Gecko) fires this for dash instead of 18965// Firefox (Gecko) fires this for # on UK keyboards, rather than66// Shift+SINGLE_QUOTE.67FF_HASH: 163,68// Firefox (Gecko) fires this for ' (:) on JP keyboards, rather than69// SINGLE_QUOTE (US keyboard layout) or SEMICOLON (JP keyboard layout in70// chrome)71FF_JP_QUOTE: 58,72QUESTION_MARK: 63, // needs localization73AT_SIGN: 64,74A: 65,75B: 66,76C: 67,77D: 68,78E: 69,79F: 70,80G: 71,81H: 72,82I: 73,83J: 74,84K: 75,85L: 76,86M: 77,87N: 78,88O: 79,89P: 80,90Q: 81,91R: 82,92S: 83,93T: 84,94U: 85,95V: 86,96W: 87,97X: 88,98Y: 89,99Z: 90,100META: 91, // WIN_KEY_LEFT101WIN_KEY_RIGHT: 92,102CONTEXT_MENU: 93,103NUM_ZERO: 96,104NUM_ONE: 97,105NUM_TWO: 98,106NUM_THREE: 99,107NUM_FOUR: 100,108NUM_FIVE: 101,109NUM_SIX: 102,110NUM_SEVEN: 103,111NUM_EIGHT: 104,112NUM_NINE: 105,113NUM_MULTIPLY: 106,114NUM_PLUS: 107,115NUM_MINUS: 109,116NUM_PERIOD: 110,117NUM_DIVISION: 111,118F1: 112,119F2: 113,120F3: 114,121F4: 115,122F5: 116,123F6: 117,124F7: 118,125F8: 119,126F9: 120,127F10: 121,128F11: 122,129F12: 123,130NUMLOCK: 144,131SCROLL_LOCK: 145,132133// OS-specific media keys like volume controls and browser controls.134FIRST_MEDIA_KEY: 166,135LAST_MEDIA_KEY: 183,136137SEMICOLON: 186, // needs localization138DASH: 189, // needs localization139EQUALS: 187, // needs localization140COMMA: 188, // needs localization141PERIOD: 190, // needs localization142SLASH: 191, // needs localization143APOSTROPHE: 192, // needs localization144TILDE: 192, // needs localization145SINGLE_QUOTE: 222, // needs localization146OPEN_SQUARE_BRACKET: 219, // needs localization147BACKSLASH: 220, // needs localization148CLOSE_SQUARE_BRACKET: 221, // needs localization149WIN_KEY: 224,150MAC_FF_META:151224, // Firefox (Gecko) fires this for the meta key instead of 91152MAC_WK_CMD_LEFT: 91, // WebKit Left Command key fired, same as META153MAC_WK_CMD_RIGHT: 93, // WebKit Right Command key fired, different from META154WIN_IME: 229,155156// "Reserved for future use". Some programs (e.g. the SlingPlayer 2.4 ActiveX157// control) fire this as a hacky way to disable screensavers.158VK_NONAME: 252,159160// We've seen users whose machines fire this keycode at regular one161// second intervals. The common thread among these users is that162// they're all using Dell Inspiron laptops, so we suspect that this163// indicates a hardware/bios problem.164// http://en.community.dell.com/support-forums/laptop/f/3518/p/19285957/19523128.aspx165PHANTOM: 255166};167168169/**170* Returns false if the event does not contain a text modifying key.171*172* When it returns true, the event might be text modifying. It is infeasible to173* say for sure because of the many different keyboard layouts, so this method174* errs on the side of assuming a key event is text-modifiable if we cannot be175* certain it is not. As an example, it will return true for ctrl+a, though in176* many standard keyboard layouts that key combination would mean "select all",177* and not actually modify the text.178*179* @param {goog.events.BrowserEvent} e A key event.180* @return {boolean} Whether it's a text modifying key.181*/182goog.events.KeyCodes.isTextModifyingKeyEvent = function(e) {183'use strict';184if (e.altKey && !e.ctrlKey || e.metaKey ||185// Function keys don't generate text186e.keyCode >= goog.events.KeyCodes.F1 &&187e.keyCode <= goog.events.KeyCodes.F12) {188return false;189}190191if (goog.events.KeyCodes.isCharacterKey(e.keyCode)) {192return true;193}194195switch (e.keyCode) {196// The following keys are quite harmless, even in combination with197// CTRL, ALT or SHIFT.198case goog.events.KeyCodes.ALT:199case goog.events.KeyCodes.CAPS_LOCK:200case goog.events.KeyCodes.CONTEXT_MENU:201case goog.events.KeyCodes.CTRL:202case goog.events.KeyCodes.DOWN:203case goog.events.KeyCodes.END:204case goog.events.KeyCodes.ESC:205case goog.events.KeyCodes.HOME:206case goog.events.KeyCodes.INSERT:207case goog.events.KeyCodes.LEFT:208case goog.events.KeyCodes.MAC_FF_META:209case goog.events.KeyCodes.META:210case goog.events.KeyCodes.NUMLOCK:211case goog.events.KeyCodes.NUM_CENTER:212case goog.events.KeyCodes.PAGE_DOWN:213case goog.events.KeyCodes.PAGE_UP:214case goog.events.KeyCodes.PAUSE:215case goog.events.KeyCodes.PHANTOM:216case goog.events.KeyCodes.PRINT_SCREEN:217case goog.events.KeyCodes.RIGHT:218case goog.events.KeyCodes.SCROLL_LOCK:219case goog.events.KeyCodes.SHIFT:220case goog.events.KeyCodes.UP:221case goog.events.KeyCodes.VK_NONAME:222case goog.events.KeyCodes.WIN_KEY:223case goog.events.KeyCodes.WIN_KEY_RIGHT:224return false;225case goog.events.KeyCodes.WIN_KEY_FF_LINUX:226return !goog.userAgent.GECKO;227default:228return e.keyCode < goog.events.KeyCodes.FIRST_MEDIA_KEY ||229e.keyCode > goog.events.KeyCodes.LAST_MEDIA_KEY;230}231};232233234/**235* Returns true if the key fires a keypress event in the current browser.236*237* Accoridng to MSDN [1] IE only fires keypress events for the following keys:238* - Letters: A - Z (uppercase and lowercase)239* - Numerals: 0 - 9240* - Symbols: ! @ # $ % ^ & * ( ) _ - + = < [ ] { } , . / ? \ | ' ` " ~241* - System: ESC, SPACEBAR, ENTER242*243* That's not entirely correct though, for instance there's no distinction244* between upper and lower case letters.245*246* [1] http://msdn2.microsoft.com/en-us/library/ms536939(VS.85).aspx)247*248* Safari is similar to IE, but does not fire keypress for ESC.249*250* Additionally, IE6 does not fire keydown or keypress events for letters when251* the control or alt keys are held down and the shift key is not. IE7 does252* fire keydown in these cases, though, but not keypress.253*254* @param {number} keyCode A key code.255* @param {number=} opt_heldKeyCode Key code of a currently-held key.256* @param {boolean=} opt_shiftKey Whether the shift key is held down.257* @param {boolean=} opt_ctrlKey Whether the control key is held down.258* @param {boolean=} opt_altKey Whether the alt key is held down.259* @param {boolean=} opt_metaKey Whether the meta key is held down.260* @return {boolean} Whether it's a key that fires a keypress event.261*/262goog.events.KeyCodes.firesKeyPressEvent = function(263keyCode, opt_heldKeyCode, opt_shiftKey, opt_ctrlKey, opt_altKey,264opt_metaKey) {265'use strict';266if (goog.userAgent.MAC && opt_altKey) {267return goog.events.KeyCodes.isCharacterKey(keyCode);268}269270// Alt but not AltGr which is represented as Alt+Ctrl.271if (opt_altKey && !opt_ctrlKey) {272return false;273}274275// Saves Ctrl or Alt + key for IE and WebKit 525+, which won't fire keypress.276// WebKit prior to 525 won't get this far so no need to check the user agent.277// Gecko doesn't need to use the held key for modifiers, it just checks the278// ctrl/meta/alt/shiftKey fields.279if (!goog.userAgent.GECKO) {280if (typeof opt_heldKeyCode === 'number') {281opt_heldKeyCode = goog.events.KeyCodes.normalizeKeyCode(opt_heldKeyCode);282}283var heldKeyIsModifier = opt_heldKeyCode == goog.events.KeyCodes.CTRL ||284opt_heldKeyCode == goog.events.KeyCodes.ALT ||285goog.userAgent.MAC && opt_heldKeyCode == goog.events.KeyCodes.META;286// The Shift key blocks keypresses on Mac iff accompanied by another287// modifier.288var modifiedShiftKey = opt_heldKeyCode == goog.events.KeyCodes.SHIFT &&289(opt_ctrlKey || opt_metaKey);290if ((!opt_shiftKey || goog.userAgent.MAC) && heldKeyIsModifier ||291goog.userAgent.MAC && modifiedShiftKey) {292return false;293}294}295296// Some keys with Ctrl/Shift do not issue keypress in WEBKIT.297if ((goog.userAgent.WEBKIT || goog.userAgent.EDGE) && opt_ctrlKey &&298opt_shiftKey) {299switch (keyCode) {300case goog.events.KeyCodes.BACKSLASH:301case goog.events.KeyCodes.OPEN_SQUARE_BRACKET:302case goog.events.KeyCodes.CLOSE_SQUARE_BRACKET:303case goog.events.KeyCodes.TILDE:304case goog.events.KeyCodes.SEMICOLON:305case goog.events.KeyCodes.DASH:306case goog.events.KeyCodes.EQUALS:307case goog.events.KeyCodes.COMMA:308case goog.events.KeyCodes.PERIOD:309case goog.events.KeyCodes.SLASH:310case goog.events.KeyCodes.APOSTROPHE:311case goog.events.KeyCodes.SINGLE_QUOTE:312return false;313}314}315316// When Ctrl+<somekey> is held in IE, it only fires a keypress once, but it317// continues to fire keydown events as the event repeats.318if (goog.userAgent.IE && opt_ctrlKey && opt_heldKeyCode == keyCode) {319return false;320}321322switch (keyCode) {323case goog.events.KeyCodes.ENTER:324if (goog.userAgent.GECKO) {325// Only Enter, Shift + Enter, Ctrl + Enter causes keypress event on326// Firefox.327if (opt_metaKey || opt_altKey) {328return false;329}330return !(opt_shiftKey && opt_ctrlKey);331} else {332return true;333}334case goog.events.KeyCodes.ESC:335return !(336goog.userAgent.WEBKIT || goog.userAgent.EDGE || goog.userAgent.GECKO);337}338339// Gecko won't fire a keypress event even when the key is a character key if340// ctrl, meta or alt are pressed. In all other cases, a keypress event is341// only fired when the key is a character.342if (goog.userAgent.GECKO && (opt_ctrlKey || opt_altKey || opt_metaKey)) {343return false;344} else {345return goog.events.KeyCodes.isCharacterKey(keyCode);346}347};348349350/**351* Returns true if the key produces a character.352* This does not cover characters on non-US keyboards (Russian, Hebrew, etc.).353*354* @param {number} keyCode A key code.355* @return {boolean} Whether it's a character key.356*/357goog.events.KeyCodes.isCharacterKey = function(keyCode) {358'use strict';359if (keyCode >= goog.events.KeyCodes.ZERO &&360keyCode <= goog.events.KeyCodes.NINE) {361return true;362}363364if (keyCode >= goog.events.KeyCodes.NUM_ZERO &&365keyCode <= goog.events.KeyCodes.NUM_MULTIPLY) {366return true;367}368369if (keyCode >= goog.events.KeyCodes.A && keyCode <= goog.events.KeyCodes.Z) {370return true;371}372373// Safari sends zero key code for non-latin characters.374if ((goog.userAgent.WEBKIT || goog.userAgent.EDGE) && keyCode == 0) {375return true;376}377378switch (keyCode) {379case goog.events.KeyCodes.SPACE:380case goog.events.KeyCodes.PLUS_SIGN:381case goog.events.KeyCodes.QUESTION_MARK:382case goog.events.KeyCodes.AT_SIGN:383case goog.events.KeyCodes.NUM_PLUS:384case goog.events.KeyCodes.NUM_MINUS:385case goog.events.KeyCodes.NUM_PERIOD:386case goog.events.KeyCodes.NUM_DIVISION:387case goog.events.KeyCodes.SEMICOLON:388case goog.events.KeyCodes.FF_SEMICOLON:389case goog.events.KeyCodes.DASH:390case goog.events.KeyCodes.EQUALS:391case goog.events.KeyCodes.FF_EQUALS:392case goog.events.KeyCodes.COMMA:393case goog.events.KeyCodes.PERIOD:394case goog.events.KeyCodes.SLASH:395case goog.events.KeyCodes.APOSTROPHE:396case goog.events.KeyCodes.SINGLE_QUOTE:397case goog.events.KeyCodes.OPEN_SQUARE_BRACKET:398case goog.events.KeyCodes.BACKSLASH:399case goog.events.KeyCodes.CLOSE_SQUARE_BRACKET:400case goog.events.KeyCodes.FF_HASH:401case goog.events.KeyCodes.FF_JP_QUOTE:402return true;403case goog.events.KeyCodes.FF_DASH:404return goog.userAgent.GECKO;405default:406return false;407}408};409410411/**412* Normalizes key codes from OS/Browser-specific value to the general one.413* @param {number} keyCode The native key code.414* @return {number} The normalized key code.415*/416goog.events.KeyCodes.normalizeKeyCode = function(keyCode) {417'use strict';418if (goog.userAgent.GECKO) {419return goog.events.KeyCodes.normalizeGeckoKeyCode(keyCode);420} else if (goog.userAgent.MAC && goog.userAgent.WEBKIT) {421return goog.events.KeyCodes.normalizeMacWebKitKeyCode(keyCode);422} else {423return keyCode;424}425};426427428/**429* Normalizes key codes from their Gecko-specific value to the general one.430* @param {number} keyCode The native key code.431* @return {number} The normalized key code.432*/433goog.events.KeyCodes.normalizeGeckoKeyCode = function(keyCode) {434'use strict';435switch (keyCode) {436case goog.events.KeyCodes.FF_EQUALS:437return goog.events.KeyCodes.EQUALS;438case goog.events.KeyCodes.FF_SEMICOLON:439return goog.events.KeyCodes.SEMICOLON;440case goog.events.KeyCodes.FF_DASH:441return goog.events.KeyCodes.DASH;442case goog.events.KeyCodes.MAC_FF_META:443return goog.events.KeyCodes.META;444case goog.events.KeyCodes.WIN_KEY_FF_LINUX:445return goog.events.KeyCodes.WIN_KEY;446default:447return keyCode;448}449};450451452/**453* Normalizes key codes from their Mac WebKit-specific value to the general one.454* @param {number} keyCode The native key code.455* @return {number} The normalized key code.456*/457goog.events.KeyCodes.normalizeMacWebKitKeyCode = function(keyCode) {458'use strict';459switch (keyCode) {460case goog.events.KeyCodes.MAC_WK_CMD_RIGHT: // 93461return goog.events.KeyCodes.META; // 91462default:463return keyCode;464}465};466467468