// Copyright 2006 Google Inc. // All Rights Reserved // Redistribution and use in source and binary forms, with or without // modification, are permitted provided that the following conditions // are met: // // * Redistributions of source code must retain the above copyright // notice, this list of conditions and the following disclaimer. // * Redistributions in binary form must reproduce the above copyright // notice, this list of conditions and the following disclaimer in // the documentation and/or other materials provided with the // distribution. // // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE // COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, // BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT // LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN // ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE // POSSIBILITY OF SUCH DAMAGE. /* * @fileoverview Constant declarations for common key codes. * */ goog.provide('goog.events.KeyCodes'); goog.require('goog.events'); /** * Key codes for common characters. * * This list is not localized and therefor some of the key codes are not correct * for non US keyboard layouts. See comments below. * * @enum {number} */ goog.events.KeyCodes = { MAC_ENTER: 3, BACKSPACE: 8, TAB: 9, NUM_CENTER: 12, ENTER: 13, SHIFT: 16, CTRL: 17, ALT: 18, PAUSE: 19, CAPS_LOCK: 20, ESC: 27, SPACE: 32, PAGE_UP: 33, // also NUM_NORTH_EAST PAGE_DOWN: 34, // also NUM_SOUTH_EAST END: 35, // also NUM_SOUTH_WEST HOME: 36, // also NUM_NORTH_WEST LEFT: 37, // also NUM_WEST UP: 38, // also NUM_NORTH RIGHT: 39, // also NUM_EAST DOWN: 40, // also NUM_SOUTH PRINT_SCREEN: 44, INSERT: 45, // also NUM_INSERT DELETE: 46, // also NUM_DELETE ZERO: 48, ONE: 49, TWO: 50, THREE: 51, FOUR: 52, FIVE: 53, SIX: 54, SEVEN: 55, EIGHT: 56, NINE: 57, QUESTION_MARK: 63, // needs localization A: 65, B: 66, C: 67, D: 68, E: 69, F: 70, G: 71, H: 72, I: 73, J: 74, K: 75, L: 76, M: 77, N: 78, O: 79, P: 80, Q: 81, R: 82, S: 83, T: 84, U: 85, V: 86, W: 87, X: 88, Y: 89, Z: 90, META: 91, CONTEXT_MENU: 93, NUM_ZERO: 96, NUM_ONE: 97, NUM_TWO: 98, NUM_THREE: 99, NUM_FOUR: 100, NUM_FIVE: 101, NUM_SIX: 102, NUM_SEVEN: 103, NUM_EIGHT: 104, NUM_NINE: 105, NUM_MULTIPLY: 106, NUM_PLUS: 107, NUM_MINUS: 109, NUM_PERIOD: 110, NUM_DIVISION: 111, F1: 112, F2: 113, F3: 114, F4: 115, F5: 116, F6: 117, F7: 118, F8: 119, F9: 120, F10: 121, F11: 122, F12: 123, NUMLOCK: 144, SEMICOLON: 186, // needs localization DASH: 189, // needs localization EQUALS: 187, // needs localization COMMA: 188, // needs localization PERIOD: 190, // needs localization SLASH: 191, // needs localization APOSTROPHE: 192, // needs localization SINGLE_QUOTE: 222, // needs localization OPEN_SQUARE_BRACKET: 219, // needs localization BACKSLASH: 220, // needs localization CLOSE_SQUARE_BRACKET: 221, // needs localization WIN_KEY: 224, MAC_FF_META: 224, // Firefox (Gecko) fires this for the meta key instead of 91 WIN_IME: 229 }; /** * Returns true if the event contains a text modifying key * @param {goog.events.BrowserEvent} e A key event. * @return {boolean} Whether it's a text modifying key. */ goog.events.KeyCodes.isTextModifyingKeyEvent = function(e) { if (e.altKey && !e.ctrlKey || e.metaKey || // Function keys don't generate text e.keyCode >= goog.events.KeyCodes.F1 && e.keyCode <= goog.events.KeyCodes.F12) { return false; } // The following keys are quite harmless, even in combination with // CTRL, ALT or SHIFT. switch (e.keyCode) { case goog.events.KeyCodes.ALT: case goog.events.KeyCodes.SHIFT: case goog.events.KeyCodes.CTRL: case goog.events.KeyCodes.PAUSE: case goog.events.KeyCodes.CAPS_LOCK: case goog.events.KeyCodes.ESC: case goog.events.KeyCodes.PAGE_UP: case goog.events.KeyCodes.PAGE_DOWN: case goog.events.KeyCodes.HOME: case goog.events.KeyCodes.END: case goog.events.KeyCodes.LEFT: case goog.events.KeyCodes.RIGHT: case goog.events.KeyCodes.UP: case goog.events.KeyCodes.DOWN: case goog.events.KeyCodes.INSERT: case goog.events.KeyCodes.NUMLOCK: case goog.events.KeyCodes.CONTEXT_MENU: case goog.events.KeyCodes.PRINT_SCREEN: return false; default: return true; } }; /** * Returns true if the key fires a keypress event in the current browser. * * Accoridng to MSDN [1] IE only fires keypress events for the following keys: * - Letters: A - Z (uppercase and lowercase) * - Numerals: 0 - 9 * - Symbols: ! @ # $ % ^ & * ( ) _ - + = < [ ] { } , . / ? \ | ' ` " ~ * - System: ESC, SPACEBAR, ENTER * * That's not entirely correct though, for instance there's no distinction * between upper and lower case letters. * * [1] http://msdn2.microsoft.com/en-us/library/ms536939(VS.85).aspx) * * Safari is similar to IE, but does not fire keypress for ESC. * * Additionally, IE6 does not fire keydown or keypress events for letters when * the control or alt keys are held down and the shift key is not. IE7 does * fire keydown in these cases, though, but not keypress. * * @param {number} keyCode A key code. * @param {number} opt_heldKeyCode Key code of a currently-held key. * @param {boolean} opt_shiftKey Whether the shift key is held down. * @return {boolean} Whether it's a key that fires a keypress event. */ goog.events.KeyCodes.firesKeyPressEvent = function(keyCode, opt_heldKeyCode, opt_shiftKey) { if (!goog.userAgent.IE && !(goog.userAgent.WEBKIT && goog.userAgent.isVersion('525'))) { return true; } // Saves Ctrl or Alt + key for IE7, which won't fire keypress. if (goog.userAgent.IE && !opt_shiftKey && (opt_heldKeyCode == goog.events.KeyCodes.CTRL || opt_heldKeyCode == goog.events.KeyCodes.ALT)) { return false; } if (keyCode >= goog.events.KeyCodes.ZERO && keyCode <= goog.events.KeyCodes.NINE) { return true; } if (keyCode >= goog.events.KeyCodes.NUM_ZERO && keyCode <= goog.events.KeyCodes.NUM_MULTIPLY) { return true; } if (keyCode >= goog.events.KeyCodes.A && keyCode <= goog.events.KeyCodes.Z) { return true; } if (keyCode == goog.events.KeyCodes.ESC && goog.userAgent.WEBKIT) { return false; } switch (keyCode) { case goog.events.KeyCodes.ENTER: case goog.events.KeyCodes.ESC: case goog.events.KeyCodes.SPACE: case goog.events.KeyCodes.QUESTION_MARK: case goog.events.KeyCodes.NUM_PLUS: case goog.events.KeyCodes.NUM_MINUS: case goog.events.KeyCodes.NUM_PERIOD: case goog.events.KeyCodes.NUM_DIVISION: case goog.events.KeyCodes.SEMICOLON: case goog.events.KeyCodes.DASH: case goog.events.KeyCodes.EQUALS: case goog.events.KeyCodes.COMMA: case goog.events.KeyCodes.PERIOD: case goog.events.KeyCodes.SLASH: case goog.events.KeyCodes.APOSTROPHE: case goog.events.KeyCodes.SINGLE_QUOTE: case goog.events.KeyCodes.OPEN_SQUARE_BRACKET: case goog.events.KeyCodes.BACKSLASH: case goog.events.KeyCodes.CLOSE_SQUARE_BRACKET: return true; default: return false; } }; /** * Returns true if the key produces a character. * * @param {number} keyCode A key code. * @return {boolean} Whether it's a character key. */ goog.events.KeyCodes.isCharacterKey = function(keyCode) { if (keyCode >= goog.events.KeyCodes.ZERO && keyCode <= goog.events.KeyCodes.NINE) { return true; } if (keyCode >= goog.events.KeyCodes.NUM_ZERO && keyCode <= goog.events.KeyCodes.NUM_MULTIPLY) { return true; } if (keyCode >= goog.events.KeyCodes.A && keyCode <= goog.events.KeyCodes.Z) { return true; } switch (keyCode) { case goog.events.KeyCodes.SPACE: case goog.events.KeyCodes.QUESTION_MARK: case goog.events.KeyCodes.NUM_PLUS: case goog.events.KeyCodes.NUM_MINUS: case goog.events.KeyCodes.NUM_PERIOD: case goog.events.KeyCodes.NUM_DIVISION: case goog.events.KeyCodes.SEMICOLON: case goog.events.KeyCodes.DASH: case goog.events.KeyCodes.EQUALS: case goog.events.KeyCodes.COMMA: case goog.events.KeyCodes.PERIOD: case goog.events.KeyCodes.SLASH: case goog.events.KeyCodes.APOSTROPHE: case goog.events.KeyCodes.SINGLE_QUOTE: case goog.events.KeyCodes.OPEN_SQUARE_BRACKET: case goog.events.KeyCodes.BACKSLASH: case goog.events.KeyCodes.CLOSE_SQUARE_BRACKET: return true; default: return false; } };