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