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