PageRenderTime 17ms CodeModel.GetById 11ms app.highlight 2ms RepoModel.GetById 1ms app.codeStats 0ms

/AppKit/Platform/DOM/CPPlatformWindow+DOMKeys.j

http://github.com/cacaodev/cappuccino
Unknown | 232 lines | 216 code | 16 blank | 0 comment | 0 complexity | 2ccf73537c2bbf0c75f636b49dd304e6 MD5 | raw file
  1/*
  2 * CPPlatformWindow+DOMKeys.j
  3 * AppKit
  4 *
  5 * Created by Ross Boucher.
  6 * Copyright 2009, 280 North, Inc.
  7 *
  8 * This library is free software; you can redistribute it and/or
  9 * modify it under the terms of the GNU Lesser General Public
 10 * License as published by the Free Software Foundation; either
 11 * version 2.1 of the License, or (at your option) any later version.
 12 *
 13 * This library is distributed in the hope that it will be useful,
 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 16 * Lesser General Public License for more details.
 17 *
 18 * You should have received a copy of the GNU Lesser General Public
 19 * License along with this library; if not, write to the Free Software
 20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 21 */
 22
 23// Keycodes taken and modified from Google Closure, available under Apache 2 License
 24
 25CPKeyCodes = {
 26  BACKSPACE: 8,
 27  TAB: 9,
 28  NUM_CENTER: 12,
 29  ENTER: 13,
 30  SHIFT: 16,
 31  CTRL: 17,
 32  ALT: 18,
 33  PAUSE: 19,
 34  CAPS_LOCK: 20,
 35  ESC: 27,
 36  SPACE: 32,
 37  PAGE_UP: 33,     // also NUM_NORTH_EAST
 38  PAGE_DOWN: 34,   // also NUM_SOUTH_EAST
 39  END: 35,         // also NUM_SOUTH_WEST
 40  HOME: 36,        // also NUM_NORTH_WEST
 41  LEFT: 37,        // also NUM_WEST
 42  UP: 38,          // also NUM_NORTH
 43  RIGHT: 39,       // also NUM_EAST
 44  DOWN: 40,        // also NUM_SOUTH
 45  PRINT_SCREEN: 44,
 46  INSERT: 45,      // also NUM_INSERT
 47  DELETE: 46,      // also NUM_DELETE
 48  ZERO: 48,
 49  ONE: 49,
 50  TWO: 50,
 51  THREE: 51,
 52  FOUR: 52,
 53  FIVE: 53,
 54  SIX: 54,
 55  SEVEN: 55,
 56  EIGHT: 56,
 57  NINE: 57,
 58  QUESTION_MARK: 63, // needs localization
 59  A: 65,
 60  B: 66,
 61  C: 67,
 62  D: 68,
 63  E: 69,
 64  F: 70,
 65  G: 71,
 66  H: 72,
 67  I: 73,
 68  J: 74,
 69  K: 75,
 70  L: 76,
 71  M: 77,
 72  N: 78,
 73  O: 79,
 74  P: 80,
 75  Q: 81,
 76  R: 82,
 77  S: 83,
 78  T: 84,
 79  U: 85,
 80  V: 86,
 81  W: 87,
 82  X: 88,
 83  Y: 89,
 84  Z: 90,
 85  META: 91,
 86  WEBKIT_RIGHT_META: 93,    // WebKit (on Mac at least) fires this for the right Command key
 87  CONTEXT_MENU: 93,
 88  NUM_ZERO: 96,
 89  NUM_ONE: 97,
 90  NUM_TWO: 98,
 91  NUM_THREE: 99,
 92  NUM_FOUR: 100,
 93  NUM_FIVE: 101,
 94  NUM_SIX: 102,
 95  NUM_SEVEN: 103,
 96  NUM_EIGHT: 104,
 97  NUM_NINE: 105,
 98  NUM_MULTIPLY: 106,
 99  NUM_PLUS: 107,
100  NUM_MINUS: 109,
101  NUM_PERIOD: 110,
102  NUM_DIVISION: 111,
103  F1: 112,
104  F2: 113,
105  F3: 114,
106  F4: 115,
107  F5: 116,
108  F6: 117,
109  F7: 118,
110  F8: 119,
111  F9: 120,
112  F10: 121,
113  F11: 122,
114  F12: 123,
115  NUMLOCK: 144,
116  SEMICOLON: 186,            // needs localization
117  DASH: 189,                 // needs localization
118  EQUALS: 187,               // needs localization
119  COMMA: 188,                // needs localization
120  PERIOD: 190,               // needs localization
121  SLASH: 191,                // needs localization
122  APOSTROPHE: 192,           // needs localization
123  SINGLE_QUOTE: 222,         // needs localization
124  OPEN_SQUARE_BRACKET: 219,  // needs localization
125  BACKSLASH: 220,            // needs localization
126  CLOSE_SQUARE_BRACKET: 221, // needs localization
127  WIN_KEY: 224,
128  MAC_FF_META: 224,          // Firefox (Gecko) fires this for the meta key instead of 91
129  WIN_IME: 229
130};
131
132
133/*!
134 * Returns true if the key fires a keypress event in the current browser.
135 *
136 * Accoridng to MSDN [1] IE only fires keypress events for the following keys:
137 * - Letters: A - Z (uppercase and lowercase)
138 * - Numerals: 0 - 9
139 * - Symbols: ! @ # $ % ^ & * ( ) _ - + = < [ ] { } , . / ? \ | ' ` " ~
140 * - System: ESC, SPACEBAR, ENTER
141 *
142 * That's not entirely correct though, for instance there's no distinction
143 * between upper and lower case letters.
144 *
145 * [1] http://msdn2.microsoft.com/en-us/library/ms536939(VS.85).aspx)
146 *
147 * Safari is similar to IE, but does not fire keypress for ESC.
148 *
149 * Additionally, IE6 does not fire keydown or keypress events for letters when
150 * the control or alt keys are held down and the shift key is not. IE7 does
151 * fire keydown in these cases, though, but not keypress.
152 *
153 * @param keyCode A key code.
154 * @param opt_heldKeyCode Key code of a currently-held key.
155 * @param opt_shiftKey Whether the shift key is held down.
156 * @param opt_ctrlKey Whether the control key is held down.
157 * @param opt_altKey Whether the alt key is held down.
158 * @return Returns YES if it's a key that fires a keypress event.
159 */
160CPKeyCodes.firesKeyPressEvent = function(keyCode, opt_heldKeyCode, opt_shiftKey, opt_ctrlKey, opt_altKey)
161{
162    if (!CPFeatureIsCompatible(CPJavaScriptRemedialKeySupport))
163        return true;
164
165    if (CPBrowserIsOperatingSystem(CPMacOperatingSystem) && opt_altKey)
166        return CPKeyCodes.isCharacterKey(keyCode);
167
168    // Alt but not AltGr which is represented as Alt+Ctrl.
169    if (opt_altKey && !opt_ctrlKey)
170        return false;
171
172    // Saves Ctrl or Alt + key for IE7, which won't fire keypress.
173    if (CPBrowserIsEngine(CPInternetExplorerBrowserEngine) && !opt_shiftKey && (opt_ctrlKey || opt_altKey))
174        return false;
175
176    // When Ctrl+<somekey> is held in IE, it only fires a keypress once, but it
177    // continues to fire keydown events as the event repeats.
178    if (CPBrowserIsEngine(CPInternetExplorerBrowserEngine) && opt_ctrlKey && opt_heldKeyCode == keyCode)
179        return false;
180
181    switch (keyCode)
182    {
183        case CPKeyCodes.ENTER: return true;
184        case CPKeyCodes.ESC:   return !CPBrowserIsEngine(CPWebKitBrowserEngine);
185    }
186
187    return CPKeyCodes.isCharacterKey(keyCode);
188};
189
190
191/*!
192 * Test for whether or not a given keyCode represents a character key.
193 *
194 * @param keyCode A key code.
195 * @return Returns YES if the keyCode is a character key.
196 */
197CPKeyCodes.isCharacterKey = function(keyCode)
198{
199    if (keyCode >= CPKeyCodes.ZERO && keyCode <= CPKeyCodes.NINE)
200        return true;
201
202    if (keyCode >= CPKeyCodes.NUM_ZERO && keyCode <= CPKeyCodes.NUM_MULTIPLY)
203        return true;
204
205    if (keyCode >= CPKeyCodes.A && keyCode <= CPKeyCodes.Z)
206        return true;
207
208    switch (keyCode)
209    {
210        case CPKeyCodes.SPACE:
211        case CPKeyCodes.QUESTION_MARK:
212        case CPKeyCodes.NUM_PLUS:
213        case CPKeyCodes.NUM_MINUS:
214        case CPKeyCodes.NUM_PERIOD:
215        case CPKeyCodes.NUM_DIVISION:
216        case CPKeyCodes.SEMICOLON:
217        case CPKeyCodes.DASH:
218        case CPKeyCodes.EQUALS:
219        case CPKeyCodes.COMMA:
220        case CPKeyCodes.PERIOD:
221        case CPKeyCodes.SLASH:
222        case CPKeyCodes.APOSTROPHE:
223        case CPKeyCodes.SINGLE_QUOTE:
224        case CPKeyCodes.OPEN_SQUARE_BRACKET:
225        case CPKeyCodes.BACKSLASH:
226        case CPKeyCodes.CLOSE_SQUARE_BRACKET:
227            return true;
228
229        default:
230            return false;
231    }
232};