Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(208)

Side by Side Diff: webkit/api/src/mac/WebInputEventFactory.mm

Issue 385057: Deleted webkit/api which now lives in webkit.org (Closed)
Patch Set: Created 11 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « webkit/api/src/linux/WebFontRendering.cpp ('k') | webkit/api/src/mac/WebScreenInfoFactory.mm » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 /*
2 * Copyright (C) 2004, 2006, 2007 Apple Inc. All rights reserved.
3 * Copyright (C) 2006-2009 Google Inc.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
18 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
22 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27 #include "config.h"
28 #include "WebInputEventFactory.h"
29
30 #include <ApplicationServices/ApplicationServices.h>
31 #import <Cocoa/Cocoa.h>
32
33 #include "WebInputEvent.h"
34 #include <wtf/ASCIICType.h>
35
36 namespace WebKit {
37
38 // WebKeyboardEvent -----------------------------------------------------------
39
40 // ----------------------------------------------------------------------------
41 // Begin Apple code, copied from KeyEventMac.mm
42 //
43 // We can share some of this code if we factored it out of KeyEventMac, but
44 // the main problem is that it relies on the NSString ctor on String for
45 // conversions, and since we're building without PLATFORM(MAC), we don't have
46 // that. As a result we have to use NSString here exclusively and thus tweak
47 // the code so it's not re-usable as-is. One possiblity would be to make the
48 // upstream code only use NSString, but I'm not certain how far that change
49 // would propagate.
50
51 static inline bool isKeyUpEvent(NSEvent* event)
52 {
53 if ([event type] != NSFlagsChanged)
54 return [event type] == NSKeyUp;
55 // FIXME: This logic fails if the user presses both Shift keys at once, for example:
56 // we treat releasing one of them as keyDown.
57 switch ([event keyCode]) {
58 case 54: // Right Command
59 case 55: // Left Command
60 return ([event modifierFlags] & NSCommandKeyMask) == 0;
61
62 case 57: // Capslock
63 return ([event modifierFlags] & NSAlphaShiftKeyMask) == 0;
64
65 case 56: // Left Shift
66 case 60: // Right Shift
67 return ([event modifierFlags] & NSShiftKeyMask) == 0;
68
69 case 58: // Left Alt
70 case 61: // Right Alt
71 return ([event modifierFlags] & NSAlternateKeyMask) == 0;
72
73 case 59: // Left Ctrl
74 case 62: // Right Ctrl
75 return ([event modifierFlags] & NSControlKeyMask) == 0;
76
77 case 63: // Function
78 return ([event modifierFlags] & NSFunctionKeyMask) == 0;
79 }
80 return false;
81 }
82
83 static bool isKeypadEvent(NSEvent* event)
84 {
85 // Check that this is the type of event that has a keyCode.
86 switch ([event type]) {
87 case NSKeyDown:
88 case NSKeyUp:
89 case NSFlagsChanged:
90 break;
91 default:
92 return false;
93 }
94
95 switch ([event keyCode]) {
96 case 71: // Clear
97 case 81: // =
98 case 75: // /
99 case 67: // *
100 case 78: // -
101 case 69: // +
102 case 76: // Enter
103 case 65: // .
104 case 82: // 0
105 case 83: // 1
106 case 84: // 2
107 case 85: // 3
108 case 86: // 4
109 case 87: // 5
110 case 88: // 6
111 case 89: // 7
112 case 91: // 8
113 case 92: // 9
114 return true;
115 }
116
117 return false;
118 }
119
120 static int windowsKeyCodeForKeyEvent(NSEvent* event)
121 {
122 switch ([event keyCode]) {
123 // VK_TAB (09) TAB key
124 case 48:
125 return 0x09;
126
127 // VK_APPS (5D) Right windows/meta key
128 case 54: // Right Command
129 return 0x5D;
130
131 // VK_LWIN (5B) Left windows/meta key
132 case 55: // Left Command
133 return 0x5B;
134
135 // VK_CAPITAL (14) caps locks key
136 case 57: // Capslock
137 return 0x14;
138
139 // VK_SHIFT (10) either shift key
140 case 56: // Left Shift
141 case 60: // Right Shift
142 return 0x10;
143
144 // VK_MENU (12) either alt key
145 case 58: // Left Alt
146 case 61: // Right Alt
147 return 0x12;
148
149 // VK_CONTROL (11) either ctrl key
150 case 59: // Left Ctrl
151 case 62: // Right Ctrl
152 return 0x11;
153
154 // Begin non-Apple addition ---------------------------------------------------
155 case 63: // Function (no Windows key code)
156 return 0;
157 // End non-Apple addition -----------------------------------------------------
158
159 // VK_CLEAR (0C) CLEAR key
160 case 71: return 0x0C;
161
162 // VK_NUMPAD0 (60) Numeric keypad 0 key
163 case 82: return 0x60;
164 // VK_NUMPAD1 (61) Numeric keypad 1 key
165 case 83: return 0x61;
166 // VK_NUMPAD2 (62) Numeric keypad 2 key
167 case 84: return 0x62;
168 // VK_NUMPAD3 (63) Numeric keypad 3 key
169 case 85: return 0x63;
170 // VK_NUMPAD4 (64) Numeric keypad 4 key
171 case 86: return 0x64;
172 // VK_NUMPAD5 (65) Numeric keypad 5 key
173 case 87: return 0x65;
174 // VK_NUMPAD6 (66) Numeric keypad 6 key
175 case 88: return 0x66;
176 // VK_NUMPAD7 (67) Numeric keypad 7 key
177 case 89: return 0x67;
178 // VK_NUMPAD8 (68) Numeric keypad 8 key
179 case 91: return 0x68;
180 // VK_NUMPAD9 (69) Numeric keypad 9 key
181 case 92: return 0x69;
182 // VK_MULTIPLY (6A) Multiply key
183 case 67: return 0x6A;
184 // VK_ADD (6B) Add key
185 case 69: return 0x6B;
186
187 // VK_SUBTRACT (6D) Subtract key
188 case 78: return 0x6D;
189 // VK_DECIMAL (6E) Decimal key
190 case 65: return 0x6E;
191 // VK_DIVIDE (6F) Divide key
192 case 75: return 0x6F;
193 }
194
195 // Begin non-Apple addition ---------------------------------------------------
196 // |-[NSEvent charactersIgnoringModifiers]| isn't allowed for
197 // NSFlagsChanged, and conceivably we may not have caught everything
198 // which causes an NSFlagsChanged above.
199 if ([event type] == NSFlagsChanged)
200 return 0;
201 // End non-Apple addition -----------------------------------------------------
202
203 NSString* s = [event charactersIgnoringModifiers];
204 if ([s length] != 1)
205 return 0;
206
207 switch ([s characterAtIndex:0]) {
208 // VK_LBUTTON (01) Left mouse button
209 // VK_RBUTTON (02) Right mouse button
210 // VK_CANCEL (03) Control-break processing
211 // VK_MBUTTON (04) Middle mouse button (three-button mouse)
212 // VK_XBUTTON1 (05)
213 // VK_XBUTTON2 (06)
214
215 // VK_BACK (08) BACKSPACE key
216 case 8: case 0x7F: return 0x08;
217 // VK_TAB (09) TAB key
218 case 9: return 0x09;
219
220 // VK_CLEAR (0C) CLEAR key
221 // handled by key code above
222
223 // VK_RETURN (0D)
224 case 0xD: case 3: return 0x0D;
225
226 // VK_SHIFT (10) SHIFT key
227 // VK_CONTROL (11) CTRL key
228 // VK_MENU (12) ALT key
229
230 // VK_PAUSE (13) PAUSE key
231 case NSPauseFunctionKey: return 0x13;
232
233 // VK_CAPITAL (14) CAPS LOCK key
234 // VK_KANA (15) Input Method Editor (IME) Kana mode
235 // VK_HANGUEL (15) IME Hanguel mode (maintained for compatibility; use VK_HANGUL)
236 // VK_HANGUL (15) IME Hangul mode
237 // VK_JUNJA (17) IME Junja mode
238 // VK_FINAL (18) IME final mode
239 // VK_HANJA (19) IME Hanja mode
240 // VK_KANJI (19) IME Kanji mode
241
242 // VK_ESCAPE (1B) ESC key
243 case 0x1B: return 0x1B;
244
245 // VK_CONVERT (1C) IME convert
246 // VK_NONCONVERT (1D) IME nonconvert
247 // VK_ACCEPT (1E) IME accept
248 // VK_MODECHANGE (1F) IME mode change request
249
250 // VK_SPACE (20) SPACEBAR
251 case ' ': return 0x20;
252 // VK_PRIOR (21) PAGE UP key
253 case NSPageUpFunctionKey: return 0x21;
254 // VK_NEXT (22) PAGE DOWN key
255 case NSPageDownFunctionKey: return 0x22;
256 // VK_END (23) END key
257 case NSEndFunctionKey: return 0x23;
258 // VK_HOME (24) HOME key
259 case NSHomeFunctionKey: return 0x24;
260 // VK_LEFT (25) LEFT ARROW key
261 case NSLeftArrowFunctionKey: return 0x25;
262 // VK_UP (26) UP ARROW key
263 case NSUpArrowFunctionKey: return 0x26;
264 // VK_RIGHT (27) RIGHT ARROW key
265 case NSRightArrowFunctionKey: return 0x27;
266 // VK_DOWN (28) DOWN ARROW key
267 case NSDownArrowFunctionKey: return 0x28;
268 // VK_SELECT (29) SELECT key
269 case NSSelectFunctionKey: return 0x29;
270 // VK_PRINT (2A) PRINT key
271 case NSPrintFunctionKey: return 0x2A;
272 // VK_EXECUTE (2B) EXECUTE key
273 case NSExecuteFunctionKey: return 0x2B;
274 // VK_SNAPSHOT (2C) PRINT SCREEN key
275 case NSPrintScreenFunctionKey: return 0x2C;
276 // VK_INSERT (2D) INS key
277 case NSInsertFunctionKey: case NSHelpFunctionKey: return 0x2D;
278 // VK_DELETE (2E) DEL key
279 case NSDeleteFunctionKey: return 0x2E;
280
281 // VK_HELP (2F) HELP key
282
283 // (30) 0 key
284 case '0': case ')': return 0x30;
285 // (31) 1 key
286 case '1': case '!': return 0x31;
287 // (32) 2 key
288 case '2': case '@': return 0x32;
289 // (33) 3 key
290 case '3': case '#': return 0x33;
291 // (34) 4 key
292 case '4': case '$': return 0x34;
293 // (35) 5 key
294 case '5': case '%': return 0x35;
295 // (36) 6 key
296 case '6': case '^': return 0x36;
297 // (37) 7 key
298 case '7': case '&': return 0x37;
299 // (38) 8 key
300 case '8': case '*': return 0x38;
301 // (39) 9 key
302 case '9': case '(': return 0x39;
303 // (41) A key
304 case 'a': case 'A': return 0x41;
305 // (42) B key
306 case 'b': case 'B': return 0x42;
307 // (43) C key
308 case 'c': case 'C': return 0x43;
309 // (44) D key
310 case 'd': case 'D': return 0x44;
311 // (45) E key
312 case 'e': case 'E': return 0x45;
313 // (46) F key
314 case 'f': case 'F': return 0x46;
315 // (47) G key
316 case 'g': case 'G': return 0x47;
317 // (48) H key
318 case 'h': case 'H': return 0x48;
319 // (49) I key
320 case 'i': case 'I': return 0x49;
321 // (4A) J key
322 case 'j': case 'J': return 0x4A;
323 // (4B) K key
324 case 'k': case 'K': return 0x4B;
325 // (4C) L key
326 case 'l': case 'L': return 0x4C;
327 // (4D) M key
328 case 'm': case 'M': return 0x4D;
329 // (4E) N key
330 case 'n': case 'N': return 0x4E;
331 // (4F) O key
332 case 'o': case 'O': return 0x4F;
333 // (50) P key
334 case 'p': case 'P': return 0x50;
335 // (51) Q key
336 case 'q': case 'Q': return 0x51;
337 // (52) R key
338 case 'r': case 'R': return 0x52;
339 // (53) S key
340 case 's': case 'S': return 0x53;
341 // (54) T key
342 case 't': case 'T': return 0x54;
343 // (55) U key
344 case 'u': case 'U': return 0x55;
345 // (56) V key
346 case 'v': case 'V': return 0x56;
347 // (57) W key
348 case 'w': case 'W': return 0x57;
349 // (58) X key
350 case 'x': case 'X': return 0x58;
351 // (59) Y key
352 case 'y': case 'Y': return 0x59;
353 // (5A) Z key
354 case 'z': case 'Z': return 0x5A;
355
356 // VK_LWIN (5B) Left Windows key (Microsoft Natural keyboard)
357 // VK_RWIN (5C) Right Windows key (Natural keyboard)
358 // VK_APPS (5D) Applications key (Natural keyboard)
359 // VK_SLEEP (5F) Computer Sleep key
360
361 // VK_NUMPAD0 (60) Numeric keypad 0 key
362 // VK_NUMPAD1 (61) Numeric keypad 1 key
363 // VK_NUMPAD2 (62) Numeric keypad 2 key
364 // VK_NUMPAD3 (63) Numeric keypad 3 key
365 // VK_NUMPAD4 (64) Numeric keypad 4 key
366 // VK_NUMPAD5 (65) Numeric keypad 5 key
367 // VK_NUMPAD6 (66) Numeric keypad 6 key
368 // VK_NUMPAD7 (67) Numeric keypad 7 key
369 // VK_NUMPAD8 (68) Numeric keypad 8 key
370 // VK_NUMPAD9 (69) Numeric keypad 9 key
371 // VK_MULTIPLY (6A) Multiply key
372 // VK_ADD (6B) Add key
373 // handled by key code above
374
375 // VK_SEPARATOR (6C) Separator key
376
377 // VK_SUBTRACT (6D) Subtract key
378 // VK_DECIMAL (6E) Decimal key
379 // VK_DIVIDE (6F) Divide key
380 // handled by key code above
381
382 // VK_F1 (70) F1 key
383 case NSF1FunctionKey: return 0x70;
384 // VK_F2 (71) F2 key
385 case NSF2FunctionKey: return 0x71;
386 // VK_F3 (72) F3 key
387 case NSF3FunctionKey: return 0x72;
388 // VK_F4 (73) F4 key
389 case NSF4FunctionKey: return 0x73;
390 // VK_F5 (74) F5 key
391 case NSF5FunctionKey: return 0x74;
392 // VK_F6 (75) F6 key
393 case NSF6FunctionKey: return 0x75;
394 // VK_F7 (76) F7 key
395 case NSF7FunctionKey: return 0x76;
396 // VK_F8 (77) F8 key
397 case NSF8FunctionKey: return 0x77;
398 // VK_F9 (78) F9 key
399 case NSF9FunctionKey: return 0x78;
400 // VK_F10 (79) F10 key
401 case NSF10FunctionKey: return 0x79;
402 // VK_F11 (7A) F11 key
403 case NSF11FunctionKey: return 0x7A;
404 // VK_F12 (7B) F12 key
405 case NSF12FunctionKey: return 0x7B;
406 // VK_F13 (7C) F13 key
407 case NSF13FunctionKey: return 0x7C;
408 // VK_F14 (7D) F14 key
409 case NSF14FunctionKey: return 0x7D;
410 // VK_F15 (7E) F15 key
411 case NSF15FunctionKey: return 0x7E;
412 // VK_F16 (7F) F16 key
413 case NSF16FunctionKey: return 0x7F;
414 // VK_F17 (80H) F17 key
415 case NSF17FunctionKey: return 0x80;
416 // VK_F18 (81H) F18 key
417 case NSF18FunctionKey: return 0x81;
418 // VK_F19 (82H) F19 key
419 case NSF19FunctionKey: return 0x82;
420 // VK_F20 (83H) F20 key
421 case NSF20FunctionKey: return 0x83;
422 // VK_F21 (84H) F21 key
423 case NSF21FunctionKey: return 0x84;
424 // VK_F22 (85H) F22 key
425 case NSF22FunctionKey: return 0x85;
426 // VK_F23 (86H) F23 key
427 case NSF23FunctionKey: return 0x86;
428 // VK_F24 (87H) F24 key
429 case NSF24FunctionKey: return 0x87;
430
431 // VK_NUMLOCK (90) NUM LOCK key
432
433 // VK_SCROLL (91) SCROLL LOCK key
434 case NSScrollLockFunctionKey: return 0x91;
435
436 // VK_LSHIFT (A0) Left SHIFT key
437 // VK_RSHIFT (A1) Right SHIFT key
438 // VK_LCONTROL (A2) Left CONTROL key
439 // VK_RCONTROL (A3) Right CONTROL key
440 // VK_LMENU (A4) Left MENU key
441 // VK_RMENU (A5) Right MENU key
442 // VK_BROWSER_BACK (A6) Windows 2000/XP: Browser Back key
443 // VK_BROWSER_FORWARD (A7) Windows 2000/XP: Browser Forward key
444 // VK_BROWSER_REFRESH (A8) Windows 2000/XP: Browser Refresh key
445 // VK_BROWSER_STOP (A9) Windows 2000/XP: Browser Stop key
446 // VK_BROWSER_SEARCH (AA) Windows 2000/XP: Browser Search key
447 // VK_BROWSER_FAVORITES (AB) Windows 2000/XP: Browser Favorites key
448 // VK_BROWSER_HOME (AC) Windows 2000/XP: Browser Start and Home key
449 // VK_VOLUME_MUTE (AD) Windows 2000/XP: Volume Mute key
450 // VK_VOLUME_DOWN (AE) Windows 2000/XP: Volume Down key
451 // VK_VOLUME_UP (AF) Windows 2000/XP: Volume Up key
452 // VK_MEDIA_NEXT_TRACK (B0) Windows 2000/XP: Next Track key
453 // VK_MEDIA_PREV_TRACK (B1) Windows 2000/XP: Previous Track key
454 // VK_MEDIA_STOP (B2) Windows 2000/XP: Stop Media key
455 // VK_MEDIA_PLAY_PAUSE (B3) Windows 2000/XP: Play/Pause Media key
456 // VK_LAUNCH_MAIL (B4) Windows 2000/XP: Start Mail key
457 // VK_LAUNCH_MEDIA_SELECT (B5) Windows 2000/XP: Select Media key
458 // VK_LAUNCH_APP1 (B6) Windows 2000/XP: Start Application 1 key
459 // VK_LAUNCH_APP2 (B7) Windows 2000/XP: Start Application 2 key
460
461 // VK_OEM_1 (BA) Used for miscellaneous characters; it can vary by keyboard. Windows 2000/XP: For the US standard keyboard, the ';:' key
462 case ';': case ':': return 0xBA;
463 // VK_OEM_PLUS (BB) Windows 2000/XP: For any country/region, the '+' key
464 case '=': case '+': return 0xBB;
465 // VK_OEM_COMMA (BC) Windows 2000/XP: For any country/region, the ',' key
466 case ',': case '<': return 0xBC;
467 // VK_OEM_MINUS (BD) Windows 2000/XP: For any country/region, the '-' key
468 case '-': case '_': return 0xBD;
469 // VK_OEM_PERIOD (BE) Windows 2000/XP: For any country/region, the '.' key
470 case '.': case '>': return 0xBE;
471 // VK_OEM_2 (BF) Used for miscellaneous characters; it can vary by keyboard. Windows 2000/XP: For the US standard keyboard, the '/?' key
472 case '/': case '?': return 0xBF;
473 // VK_OEM_3 (C0) Used for miscellaneous characters; it can vary by keyboard. Windows 2000/XP: For the US standard keyboard, the '`~' key
474 case '`': case '~': return 0xC0;
475 // VK_OEM_4 (DB) Used for miscellaneous characters; it can vary by keyboard. Windows 2000/XP: For the US standard keyboard, the '[{' key
476 case '[': case '{': return 0xDB;
477 // VK_OEM_5 (DC) Used for miscellaneous characters; it can vary by keyboard. Windows 2000/XP: For the US standard keyboard, the '\|' key
478 case '\\': case '|': return 0xDC;
479 // VK_OEM_6 (DD) Used for miscellaneous characters; it can vary by keyboard. Windows 2000/XP: For the US standard keyboard, the ']}' key
480 case ']': case '}': return 0xDD;
481 // VK_OEM_7 (DE) Used for miscellaneous characters; it can vary by keyboard. Windows 2000/XP: For the US standard keyboard, the 'single-quote/double-quote' key
482 case '\'': case '"': return 0xDE;
483
484 // VK_OEM_8 (DF) Used for miscellaneous characters; it can vary by keyboard.
485 // VK_OEM_102 (E2) Windows 2000/XP: Either the angle bracket key or the backslash key on the RT 102-key keyboard
486 // VK_PROCESSKEY (E5) Windows 95/98/Me, Windows NT 4.0, Windows 2000/XP: IME PROCESS key
487 // VK_PACKET (E7) Windows 2000/XP: Used to pass Unicode characters as if they were keystrokes. The VK_PACKET key is the low word of a 32-bit Virtual Key value used for non-keyboard input methods. For more information, see Remark in KEYBDINPUT,SendInput, WM_KEYDOWN, and WM_KEYUP
488 // VK_ATTN (F6) Attn key
489 // VK_CRSEL (F7) CrSel key
490 // VK_EXSEL (F8) ExSel key
491 // VK_EREOF (F9) Erase EOF key
492 // VK_PLAY (FA) Play key
493 // VK_ZOOM (FB) Zoom key
494 // VK_NONAME (FC) Reserved for future use
495 // VK_PA1 (FD) PA1 key
496 // VK_OEM_CLEAR (FE) Clear key
497 }
498
499 return 0;
500 }
501
502 static inline NSString* textFromEvent(NSEvent* event)
503 {
504 if ([event type] == NSFlagsChanged)
505 return @"";
506 return [event characters];
507 }
508
509 static inline NSString* unmodifiedTextFromEvent(NSEvent* event)
510 {
511 if ([event type] == NSFlagsChanged)
512 return @"";
513 return [event charactersIgnoringModifiers];
514 }
515
516 static NSString* keyIdentifierForKeyEvent(NSEvent* event)
517 {
518 if ([event type] == NSFlagsChanged) {
519 switch ([event keyCode]) {
520 case 54: // Right Command
521 case 55: // Left Command
522 return @"Meta";
523
524 case 57: // Capslock
525 return @"CapsLock";
526
527 case 56: // Left Shift
528 case 60: // Right Shift
529 return @"Shift";
530
531 case 58: // Left Alt
532 case 61: // Right Alt
533 return @"Alt";
534
535 case 59: // Left Ctrl
536 case 62: // Right Ctrl
537 return @"Control";
538
539 // Begin non-Apple addition/modification --------------------------------------
540 case 63: // Function
541 return @"Function";
542
543 default: // Unknown, but this may be a strange/new keyboard.
544 return @"Unidentified";
545 // End non-Apple addition/modification ----------------------------------------
546 }
547 }
548
549 NSString* s = [event charactersIgnoringModifiers];
550 if ([s length] != 1)
551 return @"Unidentified";
552
553 unichar c = [s characterAtIndex:0];
554 switch (c) {
555 // Each identifier listed in the DOM spec is listed here.
556 // Many are simply commented out since they do not appear on standard Macintosh keyboards
557 // or are on a key that doesn't have a corresponding character.
558
559 // "Accept"
560 // "AllCandidates"
561
562 // "Alt"
563 case NSMenuFunctionKey:
564 return @"Alt";
565
566 // "Apps"
567 // "BrowserBack"
568 // "BrowserForward"
569 // "BrowserHome"
570 // "BrowserRefresh"
571 // "BrowserSearch"
572 // "BrowserStop"
573 // "CapsLock"
574
575 // "Clear"
576 case NSClearLineFunctionKey:
577 return @"Clear";
578
579 // "CodeInput"
580 // "Compose"
581 // "Control"
582 // "Crsel"
583 // "Convert"
584 // "Copy"
585 // "Cut"
586
587 // "Down"
588 case NSDownArrowFunctionKey:
589 return @"Down";
590 // "End"
591 case NSEndFunctionKey:
592 return @"End";
593 // "Enter"
594 case 0x3: case 0xA: case 0xD: // Macintosh calls the one on the main keyboard Return, but Windows calls it Enter, so we'll do the same for the DOM
595 return @"Enter";
596
597 // "EraseEof"
598
599 // "Execute"
600 case NSExecuteFunctionKey:
601 return @"Execute";
602
603 // "Exsel"
604
605 // "F1"
606 case NSF1FunctionKey:
607 return @"F1";
608 // "F2"
609 case NSF2FunctionKey:
610 return @"F2";
611 // "F3"
612 case NSF3FunctionKey:
613 return @"F3";
614 // "F4"
615 case NSF4FunctionKey:
616 return @"F4";
617 // "F5"
618 case NSF5FunctionKey:
619 return @"F5";
620 // "F6"
621 case NSF6FunctionKey:
622 return @"F6";
623 // "F7"
624 case NSF7FunctionKey:
625 return @"F7";
626 // "F8"
627 case NSF8FunctionKey:
628 return @"F8";
629 // "F9"
630 case NSF9FunctionKey:
631 return @"F9";
632 // "F10"
633 case NSF10FunctionKey:
634 return @"F10";
635 // "F11"
636 case NSF11FunctionKey:
637 return @"F11";
638 // "F12"
639 case NSF12FunctionKey:
640 return @"F12";
641 // "F13"
642 case NSF13FunctionKey:
643 return @"F13";
644 // "F14"
645 case NSF14FunctionKey:
646 return @"F14";
647 // "F15"
648 case NSF15FunctionKey:
649 return @"F15";
650 // "F16"
651 case NSF16FunctionKey:
652 return @"F16";
653 // "F17"
654 case NSF17FunctionKey:
655 return @"F17";
656 // "F18"
657 case NSF18FunctionKey:
658 return @"F18";
659 // "F19"
660 case NSF19FunctionKey:
661 return @"F19";
662 // "F20"
663 case NSF20FunctionKey:
664 return @"F20";
665 // "F21"
666 case NSF21FunctionKey:
667 return @"F21";
668 // "F22"
669 case NSF22FunctionKey:
670 return @"F22";
671 // "F23"
672 case NSF23FunctionKey:
673 return @"F23";
674 // "F24"
675 case NSF24FunctionKey:
676 return @"F24";
677
678 // "FinalMode"
679
680 // "Find"
681 case NSFindFunctionKey:
682 return @"Find";
683
684 // "FullWidth"
685 // "HalfWidth"
686 // "HangulMode"
687 // "HanjaMode"
688
689 // "Help"
690 case NSHelpFunctionKey:
691 return @"Help";
692
693 // "Hiragana"
694
695 // "Home"
696 case NSHomeFunctionKey:
697 return @"Home";
698 // "Insert"
699 case NSInsertFunctionKey:
700 return @"Insert";
701
702 // "JapaneseHiragana"
703 // "JapaneseKatakana"
704 // "JapaneseRomaji"
705 // "JunjaMode"
706 // "KanaMode"
707 // "KanjiMode"
708 // "Katakana"
709 // "LaunchApplication1"
710 // "LaunchApplication2"
711 // "LaunchMail"
712
713 // "Left"
714 case NSLeftArrowFunctionKey:
715 return @"Left";
716
717 // "Meta"
718 // "MediaNextTrack"
719 // "MediaPlayPause"
720 // "MediaPreviousTrack"
721 // "MediaStop"
722
723 // "ModeChange"
724 case NSModeSwitchFunctionKey:
725 return @"ModeChange";
726
727 // "Nonconvert"
728 // "NumLock"
729
730 // "PageDown"
731 case NSPageDownFunctionKey:
732 return @"PageDown";
733 // "PageUp"
734 case NSPageUpFunctionKey:
735 return @"PageUp";
736
737 // "Paste"
738
739 // "Pause"
740 case NSPauseFunctionKey:
741 return @"Pause";
742
743 // "Play"
744 // "PreviousCandidate"
745
746 // "PrintScreen"
747 case NSPrintScreenFunctionKey:
748 return @"PrintScreen";
749
750 // "Process"
751 // "Props"
752
753 // "Right"
754 case NSRightArrowFunctionKey:
755 return @"Right";
756
757 // "RomanCharacters"
758
759 // "Scroll"
760 case NSScrollLockFunctionKey:
761 return @"Scroll";
762 // "Select"
763 case NSSelectFunctionKey:
764 return @"Select";
765
766 // "SelectMedia"
767 // "Shift"
768
769 // "Stop"
770 case NSStopFunctionKey:
771 return @"Stop";
772 // "Up"
773 case NSUpArrowFunctionKey:
774 return @"Up";
775 // "Undo"
776 case NSUndoFunctionKey:
777 return @"Undo";
778
779 // "VolumeDown"
780 // "VolumeMute"
781 // "VolumeUp"
782 // "Win"
783 // "Zoom"
784
785 // More function keys, not in the key identifier specification.
786 case NSF25FunctionKey:
787 return @"F25";
788 case NSF26FunctionKey:
789 return @"F26";
790 case NSF27FunctionKey:
791 return @"F27";
792 case NSF28FunctionKey:
793 return @"F28";
794 case NSF29FunctionKey:
795 return @"F29";
796 case NSF30FunctionKey:
797 return @"F30";
798 case NSF31FunctionKey:
799 return @"F31";
800 case NSF32FunctionKey:
801 return @"F32";
802 case NSF33FunctionKey:
803 return @"F33";
804 case NSF34FunctionKey:
805 return @"F34";
806 case NSF35FunctionKey:
807 return @"F35";
808
809 // Turn 0x7F into 0x08, because backspace needs to always be 0x08.
810 case 0x7F:
811 return @"U+0008";
812 // Standard says that DEL becomes U+007F.
813 case NSDeleteFunctionKey:
814 return @"U+007F";
815
816 // Always use 0x09 for tab instead of AppKit's backtab character.
817 case NSBackTabCharacter:
818 return @"U+0009";
819
820 case NSBeginFunctionKey:
821 case NSBreakFunctionKey:
822 case NSClearDisplayFunctionKey:
823 case NSDeleteCharFunctionKey:
824 case NSDeleteLineFunctionKey:
825 case NSInsertCharFunctionKey:
826 case NSInsertLineFunctionKey:
827 case NSNextFunctionKey:
828 case NSPrevFunctionKey:
829 case NSPrintFunctionKey:
830 case NSRedoFunctionKey:
831 case NSResetFunctionKey:
832 case NSSysReqFunctionKey:
833 case NSSystemFunctionKey:
834 case NSUserFunctionKey:
835 // FIXME: We should use something other than the vendor-area Unicode values for the above keys.
836 // For now, just fall through to the default.
837 default:
838 return [NSString stringWithFormat:@"U+%04X", WTF::toASCIIUpper(c)];
839 }
840 }
841
842 // End Apple code.
843 // ----------------------------------------------------------------------------
844
845 static inline int modifiersFromEvent(NSEvent* event) {
846 int modifiers = 0;
847
848 if ([event modifierFlags] & NSControlKeyMask)
849 modifiers |= WebInputEvent::ControlKey;
850 if ([event modifierFlags] & NSShiftKeyMask)
851 modifiers |= WebInputEvent::ShiftKey;
852 if ([event modifierFlags] & NSAlternateKeyMask)
853 modifiers |= WebInputEvent::AltKey;
854 if ([event modifierFlags] & NSCommandKeyMask)
855 modifiers |= WebInputEvent::MetaKey;
856 // TODO(port): Set mouse button states
857
858 return modifiers;
859 }
860
861 WebKeyboardEvent WebInputEventFactory::keyboardEvent(NSEvent* event)
862 {
863 WebKeyboardEvent result;
864
865 result.type =
866 isKeyUpEvent(event) ? WebInputEvent::KeyUp : WebInputEvent::RawKeyDown;
867
868 result.modifiers = modifiersFromEvent(event);
869
870 if (isKeypadEvent(event))
871 result.modifiers |= WebInputEvent::IsKeyPad;
872
873 if (([event type] != NSFlagsChanged) && [event isARepeat])
874 result.modifiers |= WebInputEvent::IsAutoRepeat;
875
876 result.windowsKeyCode = windowsKeyCodeForKeyEvent(event);
877 result.nativeKeyCode = [event keyCode];
878
879 NSString* textStr = textFromEvent(event);
880 NSString* unmodifiedStr = unmodifiedTextFromEvent(event);
881 NSString* identifierStr = keyIdentifierForKeyEvent(event);
882
883 // Begin Apple code, copied from KeyEventMac.mm
884
885 // Always use 13 for Enter/Return -- we don't want to use AppKit's
886 // different character for Enter.
887 if (result.windowsKeyCode == '\r') {
888 textStr = @"\r";
889 unmodifiedStr = @"\r";
890 }
891
892 // The adjustments below are only needed in backward compatibility mode,
893 // but we cannot tell what mode we are in from here.
894
895 // Turn 0x7F into 8, because backspace needs to always be 8.
896 if ([textStr isEqualToString:@"\x7F"])
897 textStr = @"\x8";
898 if ([unmodifiedStr isEqualToString:@"\x7F"])
899 unmodifiedStr = @"\x8";
900 // Always use 9 for tab -- we don't want to use AppKit's different character
901 // for shift-tab.
902 if (result.windowsKeyCode == 9) {
903 textStr = @"\x9";
904 unmodifiedStr = @"\x9";
905 }
906
907 // End Apple code.
908
909 if ([textStr length] < WebKeyboardEvent::textLengthCap &&
910 [unmodifiedStr length] < WebKeyboardEvent::textLengthCap) {
911 [textStr getCharacters:&result.text[0]];
912 [unmodifiedStr getCharacters:&result.unmodifiedText[0]];
913 } else
914 ASSERT_NOT_REACHED();
915
916 [identifierStr getCString:&result.keyIdentifier[0]
917 maxLength:sizeof(result.keyIdentifier)
918 encoding:NSASCIIStringEncoding];
919
920 result.timeStampSeconds = [event timestamp];
921
922 // Windows and Linux set |isSystemKey| if alt is down. WebKit looks at this
923 // flag to decide if it should handle a key or not. E.g. alt-left/right
924 // shouldn't be used by WebKit to scroll the current page, because we want
925 // to get that key back for it to do history navigation. Hence, the
926 // corresponding situation on OS X is to set this for cmd key presses.
927 if (result.modifiers & WebInputEvent::MetaKey)
928 result.isSystemKey = true;
929
930 return result;
931 }
932
933 WebKeyboardEvent WebInputEventFactory::keyboardEvent(wchar_t character,
934 int modifiers,
935 double timeStampSeconds)
936 {
937 // keyboardEvent(NSEvent*) depends on the NSEvent object and
938 // it is hard to use it from methods of the NSTextInput protocol. For
939 // such methods, this function creates a WebInputEvent::Char event without
940 // using a NSEvent object.
941 WebKeyboardEvent result;
942 result.type = WebKit::WebInputEvent::Char;
943 result.timeStampSeconds = timeStampSeconds;
944 result.modifiers = modifiers;
945 result.windowsKeyCode = character;
946 result.nativeKeyCode = character;
947 result.text[0] = character;
948 result.unmodifiedText[0] = character;
949
950 // Windows and Linux set |isSystemKey| if alt is down. WebKit looks at this
951 // flag to decide if it should handle a key or not. E.g. alt-left/right
952 // shouldn't be used by WebKit to scroll the current page, because we want
953 // to get that key back for it to do history navigation. Hence, the
954 // corresponding situation on OS X is to set this for cmd key presses.
955 if (result.modifiers & WebInputEvent::MetaKey)
956 result.isSystemKey = true;
957
958 return result;
959 }
960
961 // WebMouseEvent --------------------------------------------------------------
962
963 WebMouseEvent WebInputEventFactory::mouseEvent(NSEvent* event, NSView* view)
964 {
965 WebMouseEvent result;
966
967 result.clickCount = 0;
968
969 switch ([event type]) {
970 case NSMouseExited:
971 result.type = WebInputEvent::MouseLeave;
972 result.button = WebMouseEvent::ButtonNone;
973 break;
974 case NSLeftMouseDown:
975 result.type = WebInputEvent::MouseDown;
976 result.clickCount = [event clickCount];
977 result.button = WebMouseEvent::ButtonLeft;
978 break;
979 case NSOtherMouseDown:
980 result.type = WebInputEvent::MouseDown;
981 result.clickCount = [event clickCount];
982 result.button = WebMouseEvent::ButtonMiddle;
983 break;
984 case NSRightMouseDown:
985 result.type = WebInputEvent::MouseDown;
986 result.clickCount = [event clickCount];
987 result.button = WebMouseEvent::ButtonRight;
988 break;
989 case NSLeftMouseUp:
990 result.type = WebInputEvent::MouseUp;
991 result.button = WebMouseEvent::ButtonLeft;
992 break;
993 case NSOtherMouseUp:
994 result.type = WebInputEvent::MouseUp;
995 result.button = WebMouseEvent::ButtonMiddle;
996 break;
997 case NSRightMouseUp:
998 result.type = WebInputEvent::MouseUp;
999 result.button = WebMouseEvent::ButtonRight;
1000 break;
1001 case NSMouseMoved:
1002 case NSMouseEntered:
1003 result.type = WebInputEvent::MouseMove;
1004 break;
1005 case NSLeftMouseDragged:
1006 result.type = WebInputEvent::MouseMove;
1007 result.button = WebMouseEvent::ButtonLeft;
1008 break;
1009 case NSOtherMouseDragged:
1010 result.type = WebInputEvent::MouseMove;
1011 result.button = WebMouseEvent::ButtonMiddle;
1012 break;
1013 case NSRightMouseDragged:
1014 result.type = WebInputEvent::MouseMove;
1015 result.button = WebMouseEvent::ButtonRight;
1016 break;
1017 default:
1018 ASSERT_NOT_REACHED();
1019 }
1020
1021 NSPoint location = [NSEvent mouseLocation]; // global coordinates
1022 result.globalX = location.x;
1023 result.globalY = [[[view window] screen] frame].size.height - location.y;
1024
1025 NSPoint windowLocal = [event locationInWindow];
1026 location = [view convertPoint:windowLocal fromView:nil];
1027 result.y = [view frame].size.height - location.y; // flip y
1028 result.x = location.x;
1029 result.windowX = result.x;
1030 result.windowY = result.y;
1031
1032 result.modifiers = modifiersFromEvent(event);
1033
1034 result.timeStampSeconds = [event timestamp];
1035
1036 return result;
1037 }
1038
1039 // WebMouseWheelEvent ---------------------------------------------------------
1040
1041 WebMouseWheelEvent WebInputEventFactory::mouseWheelEvent(NSEvent* event, NSView* view)
1042 {
1043 WebMouseWheelEvent result;
1044
1045 result.type = WebInputEvent::MouseWheel;
1046 result.button = WebMouseEvent::ButtonNone;
1047
1048 result.modifiers = modifiersFromEvent(event);
1049
1050 // Set coordinates by translating event coordinates from screen to client.
1051 NSPoint location = [NSEvent mouseLocation]; // global coordinates
1052 result.globalX = location.x;
1053 result.globalY = location.y;
1054 NSPoint windowLocal = [event locationInWindow];
1055 location = [view convertPoint:windowLocal fromView:nil];
1056 result.x = location.x;
1057 result.y = [view frame].size.height - location.y; // flip y
1058 result.windowX = result.x;
1059 result.windowY = result.y;
1060
1061 // Of Mice and Men
1062 // ---------------
1063 //
1064 // There are three types of scroll data available on a scroll wheel CGEvent.
1065 // Apple's documentation ([1]) is rather vague in their differences, and not
1066 // terribly helpful in deciding which to use. This is what's really going on.
1067 //
1068 // First, these events behave very differently depending on whether a standard
1069 // wheel mouse is used (one that scrolls in discrete units) or a
1070 // trackpad/Mighty Mouse is used (which both provide continuous scrolling).
1071 // You must check to see which was used for the event by testing the
1072 // kCGScrollWheelEventIsContinuous field.
1073 //
1074 // Second, these events refer to "axes". Axis 1 is the y-axis, and axis 2 is
1075 // the x-axis.
1076 //
1077 // Third, there is a concept of mouse acceleration. Scrolling the same amount
1078 // of physical distance will give you different results logically depending on
1079 // whether you scrolled a little at a time or in one continuous motion. Some
1080 // fields account for this while others do not.
1081 //
1082 // Fourth, for trackpads there is a concept of chunkiness. When scrolling
1083 // continuously, events can be delivered in chunks. That is to say, lots of
1084 // scroll events with delta 0 will be delivered, and every so often an event
1085 // with a non-zero delta will be delivered, containing the accumulated deltas
1086 // from all the intermediate moves. [2]
1087 //
1088 // For notchy wheel mice (kCGScrollWheelEventIsContinuous == 0)
1089 // ------------------------------------------------------------
1090 //
1091 // kCGScrollWheelEventDeltaAxis*
1092 // This is the rawest of raw events. For each mouse notch you get a value of
1093 // +1/-1. This does not take acceleration into account and thus is less
1094 // useful for building UIs.
1095 //
1096 // kCGScrollWheelEventPointDeltaAxis*
1097 // This is smarter. In general, for each mouse notch you get a value of
1098 // +1/-1, but this _does_ take acceleration into account, so you will get
1099 // larger values on longer scrolls. This field would be ideal for building
1100 // UIs except for one nasty bug: when the shift key is pressed, this set of
1101 // fields fails to move the value into the axis2 field (the other two types
1102 // of data do). This wouldn't be so bad except for the fact that while the
1103 // number of axes is used in the creation of a CGScrollWheelEvent, there is
1104 // no way to get that information out of the event once created.
1105 //
1106 // kCGScrollWheelEventFixedPtDeltaAxis*
1107 // This is a fixed value, and for each mouse notch you get a value of
1108 // +0.1/-0.1 (but, like above, scaled appropriately for acceleration). This
1109 // value takes acceleration into account, and in fact is identical to the
1110 // results you get from -[NSEvent delta*]. (That is, if you linked on Tiger
1111 // or greater; see [2] for details.)
1112 //
1113 // A note about continuous devices
1114 // -------------------------------
1115 //
1116 // There are two devices that provide continuous scrolling events (trackpads
1117 // and Mighty Mouses) and they behave rather differently. The Mighty Mouse
1118 // behaves a lot like a regular mouse. There is no chunking, and the
1119 // FixedPtDelta values are the PointDelta values multiplied by 0.1. With the
1120 // trackpad, though, there is chunking. While the FixedPtDelta values are
1121 // reasonable (they occur about every fifth event but have values five times
1122 // larger than usual) the Delta values are unreasonable. They don't appear to
1123 // accumulate properly.
1124 //
1125 // For continuous devices (kCGScrollWheelEventIsContinuous != 0)
1126 // -------------------------------------------------------------
1127 //
1128 // kCGScrollWheelEventDeltaAxis*
1129 // This provides values with no acceleration. With a trackpad, these values
1130 // are chunked but each non-zero value does not appear to be cumulative.
1131 // This seems to be a bug.
1132 //
1133 // kCGScrollWheelEventPointDeltaAxis*
1134 // This provides values with acceleration. With a trackpad, these values are
1135 // not chunked and are highly accurate.
1136 //
1137 // kCGScrollWheelEventFixedPtDeltaAxis*
1138 // This provides values with acceleration. With a trackpad, these values are
1139 // chunked but unlike Delta events are properly cumulative.
1140 //
1141 // Summary
1142 // -------
1143 //
1144 // In general the best approach to take is: determine if the event is
1145 // continuous. If it is not, then use the FixedPtDelta events (or just stick
1146 // with Cocoa events). They provide both acceleration and proper horizontal
1147 // scrolling. If the event is continuous, then doing pixel scrolling with the
1148 // PointDelta is the way to go. In general, avoid the Delta events. They're
1149 // the oldest (dating back to 10.4, before CGEvents were public) but they lack
1150 // acceleration and precision, making them useful only in specific edge cases.
1151 //
1152 // References
1153 // ----------
1154 //
1155 // [1] <http://developer.apple.com/documentation/Carbon/Reference/QuartzEventServicesRef/Reference/reference.html>
1156 // [2] <http://developer.apple.com/releasenotes/Cocoa/AppKitOlderNotes.html>
1157 // Scroll to the section headed "NSScrollWheel events".
1158 //
1159 // P.S. The "smooth scrolling" option in the system preferences is utterly
1160 // unrelated to any of this.
1161
1162 CGEventRef cgEvent = [event CGEvent];
1163 ASSERT(cgEvent);
1164
1165 // Wheel ticks are supposed to be raw, unaccelerated values, one per physical
1166 // mouse wheel notch. The delta event is perfect for this (being a good
1167 // "specific edge case" as mentioned above). Trackpads, unfortunately, do
1168 // event chunking, and sending mousewheel events with 0 ticks causes some
1169 // websites to malfunction. Therefore, for all continuous input devices we use
1170 // the point delta data instead, since we cannot distinguish trackpad data
1171 // from data from any other continuous device.
1172
1173 if (CGEventGetIntegerValueField(cgEvent, kCGScrollWheelEventIsContinuous)) {
1174 result.wheelTicksY = result.deltaY =
1175 CGEventGetIntegerValueField(cgEvent, kCGScrollWheelEventPointDeltaAxis1);
1176 result.wheelTicksX = result.deltaX =
1177 CGEventGetIntegerValueField(cgEvent, kCGScrollWheelEventPointDeltaAxis2);
1178 } else {
1179 result.wheelTicksY =
1180 CGEventGetIntegerValueField(cgEvent, kCGScrollWheelEventDeltaAxis1);
1181 result.wheelTicksX =
1182 CGEventGetIntegerValueField(cgEvent, kCGScrollWheelEventDeltaAxis2);
1183
1184 // Convert wheel delta amount to a number of pixels to scroll.
1185 static const double scrollbarPixelsPerCocoaTick = 40.0;
1186
1187 result.deltaX = [event deltaX] * scrollbarPixelsPerCocoaTick;
1188 result.deltaY = [event deltaY] * scrollbarPixelsPerCocoaTick;
1189 }
1190
1191 result.timeStampSeconds = [event timestamp];
1192
1193 return result;
1194 }
1195
1196 } // namespace WebKit
OLDNEW
« no previous file with comments | « webkit/api/src/linux/WebFontRendering.cpp ('k') | webkit/api/src/mac/WebScreenInfoFactory.mm » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698