Path: blob/trunk/third_party/closure/goog/events/keycodes.js
2868 views
// Copyright 2006 The Closure Library Authors. All Rights Reserved.1//2// Licensed under the Apache License, Version 2.0 (the "License");3// you may not use this file except in compliance with the License.4// You may obtain a copy of the License at5//6// http://www.apache.org/licenses/LICENSE-2.07//8// Unless required by applicable law or agreed to in writing, software9// distributed under the License is distributed on an "AS-IS" BASIS,10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.11// See the License for the specific language governing permissions and12// limitations under the License.1314/**15* @fileoverview Constant declarations for common key codes.16*17* @author [email protected] (Emil A Eklund)18* @see ../demos/keyhandler.html19*/2021goog.provide('goog.events.KeyCodes');2223goog.require('goog.userAgent');2425goog.forwardDeclare('goog.events.BrowserEvent');262728/**29* Key codes for common characters.30*31* This list is not localized and therefore some of the key codes are not32* correct for non US keyboard layouts. See comments below.33*34* @enum {number}35*/36goog.events.KeyCodes = {37WIN_KEY_FF_LINUX: 0,38MAC_ENTER: 3,39BACKSPACE: 8,40TAB: 9,41NUM_CENTER: 12, // NUMLOCK on FF/Safari Mac42ENTER: 13,43SHIFT: 16,44CTRL: 17,45ALT: 18,46PAUSE: 19,47CAPS_LOCK: 20,48ESC: 27,49SPACE: 32,50PAGE_UP: 33, // also NUM_NORTH_EAST51PAGE_DOWN: 34, // also NUM_SOUTH_EAST52END: 35, // also NUM_SOUTH_WEST53HOME: 36, // also NUM_NORTH_WEST54LEFT: 37, // also NUM_WEST55UP: 38, // also NUM_NORTH56RIGHT: 39, // also NUM_EAST57DOWN: 40, // also NUM_SOUTH58PLUS_SIGN: 43, // NOT numpad plus59PRINT_SCREEN: 44,60INSERT: 45, // also NUM_INSERT61DELETE: 46, // also NUM_DELETE62ZERO: 48,63ONE: 49,64TWO: 50,65THREE: 51,66FOUR: 52,67FIVE: 53,68SIX: 54,69SEVEN: 55,70EIGHT: 56,71NINE: 57,72FF_SEMICOLON: 59, // Firefox (Gecko) fires this for semicolon instead of 18673FF_EQUALS: 61, // Firefox (Gecko) fires this for equals instead of 18774FF_DASH: 173, // Firefox (Gecko) fires this for dash instead of 18975QUESTION_MARK: 63, // needs localization76AT_SIGN: 64,77A: 65,78B: 66,79C: 67,80D: 68,81E: 69,82F: 70,83G: 71,84H: 72,85I: 73,86J: 74,87K: 75,88L: 76,89M: 77,90N: 78,91O: 79,92P: 80,93Q: 81,94R: 82,95S: 83,96T: 84,97U: 85,98V: 86,99W: 87,100X: 88,101Y: 89,102Z: 90,103META: 91, // WIN_KEY_LEFT104WIN_KEY_RIGHT: 92,105CONTEXT_MENU: 93,106NUM_ZERO: 96,107NUM_ONE: 97,108NUM_TWO: 98,109NUM_THREE: 99,110NUM_FOUR: 100,111NUM_FIVE: 101,112NUM_SIX: 102,113NUM_SEVEN: 103,114NUM_EIGHT: 104,115NUM_NINE: 105,116NUM_MULTIPLY: 106,117NUM_PLUS: 107,118NUM_MINUS: 109,119NUM_PERIOD: 110,120NUM_DIVISION: 111,121F1: 112,122F2: 113,123F3: 114,124F4: 115,125F5: 116,126F6: 117,127F7: 118,128F8: 119,129F9: 120,130F10: 121,131F11: 122,132F12: 123,133NUMLOCK: 144,134SCROLL_LOCK: 145,135136// OS-specific media keys like volume controls and browser controls.137FIRST_MEDIA_KEY: 166,138LAST_MEDIA_KEY: 183,139140SEMICOLON: 186, // needs localization141DASH: 189, // needs localization142EQUALS: 187, // needs localization143COMMA: 188, // needs localization144PERIOD: 190, // needs localization145SLASH: 191, // needs localization146APOSTROPHE: 192, // needs localization147TILDE: 192, // needs localization148SINGLE_QUOTE: 222, // needs localization149OPEN_SQUARE_BRACKET: 219, // needs localization150BACKSLASH: 220, // needs localization151CLOSE_SQUARE_BRACKET: 221, // needs localization152WIN_KEY: 224,153MAC_FF_META:154224, // Firefox (Gecko) fires this for the meta key instead of 91155MAC_WK_CMD_LEFT: 91, // WebKit Left Command key fired, same as META156MAC_WK_CMD_RIGHT: 93, // WebKit Right Command key fired, different from META157WIN_IME: 229,158159// "Reserved for future use". Some programs (e.g. the SlingPlayer 2.4 ActiveX160// control) fire this as a hacky way to disable screensavers.161VK_NONAME: 252,162163// We've seen users whose machines fire this keycode at regular one164// second intervals. The common thread among these users is that165// they're all using Dell Inspiron laptops, so we suspect that this166// indicates a hardware/bios problem.167// http://en.community.dell.com/support-forums/laptop/f/3518/p/19285957/19523128.aspx168PHANTOM: 255169};170171172/**173* Returns false if the event does not contain a text modifying key.174*175* When it returns true, the event might be text modifying. It is infeasible to176* say for sure because of the many different keyboard layouts, so this method177* errs on the side of assuming a key event is text-modifiable if we cannot be178* certain it is not. As an example, it will return true for ctrl+a, though in179* many standard keyboard layouts that key combination would mean "select all",180* and not actually modify the text.181*182* @param {goog.events.BrowserEvent} e A key event.183* @return {boolean} Whether it's a text modifying key.184*/185goog.events.KeyCodes.isTextModifyingKeyEvent = function(e) {186if (e.altKey && !e.ctrlKey || e.metaKey ||187// Function keys don't generate text188e.keyCode >= goog.events.KeyCodes.F1 &&189e.keyCode <= goog.events.KeyCodes.F12) {190return false;191}192193// The following keys are quite harmless, even in combination with194// CTRL, ALT or SHIFT.195switch (e.keyCode) {196case goog.events.KeyCodes.ALT:197case goog.events.KeyCodes.CAPS_LOCK:198case goog.events.KeyCodes.CONTEXT_MENU:199case goog.events.KeyCodes.CTRL:200case goog.events.KeyCodes.DOWN:201case goog.events.KeyCodes.END:202case goog.events.KeyCodes.ESC:203case goog.events.KeyCodes.HOME:204case goog.events.KeyCodes.INSERT:205case goog.events.KeyCodes.LEFT:206case goog.events.KeyCodes.MAC_FF_META:207case goog.events.KeyCodes.META:208case goog.events.KeyCodes.NUMLOCK:209case goog.events.KeyCodes.NUM_CENTER:210case goog.events.KeyCodes.PAGE_DOWN:211case goog.events.KeyCodes.PAGE_UP:212case goog.events.KeyCodes.PAUSE:213case goog.events.KeyCodes.PHANTOM:214case goog.events.KeyCodes.PRINT_SCREEN:215case goog.events.KeyCodes.RIGHT:216case goog.events.KeyCodes.SCROLL_LOCK:217case goog.events.KeyCodes.SHIFT:218case goog.events.KeyCodes.UP:219case goog.events.KeyCodes.VK_NONAME:220case goog.events.KeyCodes.WIN_KEY:221case goog.events.KeyCodes.WIN_KEY_RIGHT:222return false;223case goog.events.KeyCodes.WIN_KEY_FF_LINUX:224return !goog.userAgent.GECKO;225default:226return e.keyCode < goog.events.KeyCodes.FIRST_MEDIA_KEY ||227e.keyCode > goog.events.KeyCodes.LAST_MEDIA_KEY;228}229};230231232/**233* Returns true if the key fires a keypress event in the current browser.234*235* Accoridng to MSDN [1] IE only fires keypress events for the following keys:236* - Letters: A - Z (uppercase and lowercase)237* - Numerals: 0 - 9238* - Symbols: ! @ # $ % ^ & * ( ) _ - + = < [ ] { } , . / ? \ | ' ` " ~239* - System: ESC, SPACEBAR, ENTER240*241* That's not entirely correct though, for instance there's no distinction242* between upper and lower case letters.243*244* [1] http://msdn2.microsoft.com/en-us/library/ms536939(VS.85).aspx)245*246* Safari is similar to IE, but does not fire keypress for ESC.247*248* Additionally, IE6 does not fire keydown or keypress events for letters when249* the control or alt keys are held down and the shift key is not. IE7 does250* fire keydown in these cases, though, but not keypress.251*252* @param {number} keyCode A key code.253* @param {number=} opt_heldKeyCode Key code of a currently-held key.254* @param {boolean=} opt_shiftKey Whether the shift key is held down.255* @param {boolean=} opt_ctrlKey Whether the control key is held down.256* @param {boolean=} opt_altKey Whether the alt key is held down.257* @param {boolean=} opt_metaKey Whether the meta key is held down.258* @return {boolean} Whether it's a key that fires a keypress event.259*/260goog.events.KeyCodes.firesKeyPressEvent = function(261keyCode, opt_heldKeyCode, opt_shiftKey, opt_ctrlKey, opt_altKey,262opt_metaKey) {263if (!goog.userAgent.IE && !goog.userAgent.EDGE &&264!(goog.userAgent.WEBKIT && goog.userAgent.isVersionOrHigher('525'))) {265return true;266}267268if (goog.userAgent.MAC && opt_altKey) {269return goog.events.KeyCodes.isCharacterKey(keyCode);270}271272// Alt but not AltGr which is represented as Alt+Ctrl.273if (opt_altKey && !opt_ctrlKey) {274return false;275}276277// Saves Ctrl or Alt + key for IE and WebKit 525+, which won't fire keypress.278// Non-IE browsers and WebKit prior to 525 won't get this far so no need to279// check the user agent.280if (goog.isNumber(opt_heldKeyCode)) {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 another modifier.287var modifiedShiftKey = opt_heldKeyCode == goog.events.KeyCodes.SHIFT &&288(opt_ctrlKey || opt_metaKey);289if ((!opt_shiftKey || goog.userAgent.MAC) && heldKeyIsModifier ||290goog.userAgent.MAC && modifiedShiftKey) {291return false;292}293294// Some keys with Ctrl/Shift do not issue keypress in WEBKIT.295if ((goog.userAgent.WEBKIT || goog.userAgent.EDGE) && opt_ctrlKey &&296opt_shiftKey) {297switch (keyCode) {298case goog.events.KeyCodes.BACKSLASH:299case goog.events.KeyCodes.OPEN_SQUARE_BRACKET:300case goog.events.KeyCodes.CLOSE_SQUARE_BRACKET:301case goog.events.KeyCodes.TILDE:302case goog.events.KeyCodes.SEMICOLON:303case goog.events.KeyCodes.DASH:304case goog.events.KeyCodes.EQUALS:305case goog.events.KeyCodes.COMMA:306case goog.events.KeyCodes.PERIOD:307case goog.events.KeyCodes.SLASH:308case goog.events.KeyCodes.APOSTROPHE:309case goog.events.KeyCodes.SINGLE_QUOTE:310return false;311}312}313314// When Ctrl+<somekey> is held in IE, it only fires a keypress once, but it315// continues to fire keydown events as the event repeats.316if (goog.userAgent.IE && opt_ctrlKey && opt_heldKeyCode == keyCode) {317return false;318}319320switch (keyCode) {321case goog.events.KeyCodes.ENTER:322return true;323case goog.events.KeyCodes.ESC:324return !(goog.userAgent.WEBKIT || goog.userAgent.EDGE);325}326327return goog.events.KeyCodes.isCharacterKey(keyCode);328};329330331/**332* Returns true if the key produces a character.333* This does not cover characters on non-US keyboards (Russian, Hebrew, etc.).334*335* @param {number} keyCode A key code.336* @return {boolean} Whether it's a character key.337*/338goog.events.KeyCodes.isCharacterKey = function(keyCode) {339if (keyCode >= goog.events.KeyCodes.ZERO &&340keyCode <= goog.events.KeyCodes.NINE) {341return true;342}343344if (keyCode >= goog.events.KeyCodes.NUM_ZERO &&345keyCode <= goog.events.KeyCodes.NUM_MULTIPLY) {346return true;347}348349if (keyCode >= goog.events.KeyCodes.A && keyCode <= goog.events.KeyCodes.Z) {350return true;351}352353// Safari sends zero key code for non-latin characters.354if ((goog.userAgent.WEBKIT || goog.userAgent.EDGE) && keyCode == 0) {355return true;356}357358switch (keyCode) {359case goog.events.KeyCodes.SPACE:360case goog.events.KeyCodes.PLUS_SIGN:361case goog.events.KeyCodes.QUESTION_MARK:362case goog.events.KeyCodes.AT_SIGN:363case goog.events.KeyCodes.NUM_PLUS:364case goog.events.KeyCodes.NUM_MINUS:365case goog.events.KeyCodes.NUM_PERIOD:366case goog.events.KeyCodes.NUM_DIVISION:367case goog.events.KeyCodes.SEMICOLON:368case goog.events.KeyCodes.FF_SEMICOLON:369case goog.events.KeyCodes.DASH:370case goog.events.KeyCodes.EQUALS:371case goog.events.KeyCodes.FF_EQUALS:372case goog.events.KeyCodes.COMMA:373case goog.events.KeyCodes.PERIOD:374case goog.events.KeyCodes.SLASH:375case goog.events.KeyCodes.APOSTROPHE:376case goog.events.KeyCodes.SINGLE_QUOTE:377case goog.events.KeyCodes.OPEN_SQUARE_BRACKET:378case goog.events.KeyCodes.BACKSLASH:379case goog.events.KeyCodes.CLOSE_SQUARE_BRACKET:380return true;381default:382return false;383}384};385386387/**388* Normalizes key codes from OS/Browser-specific value to the general one.389* @param {number} keyCode The native key code.390* @return {number} The normalized key code.391*/392goog.events.KeyCodes.normalizeKeyCode = function(keyCode) {393if (goog.userAgent.GECKO) {394return goog.events.KeyCodes.normalizeGeckoKeyCode(keyCode);395} else if (goog.userAgent.MAC && goog.userAgent.WEBKIT) {396return goog.events.KeyCodes.normalizeMacWebKitKeyCode(keyCode);397} else {398return keyCode;399}400};401402403/**404* Normalizes key codes from their Gecko-specific value to the general one.405* @param {number} keyCode The native key code.406* @return {number} The normalized key code.407*/408goog.events.KeyCodes.normalizeGeckoKeyCode = function(keyCode) {409switch (keyCode) {410case goog.events.KeyCodes.FF_EQUALS:411return goog.events.KeyCodes.EQUALS;412case goog.events.KeyCodes.FF_SEMICOLON:413return goog.events.KeyCodes.SEMICOLON;414case goog.events.KeyCodes.FF_DASH:415return goog.events.KeyCodes.DASH;416case goog.events.KeyCodes.MAC_FF_META:417return goog.events.KeyCodes.META;418case goog.events.KeyCodes.WIN_KEY_FF_LINUX:419return goog.events.KeyCodes.WIN_KEY;420default:421return keyCode;422}423};424425426/**427* Normalizes key codes from their Mac WebKit-specific value to the general one.428* @param {number} keyCode The native key code.429* @return {number} The normalized key code.430*/431goog.events.KeyCodes.normalizeMacWebKitKeyCode = function(keyCode) {432switch (keyCode) {433case goog.events.KeyCodes.MAC_WK_CMD_RIGHT: // 93434return goog.events.KeyCodes.META; // 91435default:436return keyCode;437}438};439440441