Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | |
| 2 // for details. All rights reserved. Use of this source code is governed by a | |
| 3 // BSD-style license that can be found in the LICENSE file. | |
| 4 | |
| 5 part of html; | |
| 6 | |
| 7 /** | |
| 8 * Works with KeyboardEvent and KeyEvent to determine how to expose information | |
| 9 * about Key(board)Events. This class functions like an EventListenerList, and | |
| 10 * provides a consistent interface for the Dart | |
| 11 * user, despite the fact that a multitude of browsers that have varying | |
| 12 * keyboard default behavior. | |
| 13 * | |
| 14 * This class is very much a work in progress, and we'd love to get information | |
| 15 * on how we can make this class work with as many international keyboards as | |
| 16 * possible. Bugs welcome! | |
| 17 */ | |
| 18 class KeyboardEventController { | |
| 19 // This code inspired by Closure's KeyHandling library. | |
| 20 // http://closure-library.googlecode.com/svn/docs/closure_goog_events_keyhandl er.js.source.html | |
| 21 | |
| 22 /** | |
| 23 * The set of keys that have been pressed down without seeing their | |
| 24 * corresponding keyup event. | |
| 25 */ | |
| 26 List<KeyboardEvent> keyDownList; | |
| 27 | |
| 28 /** The set of functions that wish to be notified when a KeyEvent happens. */ | |
| 29 List<Function> _callbacks; | |
| 30 | |
| 31 /** The type of KeyEvent we are tracking (keyup, keydown, keypress). */ | |
| 32 String _type; | |
| 33 | |
| 34 /** The element we are watching for events to happen on. */ | |
| 35 EventTarget _target; | |
| 36 | |
| 37 // The distance to shift from upper case alphabet Roman letters to lower case. | |
| 38 const int _ROMAN_ALPHABET_OFFSET = "a".charCodes[0] - "A".charCodes[0]; | |
| 39 | |
| 40 // Instance members referring to the internal event handlers because closures | |
| 41 // are not hashable. | |
| 42 var _keyUp, _keyDown, _keyPress; | |
| 43 | |
| 44 /** | |
| 45 * An enumeration of key identifiers currently part of the W3C draft for DOM3 | |
| 46 * and their mappings to keyCodes. | |
| 47 * http://www.w3.org/TR/DOM-Level-3-Events/keyset.html#KeySet-Set | |
| 48 */ | |
| 49 static Map<String, int> _keyIdentifier = { | |
| 50 'Up': KeyCode.UP, | |
| 51 'Down': KeyCode.DOWN, | |
| 52 'Left': KeyCode.LEFT, | |
| 53 'Right': KeyCode.RIGHT, | |
| 54 'Enter': KeyCode.ENTER, | |
| 55 'F1': KeyCode.F1, | |
| 56 'F2': KeyCode.F2, | |
| 57 'F3': KeyCode.F3, | |
| 58 'F4': KeyCode.F4, | |
| 59 'F5': KeyCode.F5, | |
| 60 'F6': KeyCode.F6, | |
| 61 'F7': KeyCode.F7, | |
| 62 'F8': KeyCode.F8, | |
| 63 'F9': KeyCode.F9, | |
| 64 'F10': KeyCode.F10, | |
| 65 'F11': KeyCode.F11, | |
| 66 'F12': KeyCode.F12, | |
| 67 'U+007F': KeyCode.DELETE, | |
| 68 'Home': KeyCode.HOME, | |
| 69 'End': KeyCode.END, | |
| 70 'PageUp': KeyCode.PAGE_UP, | |
| 71 'PageDown': KeyCode.PAGE_DOWN, | |
| 72 'Insert': KeyCode.INSERT | |
| 73 }; | |
| 74 | |
| 75 /** Named constructor to add an onKeyPress event listener to our handler. */ | |
| 76 KeyboardEventController.keypress(EventTarget target) { | |
| 77 _KeyboardEventController(target, 'keypress'); | |
| 78 } | |
| 79 | |
| 80 /** Named constructor to add an onKeyUp event listener to our handler. */ | |
| 81 KeyboardEventController.keyup(EventTarget target) { | |
| 82 _KeyboardEventController(target, 'keyup'); | |
| 83 } | |
| 84 | |
| 85 /** Named constructor to add an onKeyDown event listener to our handler. */ | |
| 86 KeyboardEventController.keydown(EventTarget target) { | |
| 87 _KeyboardEventController(target, 'keydown'); | |
| 88 } | |
| 89 | |
| 90 /** | |
| 91 * General constructor, performs basic initialization for our improved | |
| 92 * KeyboardEvent controller. | |
| 93 */ | |
| 94 _KeyboardEventController(EventTarget target, String type) { | |
| 95 _callbacks = []; | |
| 96 _type = type; | |
| 97 _target = target; | |
| 98 _initializeAllEventListeners(); | |
|
blois
2012/11/30 00:21:02
No need to do this in constructor, since it occurs
| |
| 99 } | |
| 100 | |
| 101 /** | |
| 102 * Hook up all event listeners under the covers so we can estimate keycodes | |
| 103 * and charcodes when they are not provided. | |
| 104 */ | |
| 105 void _initializeAllEventListeners() { | |
| 106 keyDownList = []; | |
| 107 _keyDown = processKeyDown; | |
|
blois
2012/11/30 00:21:02
Would be safer to assign these in the constructor,
| |
| 108 _keyUp = processKeyUp; | |
| 109 _keyPress = processKeyPress; | |
| 110 _target.on.keyDown.add(_keyDown, true); | |
| 111 _target.on.keyPress.add(_keyPress, true); | |
| 112 _target.on.keyUp.add(_keyUp, true); | |
| 113 } | |
| 114 | |
| 115 /** Add a callback that wishes to be notified when a KeyEvent occurs. */ | |
| 116 void add(void callback(KeyEvent)) { | |
| 117 if (_callbacks.length == 0) { | |
| 118 _initializeAllEventListeners(); | |
| 119 } | |
| 120 _callbacks.add(callback); | |
| 121 } | |
| 122 | |
| 123 /** | |
| 124 * Notify all callback listeners that a KeyEvent of the relevant type has | |
| 125 * occurred. | |
| 126 */ | |
| 127 bool _dispatch(KeyEvent event) { | |
| 128 if (event.type == _type) { | |
| 129 // Make a copy of the listeners in case a callback gets removed while | |
| 130 // dispatching from the list. | |
| 131 List callbacksCopy = new List.from(_callbacks); | |
| 132 for(var callback in callbacksCopy) { | |
| 133 callback(event); | |
| 134 } | |
| 135 } | |
| 136 } | |
| 137 | |
| 138 /** Remove the given callback from the listeners list. */ | |
| 139 void remove(void callback(KeyEvent)) { | |
| 140 var index = _callbacks.indexOf(callback); | |
| 141 if (index != -1) { | |
| 142 _callbacks.removeAt(index); | |
| 143 } | |
| 144 if (_callbacks.length == 0) { | |
| 145 // If we have no listeners, don't bother keeping track of keypresses. | |
| 146 _target.on.keyDown.remove(_keyDown); | |
| 147 _target.on.keyPress.remove(_keyPress); | |
| 148 _target.on.keyUp.remove(_keyUp); | |
| 149 } | |
| 150 } | |
| 151 | |
| 152 /** Determine if caps lock is one of the currently depressed keys. */ | |
| 153 bool get _capsLockOn() => | |
| 154 keyDownList.some((var element) => element.keyCode == KeyCode.CAPS_LOCK); | |
| 155 | |
| 156 /** | |
| 157 * Given the previously recorded keydown key codes, see if we can determine | |
| 158 * the keycode of this keypress [event]. (Generally browsers only provide | |
| 159 * charCode information for keypress events, but with a little | |
| 160 * reverse-engineering, we can also determine the keyCode.) Returns | |
| 161 * KeyCode.UNKNOWN if the keycode could not be determined. | |
| 162 */ | |
| 163 int _determineKeyCodeForKeypress(KeyboardEvent event) { | |
| 164 // Note: This function is a work in progress. We'll expand this function | |
| 165 // once we get more information about other keyboards. | |
| 166 for (var prevEvent in keyDownList) { | |
| 167 if (prevEvent._shadowCharCode == event.charCode) { | |
| 168 return prevEvent.keyCode; | |
| 169 } | |
| 170 if ((event.shiftKey || _capsLockOn) && event.charCode >= "A".charCodes[0] | |
| 171 && event.charCode <= "Z".charCodes[0] && event.charCode + | |
| 172 _ROMAN_ALPHABET_OFFSET == prevEvent._shadowCharCode) { | |
| 173 return prevEvent.keyCode; | |
| 174 } | |
| 175 } | |
| 176 return KeyCode.UNKNOWN; | |
| 177 } | |
| 178 | |
| 179 /** | |
| 180 * Given the charater code returned from a keyDown [event], try to ascertain | |
| 181 * and return the corresponding charCode for the character that was pressed. | |
| 182 * This information is not shown to the user, but used to help polyfill | |
| 183 * keypress events. | |
| 184 */ | |
| 185 int _findCharCodeKeyDown(KeyboardEvent event) { | |
| 186 if (event.keyLocation == 3) { // Numpad keys. | |
| 187 switch (event.keyCode) { | |
| 188 case KeyCode.NUM_ZERO: | |
| 189 // Even though this function returns _charCodes_, for some cases the | |
| 190 // KeyCode == the charCode we want, in which case we use the keycode | |
| 191 // constant for readability. | |
| 192 return KeyCode.ZERO; | |
| 193 case KeyCode.NUM_ONE: | |
| 194 return KeyCode.ONE; | |
| 195 case KeyCode.NUM_TWO: | |
| 196 return KeyCode.TWO; | |
| 197 case KeyCode.NUM_THREE: | |
| 198 return KeyCode.THREE; | |
| 199 case KeyCode.NUM_FOUR: | |
| 200 return KeyCode.FOUR; | |
| 201 case KeyCode.NUM_FIVE: | |
| 202 return KeyCode.FIVE; | |
| 203 case KeyCode.NUM_SIX: | |
| 204 return KeyCode.SIX; | |
| 205 case KeyCode.NUM_SEVEN: | |
| 206 return KeyCode.SEVEN; | |
| 207 case KeyCode.NUM_EIGHT: | |
| 208 return KeyCode.EIGHT; | |
| 209 case KeyCode.NUM_NINE: | |
| 210 return KeyCode.NINE; | |
| 211 case KeyCode.NUM_MULTIPLY: | |
| 212 return 42; // Char code for * | |
| 213 case KeyCode.NUM_PLUS: | |
| 214 return 43; // + | |
| 215 case KeyCode.NUM_MINUS: | |
| 216 return 45; // - | |
| 217 case KeyCode.NUM_PERIOD: | |
| 218 return 46; // . | |
| 219 case KeyCode.NUM_DIVISION: | |
| 220 return 47; // / | |
| 221 } | |
| 222 } else if (event.keyCode >= 65 && event.keyCode <= 90) { | |
| 223 // Set the "char code" for key down as the lower case letter. Again, this | |
| 224 // will not show up for the user, but will be helpful in estimating | |
| 225 // keyCode locations and other information during the keyPress event. | |
| 226 return event.keyCode + _ROMAN_ALPHABET_OFFSET; | |
| 227 } | |
| 228 switch(event.keyCode) { | |
| 229 case KeyCode.SEMICOLON: | |
| 230 return KeyCode.FF_SEMICOLON; | |
| 231 case KeyCode.EQUALS: | |
| 232 return KeyCode.FF_EQUALS; | |
| 233 case KeyCode.COMMA: | |
| 234 return 44; // Ascii value for , | |
| 235 case KeyCode.DASH: | |
| 236 return 45; // - | |
| 237 case KeyCode.PERIOD: | |
| 238 return 46; // . | |
| 239 case KeyCode.SLASH: | |
| 240 return 47; // / | |
| 241 case KeyCode.APOSTROPHE: | |
| 242 return 96; // ` | |
| 243 case KeyCode.OPEN_SQUARE_BRACKET: | |
| 244 return 91; // [ | |
| 245 case KeyCode.BACKSLASH: | |
| 246 return 92; // \ | |
| 247 case KeyCode.CLOSE_SQUARE_BRACKET: | |
| 248 return 93; // ] | |
| 249 case KeyCode.SINGLE_QUOTE: | |
| 250 return 39; // ' | |
| 251 } | |
| 252 return event.keyCode; | |
| 253 } | |
| 254 | |
| 255 /** | |
| 256 * Returns true if the key fires a keypress event in the current browser. | |
| 257 */ | |
| 258 bool _firesKeyPressEvent(KeyEvent event) { | |
| 259 if (!_Device.isIE && !_Device.isWebKit) { | |
| 260 return true; | |
| 261 } | |
| 262 | |
| 263 if (_Device.userAgent.contains('Mac') && event.altKey) { | |
| 264 return KeyCode.isCharacterKey(event.keyCode); | |
| 265 } | |
| 266 | |
| 267 // Alt but not AltGr which is represented as Alt+Ctrl. | |
| 268 if (event.altKey && !event.ctrlKey) { | |
| 269 return false; | |
| 270 } | |
| 271 | |
| 272 // Saves Ctrl or Alt + key for IE and WebKit, which won't fire keypress. | |
| 273 if (!event.shiftKey && | |
| 274 (keyDownList.last.keyCode == KeyCode.CTRL || | |
| 275 keyDownList.last.keyCode == KeyCode.ALT || | |
| 276 _Device.userAgent.contains('Mac') && | |
| 277 keyDownList.last.keyCode == KeyCode.META)) { | |
| 278 return false; | |
| 279 } | |
| 280 | |
| 281 // Some keys with Ctrl/Shift do not issue keypress in WebKit. | |
| 282 if (_Device.isWebKit && event.ctrlKey && event.shiftKey && ( | |
| 283 event.keycode == KeyCode.BACKSLASH || | |
| 284 event.keycode == KeyCode.OPEN_SQUARE_BRACKET || | |
| 285 event.keycode == KeyCode.CLOSE_SQUARE_BRACKET || | |
| 286 event.keycode == KeyCode.TILDE || | |
| 287 event.keycode == KeyCode.SEMICOLON || event.keycode == KeyCode.DASH || | |
| 288 event.keycode == KeyCode.EQUALS || event.keycode == KeyCode.COMMA || | |
| 289 event.keycode == KeyCode.PERIOD || event.keycode == KeyCode.SLASH || | |
| 290 event.keycode == KeyCode.APOSTROPHE || | |
| 291 event.keycode == KeyCode.SINGLE_QUOTE)) { | |
| 292 return false; | |
| 293 } | |
| 294 | |
| 295 switch (event.keyCode) { | |
| 296 case KeyCode.ENTER: | |
| 297 // IE9 does not fire keypress on ENTER. | |
| 298 return !_Device.isIE; | |
| 299 case KeyCode.ESC: | |
| 300 return !_Device.isWebKit; | |
| 301 } | |
| 302 | |
| 303 return KeyCode.isCharacterKey(event.keyCode); | |
| 304 } | |
| 305 | |
| 306 /** | |
| 307 * Normalize the keycodes to the IE KeyCodes (this is what Chrome, IE, and | |
| 308 * Opera all use). | |
| 309 */ | |
| 310 int _normalizeKeyCodes(KeyboardEvent event) { | |
| 311 // Note: This may change once we get input about non-US keyboards. | |
| 312 if (_Device.isFirefox) { | |
| 313 switch(event.keyCode) { | |
| 314 case KeyCode.FF_EQUALS: | |
| 315 return KeyCode.EQUALS; | |
| 316 case KeyCode.FF_SEMICOLON: | |
| 317 return KeyCode.SEMICOLON; | |
| 318 case KeyCode.MAC_FF_META: | |
| 319 return KeyCode.META; | |
| 320 case KeyCode.WIN_KEY_FF_LINUX: | |
| 321 return KeyCode.WIN_KEY; | |
| 322 } | |
| 323 } | |
| 324 return event.keyCode; | |
| 325 } | |
| 326 | |
| 327 /** Handle keydown events. */ | |
| 328 void processKeyDown(KeyboardEvent e) { | |
| 329 // Ctrl-Tab and Alt-Tab can cause the focus to be moved to another window | |
| 330 // before we've caught a key-up event. If the last-key was one of these | |
| 331 // we reset the state. | |
| 332 if (keyDownList.length > 0 && | |
| 333 (keyDownList.last.keyCode == KeyCode.CTRL && !e.ctrlKey || | |
| 334 keyDownList.last.keyCode == KeyCode.ALT && !e.altKey || | |
| 335 _Device.userAgent.contains('Mac') && | |
| 336 keyDownList.last.keyCode == KeyCode.META && !e.metaKey)) { | |
| 337 keyDownList = []; | |
| 338 } | |
| 339 | |
| 340 var event = new KeyEvent(e); | |
| 341 event._shadowKeyCode = _normalizeKeyCodes(event); | |
| 342 // Technically a "keydown" event doesn't have a charCode. This is | |
| 343 // calculated nonetheless to provide us with more information in giving | |
| 344 // as much information as possible on keypress about keycode and also | |
| 345 // charCode. | |
| 346 event._shadowCharCode = _findCharCodeKeyDown(event); | |
| 347 if (keyDownList.length > 0 && event.keyCode != keyDownList.last.keyCode && | |
| 348 !_firesKeyPressEvent(event)) { | |
| 349 // Some browsers have quirks not firing keypress events where all other | |
| 350 // browsers do. This makes them more consistent. | |
| 351 processKeyPress(event); | |
| 352 } | |
| 353 keyDownList.add(event); | |
| 354 _dispatch(event); | |
| 355 } | |
| 356 | |
| 357 /** Handle keypress events. */ | |
| 358 void processKeyPress(KeyboardEvent event) { | |
| 359 var e = new KeyEvent(event); | |
| 360 // IE reports the character code in the keyCode field for keypress events. | |
| 361 // There are two exceptions however, Enter and Escape. | |
| 362 if (_Device.isIE) { | |
| 363 if (e.keyCode == KeyCode.ENTER || e.keyCode == KeyCode.ESC) { | |
| 364 e._shadowCharCode = 0; | |
| 365 } else { | |
| 366 e._shadowCharCode = e.keyCode; | |
| 367 } | |
| 368 } else if (_Device.isOpera) { | |
| 369 // Opera reports the character code in the keyCode field. | |
| 370 e._shadowCharCode = KeyCode.isCharacterKey(keyCode) ? e.keyCode : 0; | |
| 371 } | |
| 372 // Now we guestimate about what the keycode is that was actually | |
| 373 // pressed, given previous keydown information. | |
| 374 e._shadowKeyCode = _determineKeyCodeForKeypress(e); | |
| 375 | |
| 376 // Correct the key value for certain browser-specific quirks. | |
| 377 if (e._shadowKeyIdentifier && | |
| 378 _keyIdentifier.contains(e._shadowKeyIdentifier)) { | |
| 379 // This is needed for Safari Windows because it currently doesn't give a | |
| 380 // keyCode/which for non printable keys. | |
| 381 e._shadowKeyCode = _keyIdentifier[keyIdentifier]; | |
| 382 } | |
| 383 e._shadowAltKey = keyDownList.some((var element) => element.altKey); | |
| 384 _dispatch(e); | |
| 385 } | |
| 386 | |
| 387 /** Handle keyup events. */ | |
| 388 void processKeyUp(KeyboardEvent event) { | |
| 389 var e = new KeyEvent(event); | |
| 390 KeyboardEvent toRemove = null; | |
| 391 for (var key in keyDownList) { | |
| 392 if (key.keyCode == e.keyCode) { | |
| 393 toRemove = key; | |
| 394 } | |
| 395 } | |
| 396 if (toRemove != null) { | |
| 397 keyDownList = keyDownList.filter((element) => element != toRemove); | |
| 398 } else if (keyDownList.length > 0) { | |
| 399 // This happens when we've reached some international keyboard case we | |
| 400 // haven't accounted for or we haven't correctly eliminated all browser | |
| 401 // inconsistencies. Filing bugs on when this is reached is welcome! | |
| 402 keyDownList.removeLast(); | |
| 403 } | |
| 404 _dispatch(e); | |
| 405 } | |
| 406 } | |
| OLD | NEW |