WebEventFactory.mm revision 2bde8e466a4451c7319e3a072d118917957d6554
1/*
2 * Copyright (C) 2010 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 *    notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 *    notice, this list of conditions and the following disclaimer in the
11 *    documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#import "config.h"
27#import "WebEventFactory.h"
28
29#import "WebKitSystemInterface.h"
30#import <wtf/ASCIICType.h>
31#import <WebCore/Scrollbar.h>
32
33using namespace WebCore;
34
35namespace WebKit {
36
37static WebMouseEvent::Button currentMouseButton()
38{
39    NSUInteger pressedMouseButtons = [NSEvent pressedMouseButtons];
40    if (!pressedMouseButtons)
41        return WebMouseEvent::NoButton;
42    if (pressedMouseButtons == 1 << 0)
43        return WebMouseEvent::LeftButton;
44    if (pressedMouseButtons == 1 << 1)
45        return WebMouseEvent::RightButton;
46    return WebMouseEvent::MiddleButton;
47}
48
49static WebMouseEvent::Button mouseButtonForEvent(NSEvent *event)
50{
51    switch ([event type]) {
52        case NSLeftMouseDown:
53        case NSLeftMouseUp:
54        case NSLeftMouseDragged:
55            return WebMouseEvent::LeftButton;
56        case NSRightMouseDown:
57        case NSRightMouseUp:
58        case NSRightMouseDragged:
59            return WebMouseEvent::RightButton;
60        case NSOtherMouseDown:
61        case NSOtherMouseUp:
62        case NSOtherMouseDragged:
63            return WebMouseEvent::MiddleButton;
64        case NSMouseEntered:
65        case NSMouseExited:
66            return currentMouseButton();
67        default:
68            return WebMouseEvent::NoButton;
69    }
70}
71
72static WebEvent::Type mouseEventTypeForEvent(NSEvent* event)
73{
74    switch ([event type]) {
75        case NSLeftMouseDragged:
76        case NSMouseEntered:
77        case NSMouseExited:
78        case NSMouseMoved:
79        case NSOtherMouseDragged:
80        case NSRightMouseDragged:
81            return WebEvent::MouseMove;
82        case NSLeftMouseDown:
83        case NSRightMouseDown:
84        case NSOtherMouseDown:
85            return WebEvent::MouseDown;
86        case NSLeftMouseUp:
87        case NSRightMouseUp:
88        case NSOtherMouseUp:
89            return WebEvent::MouseUp;
90        default:
91            return WebEvent::MouseMove;
92    }
93}
94
95static int clickCountForEvent(NSEvent *event)
96{
97    switch ([event type]) {
98        case NSLeftMouseDown:
99        case NSLeftMouseUp:
100        case NSLeftMouseDragged:
101        case NSRightMouseDown:
102        case NSRightMouseUp:
103        case NSRightMouseDragged:
104        case NSOtherMouseDown:
105        case NSOtherMouseUp:
106        case NSOtherMouseDragged:
107            return [event clickCount];
108        default:
109            return 0;
110    }
111}
112
113static NSScreen *screenForWindow(NSWindow *window)
114{
115    NSScreen *screen = [window screen]; // nil if the window is off-screen
116    if (screen)
117        return screen;
118
119    NSArray *screens = [NSScreen screens];
120    if ([screens count] > 0)
121        return [screens objectAtIndex:0]; // screen containing the menubar
122
123    return nil;
124}
125
126static NSPoint flipScreenPoint(const NSPoint& screenPoint, NSScreen *screen)
127{
128    NSPoint flippedPoint = screenPoint;
129    flippedPoint.y = NSMaxY([screen frame]) - flippedPoint.y;
130    return flippedPoint;
131}
132
133static NSPoint globalPoint(const NSPoint& windowPoint, NSWindow *window)
134{
135    return flipScreenPoint([window convertBaseToScreen:windowPoint], screenForWindow(window));
136}
137
138static NSPoint globalPointForEvent(NSEvent *event)
139{
140    switch ([event type]) {
141        case NSLeftMouseDown:
142        case NSLeftMouseDragged:
143        case NSLeftMouseUp:
144        case NSMouseEntered:
145        case NSMouseExited:
146        case NSMouseMoved:
147        case NSOtherMouseDown:
148        case NSOtherMouseDragged:
149        case NSOtherMouseUp:
150        case NSRightMouseDown:
151        case NSRightMouseDragged:
152        case NSRightMouseUp:
153        case NSScrollWheel:
154            return globalPoint([event locationInWindow], [event window]);
155        default:
156            return NSZeroPoint;
157    }
158}
159
160static NSPoint pointForEvent(NSEvent *event, NSView *windowView)
161{
162    switch ([event type]) {
163        case NSLeftMouseDown:
164        case NSLeftMouseDragged:
165        case NSLeftMouseUp:
166        case NSMouseEntered:
167        case NSMouseExited:
168        case NSMouseMoved:
169        case NSOtherMouseDown:
170        case NSOtherMouseDragged:
171        case NSOtherMouseUp:
172        case NSRightMouseDown:
173        case NSRightMouseDragged:
174        case NSRightMouseUp:
175        case NSScrollWheel: {
176            // Note: This will have its origin at the bottom left of the window unless windowView is flipped.
177            // In those cases, the Y coordinate gets flipped by Widget::convertFromContainingWindow.
178            NSPoint location = [event locationInWindow];
179            if (windowView)
180                location = [windowView convertPoint:location fromView:nil];
181            return location;
182        }
183        default:
184            return NSZeroPoint;
185    }
186}
187
188static WebWheelEvent::Phase phaseForEvent(NSEvent *event)
189{
190#if !defined(BUILDING_ON_SNOW_LEOPARD)
191    uint32_t phase = WebWheelEvent::PhaseNone;
192    if ([event phase] & NSEventPhaseBegan)
193        phase |= WebWheelEvent::PhaseBegan;
194    if ([event phase] & NSEventPhaseStationary)
195        phase |= WebWheelEvent::PhaseStationary;
196    if ([event phase] & NSEventPhaseChanged)
197        phase |= WebWheelEvent::PhaseChanged;
198    if ([event phase] & NSEventPhaseEnded)
199        phase |= WebWheelEvent::PhaseEnded;
200    if ([event phase] & NSEventPhaseCancelled)
201        phase |= WebWheelEvent::PhaseCancelled;
202    return static_cast<WebWheelEvent::Phase>(phase);
203#else
204    return WebWheelEvent::PhaseNone;
205#endif
206}
207
208static WebWheelEvent::Phase momentumPhaseForEvent(NSEvent *event)
209{
210#if !defined(BUILDING_ON_SNOW_LEOPARD)
211    uint32_t phase = WebWheelEvent::PhaseNone;
212    if ([event momentumPhase] & NSEventPhaseBegan)
213        phase |= WebWheelEvent::PhaseBegan;
214    if ([event momentumPhase] & NSEventPhaseStationary)
215        phase |= WebWheelEvent::PhaseStationary;
216    if ([event momentumPhase] & NSEventPhaseChanged)
217        phase |= WebWheelEvent::PhaseChanged;
218    if ([event momentumPhase] & NSEventPhaseEnded)
219        phase |= WebWheelEvent::PhaseEnded;
220    if ([event momentumPhase] & NSEventPhaseCancelled)
221        phase |= WebWheelEvent::PhaseCancelled;
222    return static_cast<WebWheelEvent::Phase>(phase);
223#else
224    return WebWheelEvent::PhaseNone;
225#endif
226}
227
228#if ENABLE(GESTURE_EVENTS)
229static WebEvent::Type gestureEventTypeForEvent(NSEvent *event)
230{
231    switch ([event type]) {
232    case NSEventTypeBeginGesture:
233        return WebEvent::GestureScrollBegin;
234    case NSEventTypeEndGesture:
235        return WebEvent::GestureScrollEnd;
236    default:
237        ASSERT_NOT_REACHED();
238        return WebEvent::GestureScrollEnd;
239    }
240}
241#endif
242
243static inline String textFromEvent(NSEvent* event)
244{
245    if ([event type] == NSFlagsChanged)
246        return String("");
247    return String([event characters]);
248}
249
250static inline String unmodifiedTextFromEvent(NSEvent* event)
251{
252    if ([event type] == NSFlagsChanged)
253        return String("");
254    return String([event charactersIgnoringModifiers]);
255}
256
257static String keyIdentifierForKeyEvent(NSEvent* event)
258{
259    if ([event type] == NSFlagsChanged)
260        switch ([event keyCode]) {
261            case 54: // Right Command
262            case 55: // Left Command
263                return String("Meta");
264
265            case 57: // Capslock
266                return String("CapsLock");
267
268            case 56: // Left Shift
269            case 60: // Right Shift
270                return String("Shift");
271
272            case 58: // Left Alt
273            case 61: // Right Alt
274                return String("Alt");
275
276            case 59: // Left Ctrl
277            case 62: // Right Ctrl
278                return String("Control");
279
280            default:
281                ASSERT_NOT_REACHED();
282                return String("");
283        }
284
285    NSString *s = [event charactersIgnoringModifiers];
286    if ([s length] != 1)
287        return String("Unidentified");
288
289    unichar c = [s characterAtIndex:0];
290    switch (c) {
291        // Each identifier listed in the DOM spec is listed here.
292        // Many are simply commented out since they do not appear on standard Macintosh keyboards
293        // or are on a key that doesn't have a corresponding character.
294
295        // "Accept"
296        // "AllCandidates"
297
298        // "Alt"
299        case NSMenuFunctionKey:
300            return String("Alt");
301
302        // "Apps"
303        // "BrowserBack"
304        // "BrowserForward"
305        // "BrowserHome"
306        // "BrowserRefresh"
307        // "BrowserSearch"
308        // "BrowserStop"
309        // "CapsLock"
310
311        // "Clear"
312        case NSClearLineFunctionKey:
313            return String("Clear");
314
315        // "CodeInput"
316        // "Compose"
317        // "Control"
318        // "Crsel"
319        // "Convert"
320        // "Copy"
321        // "Cut"
322
323        // "Down"
324        case NSDownArrowFunctionKey:
325            return String("Down");
326        // "End"
327        case NSEndFunctionKey:
328            return String("End");
329        // "Enter"
330        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
331            return String("Enter");
332
333        // "EraseEof"
334
335        // "Execute"
336        case NSExecuteFunctionKey:
337            return String("Execute");
338
339        // "Exsel"
340
341        // "F1"
342        case NSF1FunctionKey:
343            return String("F1");
344        // "F2"
345        case NSF2FunctionKey:
346            return String("F2");
347        // "F3"
348        case NSF3FunctionKey:
349            return String("F3");
350        // "F4"
351        case NSF4FunctionKey:
352            return String("F4");
353        // "F5"
354        case NSF5FunctionKey:
355            return String("F5");
356        // "F6"
357        case NSF6FunctionKey:
358            return String("F6");
359        // "F7"
360        case NSF7FunctionKey:
361            return String("F7");
362        // "F8"
363        case NSF8FunctionKey:
364            return String("F8");
365        // "F9"
366        case NSF9FunctionKey:
367            return String("F9");
368        // "F10"
369        case NSF10FunctionKey:
370            return String("F10");
371        // "F11"
372        case NSF11FunctionKey:
373            return String("F11");
374        // "F12"
375        case NSF12FunctionKey:
376            return String("F12");
377        // "F13"
378        case NSF13FunctionKey:
379            return String("F13");
380        // "F14"
381        case NSF14FunctionKey:
382            return String("F14");
383        // "F15"
384        case NSF15FunctionKey:
385            return String("F15");
386        // "F16"
387        case NSF16FunctionKey:
388            return String("F16");
389        // "F17"
390        case NSF17FunctionKey:
391            return String("F17");
392        // "F18"
393        case NSF18FunctionKey:
394            return String("F18");
395        // "F19"
396        case NSF19FunctionKey:
397            return String("F19");
398        // "F20"
399        case NSF20FunctionKey:
400            return String("F20");
401        // "F21"
402        case NSF21FunctionKey:
403            return String("F21");
404        // "F22"
405        case NSF22FunctionKey:
406            return String("F22");
407        // "F23"
408        case NSF23FunctionKey:
409            return String("F23");
410        // "F24"
411        case NSF24FunctionKey:
412            return String("F24");
413
414        // "FinalMode"
415
416        // "Find"
417        case NSFindFunctionKey:
418            return String("Find");
419
420        // "FullWidth"
421        // "HalfWidth"
422        // "HangulMode"
423        // "HanjaMode"
424
425        // "Help"
426        case NSHelpFunctionKey:
427            return String("Help");
428
429        // "Hiragana"
430
431        // "Home"
432        case NSHomeFunctionKey:
433            return String("Home");
434        // "Insert"
435        case NSInsertFunctionKey:
436            return String("Insert");
437
438        // "JapaneseHiragana"
439        // "JapaneseKatakana"
440        // "JapaneseRomaji"
441        // "JunjaMode"
442        // "KanaMode"
443        // "KanjiMode"
444        // "Katakana"
445        // "LaunchApplication1"
446        // "LaunchApplication2"
447        // "LaunchMail"
448
449        // "Left"
450        case NSLeftArrowFunctionKey:
451            return String("Left");
452
453        // "Meta"
454        // "MediaNextTrack"
455        // "MediaPlayPause"
456        // "MediaPreviousTrack"
457        // "MediaStop"
458
459        // "ModeChange"
460        case NSModeSwitchFunctionKey:
461            return String("ModeChange");
462
463        // "Nonconvert"
464        // "NumLock"
465
466        // "PageDown"
467        case NSPageDownFunctionKey:
468            return String("PageDown");
469        // "PageUp"
470        case NSPageUpFunctionKey:
471            return String("PageUp");
472
473        // "Paste"
474
475        // "Pause"
476        case NSPauseFunctionKey:
477            return String("Pause");
478
479        // "Play"
480        // "PreviousCandidate"
481
482        // "PrintScreen"
483        case NSPrintScreenFunctionKey:
484            return String("PrintScreen");
485
486        // "Process"
487        // "Props"
488
489        // "Right"
490        case NSRightArrowFunctionKey:
491            return String("Right");
492
493        // "RomanCharacters"
494
495        // "Scroll"
496        case NSScrollLockFunctionKey:
497            return String("Scroll");
498        // "Select"
499        case NSSelectFunctionKey:
500            return String("Select");
501
502        // "SelectMedia"
503        // "Shift"
504
505        // "Stop"
506        case NSStopFunctionKey:
507            return String("Stop");
508        // "Up"
509        case NSUpArrowFunctionKey:
510            return String("Up");
511        // "Undo"
512        case NSUndoFunctionKey:
513            return String("Undo");
514
515        // "VolumeDown"
516        // "VolumeMute"
517        // "VolumeUp"
518        // "Win"
519        // "Zoom"
520
521        // More function keys, not in the key identifier specification.
522        case NSF25FunctionKey:
523            return String("F25");
524        case NSF26FunctionKey:
525            return String("F26");
526        case NSF27FunctionKey:
527            return String("F27");
528        case NSF28FunctionKey:
529            return String("F28");
530        case NSF29FunctionKey:
531            return String("F29");
532        case NSF30FunctionKey:
533            return String("F30");
534        case NSF31FunctionKey:
535            return String("F31");
536        case NSF32FunctionKey:
537            return String("F32");
538        case NSF33FunctionKey:
539            return String("F33");
540        case NSF34FunctionKey:
541            return String("F34");
542        case NSF35FunctionKey:
543            return String("F35");
544
545        // Turn 0x7F into 0x08, because backspace needs to always be 0x08.
546        case 0x7F:
547            return String("U+0008");
548        // Standard says that DEL becomes U+007F.
549        case NSDeleteFunctionKey:
550            return String("U+007F");
551
552        // Always use 0x09 for tab instead of AppKit's backtab character.
553        case NSBackTabCharacter:
554            return String("U+0009");
555
556        case NSBeginFunctionKey:
557        case NSBreakFunctionKey:
558        case NSClearDisplayFunctionKey:
559        case NSDeleteCharFunctionKey:
560        case NSDeleteLineFunctionKey:
561        case NSInsertCharFunctionKey:
562        case NSInsertLineFunctionKey:
563        case NSNextFunctionKey:
564        case NSPrevFunctionKey:
565        case NSPrintFunctionKey:
566        case NSRedoFunctionKey:
567        case NSResetFunctionKey:
568        case NSSysReqFunctionKey:
569        case NSSystemFunctionKey:
570        case NSUserFunctionKey:
571            // FIXME: We should use something other than the vendor-area Unicode values for the above keys.
572            // For now, just fall through to the default.
573        default:
574            return String::format("U+%04X", toASCIIUpper(c));
575    }
576}
577
578static bool isKeypadEvent(NSEvent* event)
579{
580    // Check that this is the type of event that has a keyCode.
581    switch ([event type]) {
582        case NSKeyDown:
583        case NSKeyUp:
584        case NSFlagsChanged:
585            break;
586        default:
587            return false;
588    }
589
590    switch ([event keyCode]) {
591        case 71: // Clear
592        case 81: // =
593        case 75: // /
594        case 67: // *
595        case 78: // -
596        case 69: // +
597        case 76: // Enter
598        case 65: // .
599        case 82: // 0
600        case 83: // 1
601        case 84: // 2
602        case 85: // 3
603        case 86: // 4
604        case 87: // 5
605        case 88: // 6
606        case 89: // 7
607        case 91: // 8
608        case 92: // 9
609            return true;
610     }
611
612     return false;
613}
614
615static int windowsKeyCodeForKeyEvent(NSEvent* event)
616{
617    switch ([event keyCode]) {
618        // VK_TAB (09) TAB key
619        case 48: return 0x09;
620
621        // VK_APPS (5D) Right windows/meta key
622        case 54: // Right Command
623            return 0x5D;
624
625        // VK_LWIN (5B) Left windows/meta key
626        case 55: // Left Command
627            return 0x5B;
628
629        // VK_CAPITAL (14) caps locks key
630        case 57: // Capslock
631            return 0x14;
632
633        // VK_SHIFT (10) either shift key
634        case 56: // Left Shift
635        case 60: // Right Shift
636            return 0x10;
637
638        // VK_MENU (12) either alt key
639        case 58: // Left Alt
640        case 61: // Right Alt
641            return 0x12;
642
643        // VK_CONTROL (11) either ctrl key
644        case 59: // Left Ctrl
645        case 62: // Right Ctrl
646            return 0x11;
647
648        // VK_CLEAR (0C) CLEAR key
649        case 71: return 0x0C;
650
651        // VK_NUMPAD0 (60) Numeric keypad 0 key
652        case 82: return 0x60;
653        // VK_NUMPAD1 (61) Numeric keypad 1 key
654        case 83: return 0x61;
655        // VK_NUMPAD2 (62) Numeric keypad 2 key
656        case 84: return 0x62;
657        // VK_NUMPAD3 (63) Numeric keypad 3 key
658        case 85: return 0x63;
659        // VK_NUMPAD4 (64) Numeric keypad 4 key
660        case 86: return 0x64;
661        // VK_NUMPAD5 (65) Numeric keypad 5 key
662        case 87: return 0x65;
663        // VK_NUMPAD6 (66) Numeric keypad 6 key
664        case 88: return 0x66;
665        // VK_NUMPAD7 (67) Numeric keypad 7 key
666        case 89: return 0x67;
667        // VK_NUMPAD8 (68) Numeric keypad 8 key
668        case 91: return 0x68;
669        // VK_NUMPAD9 (69) Numeric keypad 9 key
670        case 92: return 0x69;
671        // VK_MULTIPLY (6A) Multiply key
672        case 67: return 0x6A;
673        // VK_ADD (6B) Add key
674        case 69: return 0x6B;
675
676        // VK_SUBTRACT (6D) Subtract key
677        case 78: return 0x6D;
678        // VK_DECIMAL (6E) Decimal key
679        case 65: return 0x6E;
680        // VK_DIVIDE (6F) Divide key
681        case 75: return 0x6F;
682     }
683
684    NSString* s = [event charactersIgnoringModifiers];
685    if ([s length] != 1)
686        return 0;
687
688    switch ([s characterAtIndex:0]) {
689        // VK_LBUTTON (01) Left mouse button
690        // VK_RBUTTON (02) Right mouse button
691        // VK_CANCEL (03) Control-break processing
692        // VK_MBUTTON (04) Middle mouse button (three-button mouse)
693        // VK_XBUTTON1 (05)
694        // VK_XBUTTON2 (06)
695
696        // VK_BACK (08) BACKSPACE key
697        case 8: case 0x7F: return 0x08;
698        // VK_TAB (09) TAB key
699        case 9: return 0x09;
700
701        // VK_CLEAR (0C) CLEAR key
702        // handled by key code above
703
704        // VK_RETURN (0D)
705        case 0xD: case 3: return 0x0D;
706
707        // VK_SHIFT (10) SHIFT key
708        // VK_CONTROL (11) CTRL key
709        // VK_MENU (12) ALT key
710
711        // VK_PAUSE (13) PAUSE key
712        case NSPauseFunctionKey: return 0x13;
713
714        // VK_CAPITAL (14) CAPS LOCK key
715        // VK_KANA (15) Input Method Editor (IME) Kana mode
716        // VK_HANGUEL (15) IME Hanguel mode (maintained for compatibility; use VK_HANGUL)
717        // VK_HANGUL (15) IME Hangul mode
718        // VK_JUNJA (17) IME Junja mode
719        // VK_FINAL (18) IME final mode
720        // VK_HANJA (19) IME Hanja mode
721        // VK_KANJI (19) IME Kanji mode
722
723        // VK_ESCAPE (1B) ESC key
724        case 0x1B: return 0x1B;
725
726        // VK_CONVERT (1C) IME convert
727        // VK_NONCONVERT (1D) IME nonconvert
728        // VK_ACCEPT (1E) IME accept
729        // VK_MODECHANGE (1F) IME mode change request
730
731        // VK_SPACE (20) SPACEBAR
732        case ' ': return 0x20;
733        // VK_PRIOR (21) PAGE UP key
734        case NSPageUpFunctionKey: return 0x21;
735        // VK_NEXT (22) PAGE DOWN key
736        case NSPageDownFunctionKey: return 0x22;
737        // VK_END (23) END key
738        case NSEndFunctionKey: return 0x23;
739        // VK_HOME (24) HOME key
740        case NSHomeFunctionKey: return 0x24;
741        // VK_LEFT (25) LEFT ARROW key
742        case NSLeftArrowFunctionKey: return 0x25;
743        // VK_UP (26) UP ARROW key
744        case NSUpArrowFunctionKey: return 0x26;
745        // VK_RIGHT (27) RIGHT ARROW key
746        case NSRightArrowFunctionKey: return 0x27;
747        // VK_DOWN (28) DOWN ARROW key
748        case NSDownArrowFunctionKey: return 0x28;
749        // VK_SELECT (29) SELECT key
750        case NSSelectFunctionKey: return 0x29;
751        // VK_PRINT (2A) PRINT key
752        case NSPrintFunctionKey: return 0x2A;
753        // VK_EXECUTE (2B) EXECUTE key
754        case NSExecuteFunctionKey: return 0x2B;
755        // VK_SNAPSHOT (2C) PRINT SCREEN key
756        case NSPrintScreenFunctionKey: return 0x2C;
757        // VK_INSERT (2D) INS key
758        case NSInsertFunctionKey: case NSHelpFunctionKey: return 0x2D;
759        // VK_DELETE (2E) DEL key
760        case NSDeleteFunctionKey: return 0x2E;
761
762        // VK_HELP (2F) HELP key
763
764        //  (30) 0 key
765        case '0': case ')': return 0x30;
766        //  (31) 1 key
767        case '1': case '!': return 0x31;
768        //  (32) 2 key
769        case '2': case '@': return 0x32;
770        //  (33) 3 key
771        case '3': case '#': return 0x33;
772        //  (34) 4 key
773        case '4': case '$': return 0x34;
774        //  (35) 5 key
775        case '5': case '%': return 0x35;
776        //  (36) 6 key
777        case '6': case '^': return 0x36;
778        //  (37) 7 key
779        case '7': case '&': return 0x37;
780        //  (38) 8 key
781        case '8': case '*': return 0x38;
782        //  (39) 9 key
783        case '9': case '(': return 0x39;
784        //  (41) A key
785        case 'a': case 'A': return 0x41;
786        //  (42) B key
787        case 'b': case 'B': return 0x42;
788        //  (43) C key
789        case 'c': case 'C': return 0x43;
790        //  (44) D key
791        case 'd': case 'D': return 0x44;
792        //  (45) E key
793        case 'e': case 'E': return 0x45;
794        //  (46) F key
795        case 'f': case 'F': return 0x46;
796        //  (47) G key
797        case 'g': case 'G': return 0x47;
798        //  (48) H key
799        case 'h': case 'H': return 0x48;
800        //  (49) I key
801        case 'i': case 'I': return 0x49;
802        //  (4A) J key
803        case 'j': case 'J': return 0x4A;
804        //  (4B) K key
805        case 'k': case 'K': return 0x4B;
806        //  (4C) L key
807        case 'l': case 'L': return 0x4C;
808        //  (4D) M key
809        case 'm': case 'M': return 0x4D;
810        //  (4E) N key
811        case 'n': case 'N': return 0x4E;
812        //  (4F) O key
813        case 'o': case 'O': return 0x4F;
814        //  (50) P key
815        case 'p': case 'P': return 0x50;
816        //  (51) Q key
817        case 'q': case 'Q': return 0x51;
818        //  (52) R key
819        case 'r': case 'R': return 0x52;
820        //  (53) S key
821        case 's': case 'S': return 0x53;
822        //  (54) T key
823        case 't': case 'T': return 0x54;
824        //  (55) U key
825        case 'u': case 'U': return 0x55;
826        //  (56) V key
827        case 'v': case 'V': return 0x56;
828        //  (57) W key
829        case 'w': case 'W': return 0x57;
830        //  (58) X key
831        case 'x': case 'X': return 0x58;
832        //  (59) Y key
833        case 'y': case 'Y': return 0x59;
834        //  (5A) Z key
835        case 'z': case 'Z': return 0x5A;
836
837        // VK_LWIN (5B) Left Windows key (Microsoft Natural keyboard)
838        // VK_RWIN (5C) Right Windows key (Natural keyboard)
839        // VK_APPS (5D) Applications key (Natural keyboard)
840        // VK_SLEEP (5F) Computer Sleep key
841
842        // VK_NUMPAD0 (60) Numeric keypad 0 key
843        // VK_NUMPAD1 (61) Numeric keypad 1 key
844        // VK_NUMPAD2 (62) Numeric keypad 2 key
845        // VK_NUMPAD3 (63) Numeric keypad 3 key
846        // VK_NUMPAD4 (64) Numeric keypad 4 key
847        // VK_NUMPAD5 (65) Numeric keypad 5 key
848        // VK_NUMPAD6 (66) Numeric keypad 6 key
849        // VK_NUMPAD7 (67) Numeric keypad 7 key
850        // VK_NUMPAD8 (68) Numeric keypad 8 key
851        // VK_NUMPAD9 (69) Numeric keypad 9 key
852        // VK_MULTIPLY (6A) Multiply key
853        // VK_ADD (6B) Add key
854        // handled by key code above
855
856        // VK_SEPARATOR (6C) Separator key
857
858        // VK_SUBTRACT (6D) Subtract key
859        // VK_DECIMAL (6E) Decimal key
860        // VK_DIVIDE (6F) Divide key
861        // handled by key code above
862
863        // VK_F1 (70) F1 key
864        case NSF1FunctionKey: return 0x70;
865        // VK_F2 (71) F2 key
866        case NSF2FunctionKey: return 0x71;
867        // VK_F3 (72) F3 key
868        case NSF3FunctionKey: return 0x72;
869        // VK_F4 (73) F4 key
870        case NSF4FunctionKey: return 0x73;
871        // VK_F5 (74) F5 key
872        case NSF5FunctionKey: return 0x74;
873        // VK_F6 (75) F6 key
874        case NSF6FunctionKey: return 0x75;
875        // VK_F7 (76) F7 key
876        case NSF7FunctionKey: return 0x76;
877        // VK_F8 (77) F8 key
878        case NSF8FunctionKey: return 0x77;
879        // VK_F9 (78) F9 key
880        case NSF9FunctionKey: return 0x78;
881        // VK_F10 (79) F10 key
882        case NSF10FunctionKey: return 0x79;
883        // VK_F11 (7A) F11 key
884        case NSF11FunctionKey: return 0x7A;
885        // VK_F12 (7B) F12 key
886        case NSF12FunctionKey: return 0x7B;
887        // VK_F13 (7C) F13 key
888        case NSF13FunctionKey: return 0x7C;
889        // VK_F14 (7D) F14 key
890        case NSF14FunctionKey: return 0x7D;
891        // VK_F15 (7E) F15 key
892        case NSF15FunctionKey: return 0x7E;
893        // VK_F16 (7F) F16 key
894        case NSF16FunctionKey: return 0x7F;
895        // VK_F17 (80H) F17 key
896        case NSF17FunctionKey: return 0x80;
897        // VK_F18 (81H) F18 key
898        case NSF18FunctionKey: return 0x81;
899        // VK_F19 (82H) F19 key
900        case NSF19FunctionKey: return 0x82;
901        // VK_F20 (83H) F20 key
902        case NSF20FunctionKey: return 0x83;
903        // VK_F21 (84H) F21 key
904        case NSF21FunctionKey: return 0x84;
905        // VK_F22 (85H) F22 key
906        case NSF22FunctionKey: return 0x85;
907        // VK_F23 (86H) F23 key
908        case NSF23FunctionKey: return 0x86;
909        // VK_F24 (87H) F24 key
910        case NSF24FunctionKey: return 0x87;
911
912        // VK_NUMLOCK (90) NUM LOCK key
913
914        // VK_SCROLL (91) SCROLL LOCK key
915        case NSScrollLockFunctionKey: return 0x91;
916
917        // VK_LSHIFT (A0) Left SHIFT key
918        // VK_RSHIFT (A1) Right SHIFT key
919        // VK_LCONTROL (A2) Left CONTROL key
920        // VK_RCONTROL (A3) Right CONTROL key
921        // VK_LMENU (A4) Left MENU key
922        // VK_RMENU (A5) Right MENU key
923        // VK_BROWSER_BACK (A6) Windows 2000/XP: Browser Back key
924        // VK_BROWSER_FORWARD (A7) Windows 2000/XP: Browser Forward key
925        // VK_BROWSER_REFRESH (A8) Windows 2000/XP: Browser Refresh key
926        // VK_BROWSER_STOP (A9) Windows 2000/XP: Browser Stop key
927        // VK_BROWSER_SEARCH (AA) Windows 2000/XP: Browser Search key
928        // VK_BROWSER_FAVORITES (AB) Windows 2000/XP: Browser Favorites key
929        // VK_BROWSER_HOME (AC) Windows 2000/XP: Browser Start and Home key
930        // VK_VOLUME_MUTE (AD) Windows 2000/XP: Volume Mute key
931        // VK_VOLUME_DOWN (AE) Windows 2000/XP: Volume Down key
932        // VK_VOLUME_UP (AF) Windows 2000/XP: Volume Up key
933        // VK_MEDIA_NEXT_TRACK (B0) Windows 2000/XP: Next Track key
934        // VK_MEDIA_PREV_TRACK (B1) Windows 2000/XP: Previous Track key
935        // VK_MEDIA_STOP (B2) Windows 2000/XP: Stop Media key
936        // VK_MEDIA_PLAY_PAUSE (B3) Windows 2000/XP: Play/Pause Media key
937        // VK_LAUNCH_MAIL (B4) Windows 2000/XP: Start Mail key
938        // VK_LAUNCH_MEDIA_SELECT (B5) Windows 2000/XP: Select Media key
939        // VK_LAUNCH_APP1 (B6) Windows 2000/XP: Start Application 1 key
940        // VK_LAUNCH_APP2 (B7) Windows 2000/XP: Start Application 2 key
941
942        // VK_OEM_1 (BA) Used for miscellaneous characters; it can vary by keyboard. Windows 2000/XP: For the US standard keyboard, the ';:' key
943        case ';': case ':': return 0xBA;
944        // VK_OEM_PLUS (BB) Windows 2000/XP: For any country/region, the '+' key
945        case '=': case '+': return 0xBB;
946        // VK_OEM_COMMA (BC) Windows 2000/XP: For any country/region, the ',' key
947        case ',': case '<': return 0xBC;
948        // VK_OEM_MINUS (BD) Windows 2000/XP: For any country/region, the '-' key
949        case '-': case '_': return 0xBD;
950        // VK_OEM_PERIOD (BE) Windows 2000/XP: For any country/region, the '.' key
951        case '.': case '>': return 0xBE;
952        // VK_OEM_2 (BF) Used for miscellaneous characters; it can vary by keyboard. Windows 2000/XP: For the US standard keyboard, the '/?' key
953        case '/': case '?': return 0xBF;
954        // VK_OEM_3 (C0) Used for miscellaneous characters; it can vary by keyboard. Windows 2000/XP: For the US standard keyboard, the '`~' key
955        case '`': case '~': return 0xC0;
956        // VK_OEM_4 (DB) Used for miscellaneous characters; it can vary by keyboard. Windows 2000/XP: For the US standard keyboard, the '[{' key
957        case '[': case '{': return 0xDB;
958        // VK_OEM_5 (DC) Used for miscellaneous characters; it can vary by keyboard. Windows 2000/XP: For the US standard keyboard, the '\|' key
959        case '\\': case '|': return 0xDC;
960        // VK_OEM_6 (DD) Used for miscellaneous characters; it can vary by keyboard. Windows 2000/XP: For the US standard keyboard, the ']}' key
961        case ']': case '}': return 0xDD;
962        // 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
963        case '\'': case '"': return 0xDE;
964
965        // VK_OEM_8 (DF) Used for miscellaneous characters; it can vary by keyboard.
966        // VK_OEM_102 (E2) Windows 2000/XP: Either the angle bracket key or the backslash key on the RT 102-key keyboard
967        // VK_PROCESSKEY (E5) Windows 95/98/Me, Windows NT 4.0, Windows 2000/XP: IME PROCESS key
968        // 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
969        // VK_ATTN (F6) Attn key
970        // VK_CRSEL (F7) CrSel key
971        // VK_EXSEL (F8) ExSel key
972        // VK_EREOF (F9) Erase EOF key
973        // VK_PLAY (FA) Play key
974        // VK_ZOOM (FB) Zoom key
975        // VK_NONAME (FC) Reserved for future use
976        // VK_PA1 (FD) PA1 key
977        // VK_OEM_CLEAR (FE) Clear key
978    }
979
980    return 0;
981}
982
983static inline bool isKeyUpEvent(NSEvent *event)
984{
985    if ([event type] != NSFlagsChanged)
986        return [event type] == NSKeyUp;
987    // FIXME: This logic fails if the user presses both Shift keys at once, for example:
988    // we treat releasing one of them as keyDown.
989    switch ([event keyCode]) {
990        case 54: // Right Command
991        case 55: // Left Command
992            return ([event modifierFlags] & NSCommandKeyMask) == 0;
993
994        case 57: // Capslock
995            return ([event modifierFlags] & NSAlphaShiftKeyMask) == 0;
996
997        case 56: // Left Shift
998        case 60: // Right Shift
999            return ([event modifierFlags] & NSShiftKeyMask) == 0;
1000
1001        case 58: // Left Alt
1002        case 61: // Right Alt
1003            return ([event modifierFlags] & NSAlternateKeyMask) == 0;
1004
1005        case 59: // Left Ctrl
1006        case 62: // Right Ctrl
1007            return ([event modifierFlags] & NSControlKeyMask) == 0;
1008
1009        case 63: // Function
1010            return ([event modifierFlags] & NSFunctionKeyMask) == 0;
1011    }
1012    return false;
1013}
1014
1015static inline WebEvent::Modifiers modifiersForEvent(NSEvent *event)
1016{
1017    unsigned modifiers = 0;
1018    if ([event modifierFlags] & NSShiftKeyMask)
1019        modifiers |= WebEvent::ShiftKey;
1020    if ([event modifierFlags] & NSControlKeyMask)
1021        modifiers |= WebEvent::ControlKey;
1022    if ([event modifierFlags] & NSAlternateKeyMask)
1023        modifiers |= WebEvent::AltKey;
1024    if ([event modifierFlags] & NSCommandKeyMask)
1025        modifiers |= WebEvent::MetaKey;
1026    return (WebEvent::Modifiers)modifiers;
1027}
1028
1029WebMouseEvent WebEventFactory::createWebMouseEvent(NSEvent *event, NSView *windowView)
1030{
1031    NSPoint position = pointForEvent(event, windowView);
1032    NSPoint globalPosition = globalPointForEvent(event);
1033
1034    WebEvent::Type type                     = mouseEventTypeForEvent(event);
1035    WebMouseEvent::Button button            = mouseButtonForEvent(event);
1036    float deltaX                            = [event deltaX];
1037    float deltaY                            = [event deltaY];
1038    float deltaZ                            = [event deltaZ];
1039    int clickCount                          = clickCountForEvent(event);
1040    WebEvent::Modifiers modifiers           = modifiersForEvent(event);
1041    double timestamp                        = [event timestamp];
1042
1043    return WebMouseEvent(type, button, IntPoint(position), IntPoint(globalPosition), deltaX, deltaY, deltaZ, clickCount, modifiers, timestamp);
1044}
1045
1046WebWheelEvent WebEventFactory::createWebWheelEvent(NSEvent *event, NSView *windowView)
1047{
1048    NSPoint position = pointForEvent(event, windowView);
1049    NSPoint globalPosition = globalPointForEvent(event);
1050
1051    WebWheelEvent::Granularity granularity  = WebWheelEvent::ScrollByPixelWheelEvent;
1052
1053    BOOL continuous;
1054    float deltaX = 0;
1055    float deltaY = 0;
1056    float wheelTicksX = 0;
1057    float wheelTicksY = 0;
1058
1059    WKGetWheelEventDeltas(event, &deltaX, &deltaY, &continuous);
1060
1061    if (continuous) {
1062        // smooth scroll events
1063        wheelTicksX = deltaX / static_cast<float>(Scrollbar::pixelsPerLineStep());
1064        wheelTicksY = deltaY / static_cast<float>(Scrollbar::pixelsPerLineStep());
1065    } else {
1066        // plain old wheel events
1067        wheelTicksX = deltaX;
1068        wheelTicksY = deltaY;
1069        deltaX *= static_cast<float>(Scrollbar::pixelsPerLineStep());
1070        deltaY *= static_cast<float>(Scrollbar::pixelsPerLineStep());
1071    }
1072
1073    WebWheelEvent::Phase phase              = phaseForEvent(event);
1074    WebWheelEvent::Phase momentumPhase      = momentumPhaseForEvent(event);
1075    bool hasPreciseScrollingDeltas          = continuous;
1076    WebEvent::Modifiers modifiers           = modifiersForEvent(event);
1077    double timestamp                        = [event timestamp];
1078
1079    return WebWheelEvent(WebEvent::Wheel, IntPoint(position), IntPoint(globalPosition), FloatSize(deltaX, deltaY), FloatSize(wheelTicksX, wheelTicksY), granularity, phase, momentumPhase, hasPreciseScrollingDeltas, modifiers, timestamp);
1080}
1081
1082WebKeyboardEvent WebEventFactory::createWebKeyboardEvent(NSEvent *event, NSView *)
1083{
1084    WebEvent::Type type             = isKeyUpEvent(event) ? WebEvent::KeyUp : WebEvent::KeyDown;
1085    String text                     = textFromEvent(event);
1086    String unmodifiedText           = unmodifiedTextFromEvent(event);
1087    String keyIdentifier            = keyIdentifierForKeyEvent(event);
1088    int windowsVirtualKeyCode       = windowsKeyCodeForKeyEvent(event);
1089    int nativeVirtualKeyCode        = [event keyCode];
1090    int macCharCode                 = WKGetNSEventKeyChar(event);
1091    bool autoRepeat                 = ([event type] != NSFlagsChanged) && [event isARepeat];
1092    bool isKeypad                   = isKeypadEvent(event);
1093    bool isSystemKey                = false; // SystemKey is always false on the Mac.
1094    WebEvent::Modifiers modifiers   = modifiersForEvent(event);
1095    double timestamp                = [event timestamp];
1096
1097    return WebKeyboardEvent(type, text, unmodifiedText, keyIdentifier, windowsVirtualKeyCode, nativeVirtualKeyCode, macCharCode, autoRepeat, isKeypad, isSystemKey, modifiers, timestamp);
1098}
1099
1100#if ENABLE(GESTURE_EVENTS)
1101WebGestureEvent WebEventFactory::createWebGestureEvent(NSEvent *event, NSView *windowView)
1102{
1103    WebEvent::Type type             = gestureEventTypeForEvent(event);
1104    NSPoint position                = pointForEvent(event, windowView);
1105    NSPoint globalPosition          = globalPointForEvent(event);
1106    WebEvent::Modifiers modifiers   = modifiersForEvent(event);
1107    double timestamp                = [event timestamp];
1108
1109    return WebGestureEvent(type, IntPoint(position), IntPoint(globalPosition), modifiers, timestamp);
1110}
1111#endif
1112
1113} // namespace WebKit
1114