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 _keyDown = processKeyDown; |
| 99 _keyUp = processKeyUp; |
| 100 _keyPress = processKeyPress; |
| 101 } |
| 102 |
| 103 /** |
| 104 * Hook up all event listeners under the covers so we can estimate keycodes |
| 105 * and charcodes when they are not provided. |
| 106 */ |
| 107 void _initializeAllEventListeners() { |
| 108 _keyDownList = []; |
| 109 _target.on.keyDown.add(_keyDown, true); |
| 110 _target.on.keyPress.add(_keyPress, true); |
| 111 _target.on.keyUp.add(_keyUp, true); |
| 112 } |
| 113 |
| 114 /** Add a callback that wishes to be notified when a KeyEvent occurs. */ |
| 115 void add(void callback(KeyEvent)) { |
| 116 if (_callbacks.length == 0) { |
| 117 _initializeAllEventListeners(); |
| 118 } |
| 119 _callbacks.add(callback); |
| 120 } |
| 121 |
| 122 /** |
| 123 * Notify all callback listeners that a KeyEvent of the relevant type has |
| 124 * occurred. |
| 125 */ |
| 126 bool _dispatch(KeyEvent event) { |
| 127 if (event.type == _type) { |
| 128 // Make a copy of the listeners in case a callback gets removed while |
| 129 // dispatching from the list. |
| 130 List callbacksCopy = new List.from(_callbacks); |
| 131 for(var callback in callbacksCopy) { |
| 132 callback(event); |
| 133 } |
| 134 } |
| 135 } |
| 136 |
| 137 /** Remove the given callback from the listeners list. */ |
| 138 void remove(void callback(KeyEvent)) { |
| 139 var index = _callbacks.indexOf(callback); |
| 140 if (index != -1) { |
| 141 _callbacks.removeAt(index); |
| 142 } |
| 143 if (_callbacks.length == 0) { |
| 144 // If we have no listeners, don't bother keeping track of keypresses. |
| 145 _target.on.keyDown.remove(_keyDown); |
| 146 _target.on.keyPress.remove(_keyPress); |
| 147 _target.on.keyUp.remove(_keyUp); |
| 148 } |
| 149 } |
| 150 |
| 151 /** Determine if caps lock is one of the currently depressed keys. */ |
| 152 bool get _capsLockOn() => |
| 153 _keyDownList.some((var element) => element.keyCode == KeyCode.CAPS_LOCK); |
| 154 |
| 155 /** |
| 156 * Given the previously recorded keydown key codes, see if we can determine |
| 157 * the keycode of this keypress [event]. (Generally browsers only provide |
| 158 * charCode information for keypress events, but with a little |
| 159 * reverse-engineering, we can also determine the keyCode.) Returns |
| 160 * KeyCode.UNKNOWN if the keycode could not be determined. |
| 161 */ |
| 162 int _determineKeyCodeForKeypress(KeyboardEvent event) { |
| 163 // Note: This function is a work in progress. We'll expand this function |
| 164 // once we get more information about other keyboards. |
| 165 for (var prevEvent in _keyDownList) { |
| 166 if (prevEvent._shadowCharCode == event.charCode) { |
| 167 return prevEvent.keyCode; |
| 168 } |
| 169 if ((event.shiftKey || _capsLockOn) && event.charCode >= "A".charCodes[0] |
| 170 && event.charCode <= "Z".charCodes[0] && event.charCode + |
| 171 _ROMAN_ALPHABET_OFFSET == prevEvent._shadowCharCode) { |
| 172 return prevEvent.keyCode; |
| 173 } |
| 174 } |
| 175 return KeyCode.UNKNOWN; |
| 176 } |
| 177 |
| 178 /** |
| 179 * Given the charater code returned from a keyDown [event], try to ascertain |
| 180 * and return the corresponding charCode for the character that was pressed. |
| 181 * This information is not shown to the user, but used to help polyfill |
| 182 * keypress events. |
| 183 */ |
| 184 int _findCharCodeKeyDown(KeyboardEvent event) { |
| 185 if (event.keyLocation == 3) { // Numpad keys. |
| 186 switch (event.keyCode) { |
| 187 case KeyCode.NUM_ZERO: |
| 188 // Even though this function returns _charCodes_, for some cases the |
| 189 // KeyCode == the charCode we want, in which case we use the keycode |
| 190 // constant for readability. |
| 191 return KeyCode.ZERO; |
| 192 case KeyCode.NUM_ONE: |
| 193 return KeyCode.ONE; |
| 194 case KeyCode.NUM_TWO: |
| 195 return KeyCode.TWO; |
| 196 case KeyCode.NUM_THREE: |
| 197 return KeyCode.THREE; |
| 198 case KeyCode.NUM_FOUR: |
| 199 return KeyCode.FOUR; |
| 200 case KeyCode.NUM_FIVE: |
| 201 return KeyCode.FIVE; |
| 202 case KeyCode.NUM_SIX: |
| 203 return KeyCode.SIX; |
| 204 case KeyCode.NUM_SEVEN: |
| 205 return KeyCode.SEVEN; |
| 206 case KeyCode.NUM_EIGHT: |
| 207 return KeyCode.EIGHT; |
| 208 case KeyCode.NUM_NINE: |
| 209 return KeyCode.NINE; |
| 210 case KeyCode.NUM_MULTIPLY: |
| 211 return 42; // Char code for * |
| 212 case KeyCode.NUM_PLUS: |
| 213 return 43; // + |
| 214 case KeyCode.NUM_MINUS: |
| 215 return 45; // - |
| 216 case KeyCode.NUM_PERIOD: |
| 217 return 46; // . |
| 218 case KeyCode.NUM_DIVISION: |
| 219 return 47; // / |
| 220 } |
| 221 } else if (event.keyCode >= 65 && event.keyCode <= 90) { |
| 222 // Set the "char code" for key down as the lower case letter. Again, this |
| 223 // will not show up for the user, but will be helpful in estimating |
| 224 // keyCode locations and other information during the keyPress event. |
| 225 return event.keyCode + _ROMAN_ALPHABET_OFFSET; |
| 226 } |
| 227 switch(event.keyCode) { |
| 228 case KeyCode.SEMICOLON: |
| 229 return KeyCode.FF_SEMICOLON; |
| 230 case KeyCode.EQUALS: |
| 231 return KeyCode.FF_EQUALS; |
| 232 case KeyCode.COMMA: |
| 233 return 44; // Ascii value for , |
| 234 case KeyCode.DASH: |
| 235 return 45; // - |
| 236 case KeyCode.PERIOD: |
| 237 return 46; // . |
| 238 case KeyCode.SLASH: |
| 239 return 47; // / |
| 240 case KeyCode.APOSTROPHE: |
| 241 return 96; // ` |
| 242 case KeyCode.OPEN_SQUARE_BRACKET: |
| 243 return 91; // [ |
| 244 case KeyCode.BACKSLASH: |
| 245 return 92; // \ |
| 246 case KeyCode.CLOSE_SQUARE_BRACKET: |
| 247 return 93; // ] |
| 248 case KeyCode.SINGLE_QUOTE: |
| 249 return 39; // ' |
| 250 } |
| 251 return event.keyCode; |
| 252 } |
| 253 |
| 254 /** |
| 255 * Returns true if the key fires a keypress event in the current browser. |
| 256 */ |
| 257 bool _firesKeyPressEvent(KeyEvent event) { |
| 258 if (!_Device.isIE && !_Device.isWebKit) { |
| 259 return true; |
| 260 } |
| 261 |
| 262 if (_Device.userAgent.contains('Mac') && event.altKey) { |
| 263 return KeyCode.isCharacterKey(event.keyCode); |
| 264 } |
| 265 |
| 266 // Alt but not AltGr which is represented as Alt+Ctrl. |
| 267 if (event.altKey && !event.ctrlKey) { |
| 268 return false; |
| 269 } |
| 270 |
| 271 // Saves Ctrl or Alt + key for IE and WebKit, which won't fire keypress. |
| 272 if (!event.shiftKey && |
| 273 (_keyDownList.last.keyCode == KeyCode.CTRL || |
| 274 _keyDownList.last.keyCode == KeyCode.ALT || |
| 275 _Device.userAgent.contains('Mac') && |
| 276 _keyDownList.last.keyCode == KeyCode.META)) { |
| 277 return false; |
| 278 } |
| 279 |
| 280 // Some keys with Ctrl/Shift do not issue keypress in WebKit. |
| 281 if (_Device.isWebKit && event.ctrlKey && event.shiftKey && ( |
| 282 event.keycode == KeyCode.BACKSLASH || |
| 283 event.keycode == KeyCode.OPEN_SQUARE_BRACKET || |
| 284 event.keycode == KeyCode.CLOSE_SQUARE_BRACKET || |
| 285 event.keycode == KeyCode.TILDE || |
| 286 event.keycode == KeyCode.SEMICOLON || event.keycode == KeyCode.DASH || |
| 287 event.keycode == KeyCode.EQUALS || event.keycode == KeyCode.COMMA || |
| 288 event.keycode == KeyCode.PERIOD || event.keycode == KeyCode.SLASH || |
| 289 event.keycode == KeyCode.APOSTROPHE || |
| 290 event.keycode == KeyCode.SINGLE_QUOTE)) { |
| 291 return false; |
| 292 } |
| 293 |
| 294 switch (event.keyCode) { |
| 295 case KeyCode.ENTER: |
| 296 // IE9 does not fire keypress on ENTER. |
| 297 return !_Device.isIE; |
| 298 case KeyCode.ESC: |
| 299 return !_Device.isWebKit; |
| 300 } |
| 301 |
| 302 return KeyCode.isCharacterKey(event.keyCode); |
| 303 } |
| 304 |
| 305 /** |
| 306 * Normalize the keycodes to the IE KeyCodes (this is what Chrome, IE, and |
| 307 * Opera all use). |
| 308 */ |
| 309 int _normalizeKeyCodes(KeyboardEvent event) { |
| 310 // Note: This may change once we get input about non-US keyboards. |
| 311 if (_Device.isFirefox) { |
| 312 switch(event.keyCode) { |
| 313 case KeyCode.FF_EQUALS: |
| 314 return KeyCode.EQUALS; |
| 315 case KeyCode.FF_SEMICOLON: |
| 316 return KeyCode.SEMICOLON; |
| 317 case KeyCode.MAC_FF_META: |
| 318 return KeyCode.META; |
| 319 case KeyCode.WIN_KEY_FF_LINUX: |
| 320 return KeyCode.WIN_KEY; |
| 321 } |
| 322 } |
| 323 return event.keyCode; |
| 324 } |
| 325 |
| 326 /** Handle keydown events. */ |
| 327 void processKeyDown(KeyboardEvent e) { |
| 328 // Ctrl-Tab and Alt-Tab can cause the focus to be moved to another window |
| 329 // before we've caught a key-up event. If the last-key was one of these |
| 330 // we reset the state. |
| 331 if (_keyDownList.length > 0 && |
| 332 (_keyDownList.last.keyCode == KeyCode.CTRL && !e.ctrlKey || |
| 333 _keyDownList.last.keyCode == KeyCode.ALT && !e.altKey || |
| 334 _Device.userAgent.contains('Mac') && |
| 335 _keyDownList.last.keyCode == KeyCode.META && !e.metaKey)) { |
| 336 _keyDownList = []; |
| 337 } |
| 338 |
| 339 var event = new KeyEvent(e); |
| 340 event._shadowKeyCode = _normalizeKeyCodes(event); |
| 341 // Technically a "keydown" event doesn't have a charCode. This is |
| 342 // calculated nonetheless to provide us with more information in giving |
| 343 // as much information as possible on keypress about keycode and also |
| 344 // charCode. |
| 345 event._shadowCharCode = _findCharCodeKeyDown(event); |
| 346 if (_keyDownList.length > 0 && event.keyCode != _keyDownList.last.keyCode && |
| 347 !_firesKeyPressEvent(event)) { |
| 348 // Some browsers have quirks not firing keypress events where all other |
| 349 // browsers do. This makes them more consistent. |
| 350 processKeyPress(event); |
| 351 } |
| 352 _keyDownList.add(event); |
| 353 _dispatch(event); |
| 354 } |
| 355 |
| 356 /** Handle keypress events. */ |
| 357 void processKeyPress(KeyboardEvent event) { |
| 358 var e = new KeyEvent(event); |
| 359 // IE reports the character code in the keyCode field for keypress events. |
| 360 // There are two exceptions however, Enter and Escape. |
| 361 if (_Device.isIE) { |
| 362 if (e.keyCode == KeyCode.ENTER || e.keyCode == KeyCode.ESC) { |
| 363 e._shadowCharCode = 0; |
| 364 } else { |
| 365 e._shadowCharCode = e.keyCode; |
| 366 } |
| 367 } else if (_Device.isOpera) { |
| 368 // Opera reports the character code in the keyCode field. |
| 369 e._shadowCharCode = KeyCode.isCharacterKey(keyCode) ? e.keyCode : 0; |
| 370 } |
| 371 // Now we guestimate about what the keycode is that was actually |
| 372 // pressed, given previous keydown information. |
| 373 e._shadowKeyCode = _determineKeyCodeForKeypress(e); |
| 374 |
| 375 // Correct the key value for certain browser-specific quirks. |
| 376 if (e._shadowKeyIdentifier && |
| 377 _keyIdentifier.contains(e._shadowKeyIdentifier)) { |
| 378 // This is needed for Safari Windows because it currently doesn't give a |
| 379 // keyCode/which for non printable keys. |
| 380 e._shadowKeyCode = _keyIdentifier[keyIdentifier]; |
| 381 } |
| 382 e._shadowAltKey = _keyDownList.some((var element) => element.altKey); |
| 383 _dispatch(e); |
| 384 } |
| 385 |
| 386 /** Handle keyup events. */ |
| 387 void processKeyUp(KeyboardEvent event) { |
| 388 var e = new KeyEvent(event); |
| 389 KeyboardEvent toRemove = null; |
| 390 for (var key in _keyDownList) { |
| 391 if (key.keyCode == e.keyCode) { |
| 392 toRemove = key; |
| 393 } |
| 394 } |
| 395 if (toRemove != null) { |
| 396 _keyDownList = _keyDownList.filter((element) => element != toRemove); |
| 397 } else if (_keyDownList.length > 0) { |
| 398 // This happens when we've reached some international keyboard case we |
| 399 // haven't accounted for or we haven't correctly eliminated all browser |
| 400 // inconsistencies. Filing bugs on when this is reached is welcome! |
| 401 _keyDownList.removeLast(); |
| 402 } |
| 403 _dispatch(e); |
| 404 } |
| 405 } |
OLD | NEW |