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