| OLD | NEW |
| (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 "public/web/mac/WebInputEventFactory.h" | |
| 29 | |
| 30 #include <ApplicationServices/ApplicationServices.h> | |
| 31 #import <AvailabilityMacros.h> | |
| 32 #import <Cocoa/Cocoa.h> | |
| 33 | |
| 34 #include "platform/WindowsKeyboardCodes.h" | |
| 35 #include "public/web/WebInputEvent.h" | |
| 36 #include "wtf/ASCIICType.h" | |
| 37 | |
| 38 #if __MAC_OS_X_VERSION_MAX_ALLOWED == 1060 | |
| 39 | |
| 40 // Additional Lion APIs. | |
| 41 enum { | |
| 42 NSEventPhaseNone = 0, | |
| 43 NSEventPhaseBegan = 0x1 << 0, | |
| 44 NSEventPhaseStationary = 0x1 << 1, | |
| 45 NSEventPhaseChanged = 0x1 << 2, | |
| 46 NSEventPhaseEnded = 0x1 << 3, | |
| 47 NSEventPhaseCancelled = 0x1 << 4 | |
| 48 }; | |
| 49 typedef NSUInteger NSEventPhase; | |
| 50 | |
| 51 #endif // __MAC_OS_X_VERSION_MAX_ALLOWED == 1060 | |
| 52 | |
| 53 #if __MAC_OS_X_VERSION_MAX_ALLOWED < 1080 | |
| 54 | |
| 55 // Additional Mountain Lion APIs. | |
| 56 enum { | |
| 57 NSEventPhaseMayBegin = 0x1 << 5 | |
| 58 }; | |
| 59 | |
| 60 enum { | |
| 61 NSEventTypeSmartMagnify = 32 | |
| 62 }; | |
| 63 | |
| 64 #endif // __MAC_OS_X_VERSION_MAX_ALLOWED < 1080 | |
| 65 | |
| 66 // Redeclare methods only available on OSX 10.7+ to suppress -Wpartial-availabil
ity warnings. | |
| 67 | |
| 68 #if !defined(MAC_OS_X_VERSION_10_7) || MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_
VERSION_10_7 | |
| 69 | |
| 70 @interface NSEvent (LionSDKDeclarations) | |
| 71 - (NSEventPhase)phase; | |
| 72 - (NSEventPhase)momentumPhase; | |
| 73 @end | |
| 74 | |
| 75 #endif // !defined(MAC_OS_X_VERSION_10_7) || MAC_OS_X_VERSION_MIN_REQUIRED < MA
C_OS_X_VERSION_10_7 | |
| 76 | |
| 77 namespace blink { | |
| 78 | |
| 79 static int windowsKeyCodeForKeyCode(uint16_t keyCode) | |
| 80 { | |
| 81 static const int windowsKeyCode[] = { | |
| 82 /* 0 */ VK_A, | |
| 83 /* 1 */ VK_S, | |
| 84 /* 2 */ VK_D, | |
| 85 /* 3 */ VK_F, | |
| 86 /* 4 */ VK_H, | |
| 87 /* 5 */ VK_G, | |
| 88 /* 6 */ VK_Z, | |
| 89 /* 7 */ VK_X, | |
| 90 /* 8 */ VK_C, | |
| 91 /* 9 */ VK_V, | |
| 92 /* 0x0A */ VK_OEM_3, // "Section" - key to the left from 1 (ISO Keyboard
Only) | |
| 93 /* 0x0B */ VK_B, | |
| 94 /* 0x0C */ VK_Q, | |
| 95 /* 0x0D */ VK_W, | |
| 96 /* 0x0E */ VK_E, | |
| 97 /* 0x0F */ VK_R, | |
| 98 /* 0x10 */ VK_Y, | |
| 99 /* 0x11 */ VK_T, | |
| 100 /* 0x12 */ VK_1, | |
| 101 /* 0x13 */ VK_2, | |
| 102 /* 0x14 */ VK_3, | |
| 103 /* 0x15 */ VK_4, | |
| 104 /* 0x16 */ VK_6, | |
| 105 /* 0x17 */ VK_5, | |
| 106 /* 0x18 */ VK_OEM_PLUS, // =+ | |
| 107 /* 0x19 */ VK_9, | |
| 108 /* 0x1A */ VK_7, | |
| 109 /* 0x1B */ VK_OEM_MINUS, // -_ | |
| 110 /* 0x1C */ VK_8, | |
| 111 /* 0x1D */ VK_0, | |
| 112 /* 0x1E */ VK_OEM_6, // ]} | |
| 113 /* 0x1F */ VK_O, | |
| 114 /* 0x20 */ VK_U, | |
| 115 /* 0x21 */ VK_OEM_4, // {[ | |
| 116 /* 0x22 */ VK_I, | |
| 117 /* 0x23 */ VK_P, | |
| 118 /* 0x24 */ VK_RETURN, // Return | |
| 119 /* 0x25 */ VK_L, | |
| 120 /* 0x26 */ VK_J, | |
| 121 /* 0x27 */ VK_OEM_7, // '" | |
| 122 /* 0x28 */ VK_K, | |
| 123 /* 0x29 */ VK_OEM_1, // ;: | |
| 124 /* 0x2A */ VK_OEM_5, // \| | |
| 125 /* 0x2B */ VK_OEM_COMMA, // ,< | |
| 126 /* 0x2C */ VK_OEM_2, // /? | |
| 127 /* 0x2D */ VK_N, | |
| 128 /* 0x2E */ VK_M, | |
| 129 /* 0x2F */ VK_OEM_PERIOD, // .> | |
| 130 /* 0x30 */ VK_TAB, | |
| 131 /* 0x31 */ VK_SPACE, | |
| 132 /* 0x32 */ VK_OEM_3, // `~ | |
| 133 /* 0x33 */ VK_BACK, // Backspace | |
| 134 /* 0x34 */ 0, // n/a | |
| 135 /* 0x35 */ VK_ESCAPE, | |
| 136 /* 0x36 */ VK_APPS, // Right Command | |
| 137 /* 0x37 */ VK_LWIN, // Left Command | |
| 138 /* 0x38 */ VK_LSHIFT, // Left Shift | |
| 139 /* 0x39 */ VK_CAPITAL, // Caps Lock | |
| 140 /* 0x3A */ VK_LMENU, // Left Option | |
| 141 /* 0x3B */ VK_LCONTROL, // Left Ctrl | |
| 142 /* 0x3C */ VK_RSHIFT, // Right Shift | |
| 143 /* 0x3D */ VK_RMENU, // Right Option | |
| 144 /* 0x3E */ VK_RCONTROL, // Right Ctrl | |
| 145 /* 0x3F */ 0, // fn | |
| 146 /* 0x40 */ VK_F17, | |
| 147 /* 0x41 */ VK_DECIMAL, // Num Pad . | |
| 148 /* 0x42 */ 0, // n/a | |
| 149 /* 0x43 */ VK_MULTIPLY, // Num Pad * | |
| 150 /* 0x44 */ 0, // n/a | |
| 151 /* 0x45 */ VK_ADD, // Num Pad + | |
| 152 /* 0x46 */ 0, // n/a | |
| 153 /* 0x47 */ VK_CLEAR, // Num Pad Clear | |
| 154 /* 0x48 */ VK_VOLUME_UP, | |
| 155 /* 0x49 */ VK_VOLUME_DOWN, | |
| 156 /* 0x4A */ VK_VOLUME_MUTE, | |
| 157 /* 0x4B */ VK_DIVIDE, // Num Pad / | |
| 158 /* 0x4C */ VK_RETURN, // Num Pad Enter | |
| 159 /* 0x4D */ 0, // n/a | |
| 160 /* 0x4E */ VK_SUBTRACT, // Num Pad - | |
| 161 /* 0x4F */ VK_F18, | |
| 162 /* 0x50 */ VK_F19, | |
| 163 /* 0x51 */ VK_OEM_PLUS, // Num Pad =. There is no such key on common PC
keyboards, mapping to normal "+=". | |
| 164 /* 0x52 */ VK_NUMPAD0, | |
| 165 /* 0x53 */ VK_NUMPAD1, | |
| 166 /* 0x54 */ VK_NUMPAD2, | |
| 167 /* 0x55 */ VK_NUMPAD3, | |
| 168 /* 0x56 */ VK_NUMPAD4, | |
| 169 /* 0x57 */ VK_NUMPAD5, | |
| 170 /* 0x58 */ VK_NUMPAD6, | |
| 171 /* 0x59 */ VK_NUMPAD7, | |
| 172 /* 0x5A */ VK_F20, | |
| 173 /* 0x5B */ VK_NUMPAD8, | |
| 174 /* 0x5C */ VK_NUMPAD9, | |
| 175 /* 0x5D */ 0, // Yen (JIS Keyboard Only) | |
| 176 /* 0x5E */ 0, // Underscore (JIS Keyboard Only) | |
| 177 /* 0x5F */ 0, // KeypadComma (JIS Keyboard Only) | |
| 178 /* 0x60 */ VK_F5, | |
| 179 /* 0x61 */ VK_F6, | |
| 180 /* 0x62 */ VK_F7, | |
| 181 /* 0x63 */ VK_F3, | |
| 182 /* 0x64 */ VK_F8, | |
| 183 /* 0x65 */ VK_F9, | |
| 184 /* 0x66 */ 0, // Eisu (JIS Keyboard Only) | |
| 185 /* 0x67 */ VK_F11, | |
| 186 /* 0x68 */ 0, // Kana (JIS Keyboard Only) | |
| 187 /* 0x69 */ VK_F13, | |
| 188 /* 0x6A */ VK_F16, | |
| 189 /* 0x6B */ VK_F14, | |
| 190 /* 0x6C */ 0, // n/a | |
| 191 /* 0x6D */ VK_F10, | |
| 192 /* 0x6E */ 0, // n/a (Windows95 key?) | |
| 193 /* 0x6F */ VK_F12, | |
| 194 /* 0x70 */ 0, // n/a | |
| 195 /* 0x71 */ VK_F15, | |
| 196 /* 0x72 */ VK_INSERT, // Help | |
| 197 /* 0x73 */ VK_HOME, // Home | |
| 198 /* 0x74 */ VK_PRIOR, // Page Up | |
| 199 /* 0x75 */ VK_DELETE, // Forward Delete | |
| 200 /* 0x76 */ VK_F4, | |
| 201 /* 0x77 */ VK_END, // End | |
| 202 /* 0x78 */ VK_F2, | |
| 203 /* 0x79 */ VK_NEXT, // Page Down | |
| 204 /* 0x7A */ VK_F1, | |
| 205 /* 0x7B */ VK_LEFT, // Left Arrow | |
| 206 /* 0x7C */ VK_RIGHT, // Right Arrow | |
| 207 /* 0x7D */ VK_DOWN, // Down Arrow | |
| 208 /* 0x7E */ VK_UP, // Up Arrow | |
| 209 /* 0x7F */ 0 // n/a | |
| 210 }; | |
| 211 | |
| 212 if (keyCode >= 0x80) | |
| 213 return 0; | |
| 214 | |
| 215 return windowsKeyCode[keyCode]; | |
| 216 } | |
| 217 | |
| 218 static int windowsKeyCodeForCharCode(unichar charCode) | |
| 219 { | |
| 220 switch (charCode) { | |
| 221 | |
| 222 case 'a': case 'A': return VK_A; | |
| 223 case 'b': case 'B': return VK_B; | |
| 224 case 'c': case 'C': return VK_C; | |
| 225 case 'd': case 'D': return VK_D; | |
| 226 case 'e': case 'E': return VK_E; | |
| 227 case 'f': case 'F': return VK_F; | |
| 228 case 'g': case 'G': return VK_G; | |
| 229 case 'h': case 'H': return VK_H; | |
| 230 case 'i': case 'I': return VK_I; | |
| 231 case 'j': case 'J': return VK_J; | |
| 232 case 'k': case 'K': return VK_K; | |
| 233 case 'l': case 'L': return VK_L; | |
| 234 case 'm': case 'M': return VK_M; | |
| 235 case 'n': case 'N': return VK_N; | |
| 236 case 'o': case 'O': return VK_O; | |
| 237 case 'p': case 'P': return VK_P; | |
| 238 case 'q': case 'Q': return VK_Q; | |
| 239 case 'r': case 'R': return VK_R; | |
| 240 case 's': case 'S': return VK_S; | |
| 241 case 't': case 'T': return VK_T; | |
| 242 case 'u': case 'U': return VK_U; | |
| 243 case 'v': case 'V': return VK_V; | |
| 244 case 'w': case 'W': return VK_W; | |
| 245 case 'x': case 'X': return VK_X; | |
| 246 case 'y': case 'Y': return VK_Y; | |
| 247 case 'z': case 'Z': return VK_Z; | |
| 248 | |
| 249 // AppKit generates Unicode PUA character codes for some function keys;
using these when key code is not known. | |
| 250 case NSPauseFunctionKey: return VK_PAUSE; | |
| 251 case NSSelectFunctionKey: return VK_SELECT; | |
| 252 case NSPrintFunctionKey: return VK_PRINT; | |
| 253 case NSExecuteFunctionKey: return VK_EXECUTE; | |
| 254 case NSPrintScreenFunctionKey: return VK_SNAPSHOT; | |
| 255 case NSInsertFunctionKey: return VK_INSERT; | |
| 256 | |
| 257 case NSF21FunctionKey: return VK_F21; | |
| 258 case NSF22FunctionKey: return VK_F22; | |
| 259 case NSF23FunctionKey: return VK_F23; | |
| 260 case NSF24FunctionKey: return VK_F24; | |
| 261 case NSScrollLockFunctionKey: return VK_SCROLL; | |
| 262 | |
| 263 // This is for U.S. keyboard mapping, and doesn't necessarily make sense
for different keyboard layouts. | |
| 264 // For example, '"' on Windows Russian layout is VK_2, not VK_OEM_7. | |
| 265 case ';': case ':': return VK_OEM_1; | |
| 266 case '=': case '+': return VK_OEM_PLUS; | |
| 267 case ',': case '<': return VK_OEM_COMMA; | |
| 268 case '-': case '_': return VK_OEM_MINUS; | |
| 269 case '.': case '>': return VK_OEM_PERIOD; | |
| 270 case '/': case '?': return VK_OEM_2; | |
| 271 case '`': case '~': return VK_OEM_3; | |
| 272 case '[': case '{': return VK_OEM_4; | |
| 273 case '\\': case '|': return VK_OEM_5; | |
| 274 case ']': case '}': return VK_OEM_6; | |
| 275 case '\'': case '"': return VK_OEM_7; | |
| 276 | |
| 277 } | |
| 278 | |
| 279 return 0; | |
| 280 } | |
| 281 | |
| 282 // Return true if the target modifier key is up. OS X has an "official" flag | |
| 283 // to test whether either left or right versions of a modifier key are held, | |
| 284 // and "unofficial" flags for the left and right versions independently. This | |
| 285 // function verifies that |targetKeyMask| and |otherKeyMask| (which should be | |
| 286 // the left and right versions of a modifier) are consistent with with the | |
| 287 // state of |eitherKeyMask| (which should be the corresponding ""official" | |
| 288 // flag). If they are consistent, it tests |targetKeyMask|; otherwise it tests | |
| 289 // |eitherKeyMask|. | |
| 290 static inline bool isModifierKeyUp( | |
| 291 unsigned int flags, unsigned int targetKeyMask, unsigned int otherKeyMask, | |
| 292 unsigned int eitherKeyMask) | |
| 293 { | |
| 294 bool eitherKeyDown = (flags & eitherKeyMask) != 0; | |
| 295 bool targetKeyDown = (flags & targetKeyMask) != 0; | |
| 296 bool otherKeyDown = (flags & otherKeyMask) != 0; | |
| 297 if (eitherKeyDown != (targetKeyDown || otherKeyDown)) | |
| 298 return !eitherKeyDown; | |
| 299 return !targetKeyDown; | |
| 300 } | |
| 301 | |
| 302 static inline bool isKeyUpEvent(NSEvent* event) | |
| 303 { | |
| 304 if ([event type] != NSFlagsChanged) | |
| 305 return [event type] == NSKeyUp; | |
| 306 | |
| 307 // Unofficial bit-masks for left- and right-hand versions of modifier keys. | |
| 308 // These values were determined empirically. | |
| 309 const unsigned int kLeftControlKeyMask = 1 << 0; | |
| 310 const unsigned int kLeftShiftKeyMask = 1 << 1; | |
| 311 const unsigned int kRightShiftKeyMask = 1 << 2; | |
| 312 const unsigned int kLeftCommandKeyMask = 1 << 3; | |
| 313 const unsigned int kRightCommandKeyMask = 1 << 4; | |
| 314 const unsigned int kLeftAlternateKeyMask = 1 << 5; | |
| 315 const unsigned int kRightAlternateKeyMask = 1 << 6; | |
| 316 const unsigned int kRightControlKeyMask = 1 << 13; | |
| 317 | |
| 318 switch ([event keyCode]) { | |
| 319 case 54: // Right Command | |
| 320 return isModifierKeyUp([event modifierFlags], kRightCommandKeyMask, | |
| 321 kLeftCommandKeyMask, NSCommandKeyMask); | |
| 322 case 55: // Left Command | |
| 323 return isModifierKeyUp([event modifierFlags], kLeftCommandKeyMask, | |
| 324 kRightCommandKeyMask, NSCommandKeyMask); | |
| 325 | |
| 326 case 57: // Capslock | |
| 327 return ([event modifierFlags] & NSAlphaShiftKeyMask) == 0; | |
| 328 | |
| 329 case 56: // Left Shift | |
| 330 return isModifierKeyUp([event modifierFlags], kLeftShiftKeyMask, | |
| 331 kRightShiftKeyMask, NSShiftKeyMask); | |
| 332 case 60: // Right Shift | |
| 333 return isModifierKeyUp([event modifierFlags],kRightShiftKeyMask, | |
| 334 kLeftShiftKeyMask, NSShiftKeyMask); | |
| 335 | |
| 336 case 58: // Left Alt | |
| 337 return isModifierKeyUp([event modifierFlags], kLeftAlternateKeyMask, | |
| 338 kRightAlternateKeyMask, NSAlternateKeyMask); | |
| 339 case 61: // Right Alt | |
| 340 return isModifierKeyUp([event modifierFlags], kRightAlternateKeyMask, | |
| 341 kLeftAlternateKeyMask, NSAlternateKeyMask); | |
| 342 | |
| 343 case 59: // Left Ctrl | |
| 344 return isModifierKeyUp([event modifierFlags], kLeftControlKeyMask, | |
| 345 kRightControlKeyMask, NSControlKeyMask); | |
| 346 case 62: // Right Ctrl | |
| 347 return isModifierKeyUp([event modifierFlags], kRightControlKeyMask, | |
| 348 kLeftControlKeyMask, NSControlKeyMask); | |
| 349 | |
| 350 case 63: // Function | |
| 351 return ([event modifierFlags] & NSFunctionKeyMask) == 0; | |
| 352 } | |
| 353 return false; | |
| 354 } | |
| 355 | |
| 356 static bool isKeypadEvent(NSEvent* event) | |
| 357 { | |
| 358 // Check that this is the type of event that has a keyCode. | |
| 359 switch ([event type]) { | |
| 360 case NSKeyDown: | |
| 361 case NSKeyUp: | |
| 362 case NSFlagsChanged: | |
| 363 break; | |
| 364 default: | |
| 365 return false; | |
| 366 } | |
| 367 | |
| 368 switch ([event keyCode]) { | |
| 369 case 71: // Clear | |
| 370 case 81: // = | |
| 371 case 75: // / | |
| 372 case 67: // * | |
| 373 case 78: // - | |
| 374 case 69: // + | |
| 375 case 76: // Enter | |
| 376 case 65: // . | |
| 377 case 82: // 0 | |
| 378 case 83: // 1 | |
| 379 case 84: // 2 | |
| 380 case 85: // 3 | |
| 381 case 86: // 4 | |
| 382 case 87: // 5 | |
| 383 case 88: // 6 | |
| 384 case 89: // 7 | |
| 385 case 91: // 8 | |
| 386 case 92: // 9 | |
| 387 return true; | |
| 388 } | |
| 389 | |
| 390 return false; | |
| 391 } | |
| 392 | |
| 393 static int windowsKeyCodeForKeyEvent(NSEvent* event) | |
| 394 { | |
| 395 int code = 0; | |
| 396 // There are several kinds of characters for which we produce key code from
char code: | |
| 397 // 1. Roman letters. Windows keyboard layouts affect both virtual key codes
and character codes for these, | |
| 398 // so e.g. 'A' gets the same keyCode on QWERTY, AZERTY or Dvorak layouts. | |
| 399 // 2. Keys for which there is no known Mac virtual key codes, like PrintScre
en. | |
| 400 // 3. Certain punctuation keys. On Windows, these are also remapped dependin
g on current keyboard layout, | |
| 401 // but see comment in windowsKeyCodeForCharCode(). | |
| 402 if (!isKeypadEvent(event) && ([event type] == NSKeyDown || [event type] == N
SKeyUp)) { | |
| 403 // Cmd switches Roman letters for Dvorak-QWERTY layout, so try modified
characters first. | |
| 404 NSString* s = [event characters]; | |
| 405 code = [s length] > 0 ? windowsKeyCodeForCharCode([s characterAtIndex:0]
) : 0; | |
| 406 if (code) | |
| 407 return code; | |
| 408 | |
| 409 // Ctrl+A on an AZERTY keyboard would get VK_Q keyCode if we relied on -
[NSEvent keyCode] below. | |
| 410 s = [event charactersIgnoringModifiers]; | |
| 411 code = [s length] > 0 ? windowsKeyCodeForCharCode([s characterAtIndex:0]
) : 0; | |
| 412 if (code) | |
| 413 return code; | |
| 414 } | |
| 415 | |
| 416 // Map Mac virtual key code directly to Windows one for any keys not handled
above. | |
| 417 // E.g. the key next to Caps Lock has the same Event.keyCode on U.S. keyboar
d ('A') and on Russian keyboard (CYRILLIC LETTER EF). | |
| 418 return windowsKeyCodeForKeyCode([event keyCode]); | |
| 419 } | |
| 420 | |
| 421 static inline NSString* textFromEvent(NSEvent* event) | |
| 422 { | |
| 423 if ([event type] == NSFlagsChanged) | |
| 424 return @""; | |
| 425 return [event characters]; | |
| 426 } | |
| 427 | |
| 428 static inline NSString* unmodifiedTextFromEvent(NSEvent* event) | |
| 429 { | |
| 430 if ([event type] == NSFlagsChanged) | |
| 431 return @""; | |
| 432 return [event charactersIgnoringModifiers]; | |
| 433 } | |
| 434 | |
| 435 static NSString* keyIdentifierForKeyEvent(NSEvent* event) | |
| 436 { | |
| 437 if ([event type] == NSFlagsChanged) { | |
| 438 switch ([event keyCode]) { | |
| 439 case 54: // Right Command | |
| 440 case 55: // Left Command | |
| 441 return @"Meta"; | |
| 442 | |
| 443 case 57: // Capslock | |
| 444 return @"CapsLock"; | |
| 445 | |
| 446 case 56: // Left Shift | |
| 447 case 60: // Right Shift | |
| 448 return @"Shift"; | |
| 449 | |
| 450 case 58: // Left Alt | |
| 451 case 61: // Right Alt | |
| 452 return @"Alt"; | |
| 453 | |
| 454 case 59: // Left Ctrl | |
| 455 case 62: // Right Ctrl | |
| 456 return @"Control"; | |
| 457 | |
| 458 // Begin non-Apple addition/modification -------------------------------------- | |
| 459 case 63: // Function | |
| 460 return @"Function"; | |
| 461 | |
| 462 default: // Unknown, but this may be a strange/new keyboard. | |
| 463 return @"Unidentified"; | |
| 464 // End non-Apple addition/modification ---------------------------------------- | |
| 465 } | |
| 466 } | |
| 467 | |
| 468 NSString* s = [event charactersIgnoringModifiers]; | |
| 469 if ([s length] != 1) | |
| 470 return @"Unidentified"; | |
| 471 | |
| 472 unichar c = [s characterAtIndex:0]; | |
| 473 switch (c) { | |
| 474 // Each identifier listed in the DOM spec is listed here. | |
| 475 // Many are simply commented out since they do not appear on standard Macint
osh keyboards | |
| 476 // or are on a key that doesn't have a corresponding character. | |
| 477 | |
| 478 // "Accept" | |
| 479 // "AllCandidates" | |
| 480 | |
| 481 // "Alt" | |
| 482 case NSMenuFunctionKey: | |
| 483 return @"Alt"; | |
| 484 | |
| 485 // "Apps" | |
| 486 // "BrowserBack" | |
| 487 // "BrowserForward" | |
| 488 // "BrowserHome" | |
| 489 // "BrowserRefresh" | |
| 490 // "BrowserSearch" | |
| 491 // "BrowserStop" | |
| 492 // "CapsLock" | |
| 493 | |
| 494 // "Clear" | |
| 495 case NSClearLineFunctionKey: | |
| 496 return @"Clear"; | |
| 497 | |
| 498 // "CodeInput" | |
| 499 // "Compose" | |
| 500 // "Control" | |
| 501 // "Crsel" | |
| 502 // "Convert" | |
| 503 // "Copy" | |
| 504 // "Cut" | |
| 505 | |
| 506 // "Down" | |
| 507 case NSDownArrowFunctionKey: | |
| 508 return @"Down"; | |
| 509 // "End" | |
| 510 case NSEndFunctionKey: | |
| 511 return @"End"; | |
| 512 // "Enter" | |
| 513 case 0x3: case 0xA: case 0xD: // Macintosh calls the one on the main keyboar
d Return, but Windows calls it Enter, so we'll do the same for the DOM | |
| 514 return @"Enter"; | |
| 515 | |
| 516 // "EraseEof" | |
| 517 | |
| 518 // "Execute" | |
| 519 case NSExecuteFunctionKey: | |
| 520 return @"Execute"; | |
| 521 | |
| 522 // "Exsel" | |
| 523 | |
| 524 // "F1" | |
| 525 case NSF1FunctionKey: | |
| 526 return @"F1"; | |
| 527 // "F2" | |
| 528 case NSF2FunctionKey: | |
| 529 return @"F2"; | |
| 530 // "F3" | |
| 531 case NSF3FunctionKey: | |
| 532 return @"F3"; | |
| 533 // "F4" | |
| 534 case NSF4FunctionKey: | |
| 535 return @"F4"; | |
| 536 // "F5" | |
| 537 case NSF5FunctionKey: | |
| 538 return @"F5"; | |
| 539 // "F6" | |
| 540 case NSF6FunctionKey: | |
| 541 return @"F6"; | |
| 542 // "F7" | |
| 543 case NSF7FunctionKey: | |
| 544 return @"F7"; | |
| 545 // "F8" | |
| 546 case NSF8FunctionKey: | |
| 547 return @"F8"; | |
| 548 // "F9" | |
| 549 case NSF9FunctionKey: | |
| 550 return @"F9"; | |
| 551 // "F10" | |
| 552 case NSF10FunctionKey: | |
| 553 return @"F10"; | |
| 554 // "F11" | |
| 555 case NSF11FunctionKey: | |
| 556 return @"F11"; | |
| 557 // "F12" | |
| 558 case NSF12FunctionKey: | |
| 559 return @"F12"; | |
| 560 // "F13" | |
| 561 case NSF13FunctionKey: | |
| 562 return @"F13"; | |
| 563 // "F14" | |
| 564 case NSF14FunctionKey: | |
| 565 return @"F14"; | |
| 566 // "F15" | |
| 567 case NSF15FunctionKey: | |
| 568 return @"F15"; | |
| 569 // "F16" | |
| 570 case NSF16FunctionKey: | |
| 571 return @"F16"; | |
| 572 // "F17" | |
| 573 case NSF17FunctionKey: | |
| 574 return @"F17"; | |
| 575 // "F18" | |
| 576 case NSF18FunctionKey: | |
| 577 return @"F18"; | |
| 578 // "F19" | |
| 579 case NSF19FunctionKey: | |
| 580 return @"F19"; | |
| 581 // "F20" | |
| 582 case NSF20FunctionKey: | |
| 583 return @"F20"; | |
| 584 // "F21" | |
| 585 case NSF21FunctionKey: | |
| 586 return @"F21"; | |
| 587 // "F22" | |
| 588 case NSF22FunctionKey: | |
| 589 return @"F22"; | |
| 590 // "F23" | |
| 591 case NSF23FunctionKey: | |
| 592 return @"F23"; | |
| 593 // "F24" | |
| 594 case NSF24FunctionKey: | |
| 595 return @"F24"; | |
| 596 | |
| 597 // "FinalMode" | |
| 598 | |
| 599 // "Find" | |
| 600 case NSFindFunctionKey: | |
| 601 return @"Find"; | |
| 602 | |
| 603 // "FullWidth" | |
| 604 // "HalfWidth" | |
| 605 // "HangulMode" | |
| 606 // "HanjaMode" | |
| 607 | |
| 608 // "Help" | |
| 609 case NSHelpFunctionKey: | |
| 610 return @"Help"; | |
| 611 | |
| 612 // "Hiragana" | |
| 613 | |
| 614 // "Home" | |
| 615 case NSHomeFunctionKey: | |
| 616 return @"Home"; | |
| 617 // "Insert" | |
| 618 case NSInsertFunctionKey: | |
| 619 return @"Insert"; | |
| 620 | |
| 621 // "JapaneseHiragana" | |
| 622 // "JapaneseKatakana" | |
| 623 // "JapaneseRomaji" | |
| 624 // "JunjaMode" | |
| 625 // "KanaMode" | |
| 626 // "KanjiMode" | |
| 627 // "Katakana" | |
| 628 // "LaunchApplication1" | |
| 629 // "LaunchApplication2" | |
| 630 // "LaunchMail" | |
| 631 | |
| 632 // "Left" | |
| 633 case NSLeftArrowFunctionKey: | |
| 634 return @"Left"; | |
| 635 | |
| 636 // "Meta" | |
| 637 // "MediaNextTrack" | |
| 638 // "MediaPlayPause" | |
| 639 // "MediaPreviousTrack" | |
| 640 // "MediaStop" | |
| 641 | |
| 642 // "ModeChange" | |
| 643 case NSModeSwitchFunctionKey: | |
| 644 return @"ModeChange"; | |
| 645 | |
| 646 // "Nonconvert" | |
| 647 // "NumLock" | |
| 648 | |
| 649 // "PageDown" | |
| 650 case NSPageDownFunctionKey: | |
| 651 return @"PageDown"; | |
| 652 // "PageUp" | |
| 653 case NSPageUpFunctionKey: | |
| 654 return @"PageUp"; | |
| 655 | |
| 656 // "Paste" | |
| 657 | |
| 658 // "Pause" | |
| 659 case NSPauseFunctionKey: | |
| 660 return @"Pause"; | |
| 661 | |
| 662 // "Play" | |
| 663 // "PreviousCandidate" | |
| 664 | |
| 665 // "PrintScreen" | |
| 666 case NSPrintScreenFunctionKey: | |
| 667 return @"PrintScreen"; | |
| 668 | |
| 669 // "Process" | |
| 670 // "Props" | |
| 671 | |
| 672 // "Right" | |
| 673 case NSRightArrowFunctionKey: | |
| 674 return @"Right"; | |
| 675 | |
| 676 // "RomanCharacters" | |
| 677 | |
| 678 // "Scroll" | |
| 679 case NSScrollLockFunctionKey: | |
| 680 return @"Scroll"; | |
| 681 // "Select" | |
| 682 case NSSelectFunctionKey: | |
| 683 return @"Select"; | |
| 684 | |
| 685 // "SelectMedia" | |
| 686 // "Shift" | |
| 687 | |
| 688 // "Stop" | |
| 689 case NSStopFunctionKey: | |
| 690 return @"Stop"; | |
| 691 // "Up" | |
| 692 case NSUpArrowFunctionKey: | |
| 693 return @"Up"; | |
| 694 // "Undo" | |
| 695 case NSUndoFunctionKey: | |
| 696 return @"Undo"; | |
| 697 | |
| 698 // "VolumeDown" | |
| 699 // "VolumeMute" | |
| 700 // "VolumeUp" | |
| 701 // "Win" | |
| 702 // "Zoom" | |
| 703 | |
| 704 // More function keys, not in the key identifier specification. | |
| 705 case NSF25FunctionKey: | |
| 706 return @"F25"; | |
| 707 case NSF26FunctionKey: | |
| 708 return @"F26"; | |
| 709 case NSF27FunctionKey: | |
| 710 return @"F27"; | |
| 711 case NSF28FunctionKey: | |
| 712 return @"F28"; | |
| 713 case NSF29FunctionKey: | |
| 714 return @"F29"; | |
| 715 case NSF30FunctionKey: | |
| 716 return @"F30"; | |
| 717 case NSF31FunctionKey: | |
| 718 return @"F31"; | |
| 719 case NSF32FunctionKey: | |
| 720 return @"F32"; | |
| 721 case NSF33FunctionKey: | |
| 722 return @"F33"; | |
| 723 case NSF34FunctionKey: | |
| 724 return @"F34"; | |
| 725 case NSF35FunctionKey: | |
| 726 return @"F35"; | |
| 727 | |
| 728 // Turn 0x7F into 0x08, because backspace needs to always be 0x08. | |
| 729 case 0x7F: | |
| 730 return @"U+0008"; | |
| 731 // Standard says that DEL becomes U+007F. | |
| 732 case NSDeleteFunctionKey: | |
| 733 return @"U+007F"; | |
| 734 | |
| 735 // Always use 0x09 for tab instead of AppKit's backtab character. | |
| 736 case NSBackTabCharacter: | |
| 737 return @"U+0009"; | |
| 738 | |
| 739 case NSBeginFunctionKey: | |
| 740 case NSBreakFunctionKey: | |
| 741 case NSClearDisplayFunctionKey: | |
| 742 case NSDeleteCharFunctionKey: | |
| 743 case NSDeleteLineFunctionKey: | |
| 744 case NSInsertCharFunctionKey: | |
| 745 case NSInsertLineFunctionKey: | |
| 746 case NSNextFunctionKey: | |
| 747 case NSPrevFunctionKey: | |
| 748 case NSPrintFunctionKey: | |
| 749 case NSRedoFunctionKey: | |
| 750 case NSResetFunctionKey: | |
| 751 case NSSysReqFunctionKey: | |
| 752 case NSSystemFunctionKey: | |
| 753 case NSUserFunctionKey: | |
| 754 // FIXME: We should use something other than the vendor-area Unicode val
ues for the above keys. | |
| 755 // For now, just fall through to the default. | |
| 756 default: | |
| 757 return [NSString stringWithFormat:@"U+%04X", WTF::toASCIIUpper(c)]; | |
| 758 } | |
| 759 } | |
| 760 | |
| 761 // End Apple code. | |
| 762 // ---------------------------------------------------------------------------- | |
| 763 | |
| 764 static inline int modifiersFromEvent(NSEvent* event) { | |
| 765 int modifiers = 0; | |
| 766 | |
| 767 if ([event modifierFlags] & NSControlKeyMask) | |
| 768 modifiers |= WebInputEvent::ControlKey; | |
| 769 if ([event modifierFlags] & NSShiftKeyMask) | |
| 770 modifiers |= WebInputEvent::ShiftKey; | |
| 771 if ([event modifierFlags] & NSAlternateKeyMask) | |
| 772 modifiers |= WebInputEvent::AltKey; | |
| 773 if ([event modifierFlags] & NSCommandKeyMask) | |
| 774 modifiers |= WebInputEvent::MetaKey; | |
| 775 if ([event modifierFlags] & NSAlphaShiftKeyMask) | |
| 776 modifiers |= WebInputEvent::CapsLockOn; | |
| 777 | |
| 778 // The return value of 1 << 0 corresponds to the left mouse button, | |
| 779 // 1 << 1 corresponds to the right mouse button, | |
| 780 // 1 << n, n >= 2 correspond to other mouse buttons. | |
| 781 NSUInteger pressedButtons = [NSEvent pressedMouseButtons]; | |
| 782 | |
| 783 if (pressedButtons & (1 << 0)) | |
| 784 modifiers |= WebInputEvent::LeftButtonDown; | |
| 785 if (pressedButtons & (1 << 1)) | |
| 786 modifiers |= WebInputEvent::RightButtonDown; | |
| 787 if (pressedButtons & (1 << 2)) | |
| 788 modifiers |= WebInputEvent::MiddleButtonDown; | |
| 789 | |
| 790 return modifiers; | |
| 791 } | |
| 792 | |
| 793 static inline void setWebEventLocationFromEventInView(WebMouseEvent* result, | |
| 794 NSEvent* event, | |
| 795 NSView* view) { | |
| 796 NSPoint windowLocal = [event locationInWindow]; | |
| 797 | |
| 798 NSPoint screenLocal = [[view window] convertBaseToScreen:windowLocal]; | |
| 799 result->globalX = screenLocal.x; | |
| 800 // Flip y. | |
| 801 NSScreen* primaryScreen = ([[NSScreen screens] count] > 0) ? | |
| 802 [[NSScreen screens] objectAtIndex:0] : nil; | |
| 803 if (primaryScreen) | |
| 804 result->globalY = [primaryScreen frame].size.height - screenLocal.y; | |
| 805 else | |
| 806 result->globalY = screenLocal.y; | |
| 807 | |
| 808 NSPoint contentLocal = [view convertPoint:windowLocal fromView:nil]; | |
| 809 result->x = contentLocal.x; | |
| 810 result->y = [view frame].size.height - contentLocal.y; // Flip y. | |
| 811 | |
| 812 result->windowX = result->x; | |
| 813 result->windowY = result->y; | |
| 814 | |
| 815 result->movementX = [event deltaX]; | |
| 816 result->movementY = [event deltaY]; | |
| 817 } | |
| 818 | |
| 819 bool WebInputEventFactory::isSystemKeyEvent(const WebKeyboardEvent& event) | |
| 820 { | |
| 821 // Windows and Linux set |isSystemKey| if alt is down. Blink looks at this | |
| 822 // flag to decide if it should handle a key or not. E.g. alt-left/right | |
| 823 // shouldn't be used by Blink to scroll the current page, because we want | |
| 824 // to get that key back for it to do history navigation. Hence, the | |
| 825 // corresponding situation on OS X is to set this for cmd key presses. | |
| 826 // cmd-b and and cmd-i are system wide key bindings that OS X doesn't | |
| 827 // handle for us, so the editor handles them. | |
| 828 return event.modifiers & WebInputEvent::MetaKey | |
| 829 && event.windowsKeyCode != VK_B | |
| 830 && event.windowsKeyCode != VK_I; | |
| 831 } | |
| 832 | |
| 833 WebKeyboardEvent WebInputEventFactory::keyboardEvent(NSEvent* event) | |
| 834 { | |
| 835 WebKeyboardEvent result; | |
| 836 | |
| 837 result.type = | |
| 838 isKeyUpEvent(event) ? WebInputEvent::KeyUp : WebInputEvent::RawKeyDown; | |
| 839 | |
| 840 result.modifiers = modifiersFromEvent(event); | |
| 841 | |
| 842 if (isKeypadEvent(event)) | |
| 843 result.modifiers |= WebInputEvent::IsKeyPad; | |
| 844 | |
| 845 if (([event type] != NSFlagsChanged) && [event isARepeat]) | |
| 846 result.modifiers |= WebInputEvent::IsAutoRepeat; | |
| 847 | |
| 848 int windowsKeyCode = windowsKeyCodeForKeyEvent(event); | |
| 849 result.windowsKeyCode = WebKeyboardEvent::windowsKeyCodeWithoutLocation(wind
owsKeyCode); | |
| 850 result.modifiers |= WebKeyboardEvent::locationModifiersFromWindowsKeyCode(wi
ndowsKeyCode); | |
| 851 result.nativeKeyCode = [event keyCode]; | |
| 852 NSString* textStr = textFromEvent(event); | |
| 853 NSString* unmodifiedStr = unmodifiedTextFromEvent(event); | |
| 854 NSString* identifierStr = keyIdentifierForKeyEvent(event); | |
| 855 | |
| 856 // Begin Apple code, copied from KeyEventMac.mm | |
| 857 | |
| 858 // Always use 13 for Enter/Return -- we don't want to use AppKit's | |
| 859 // different character for Enter. | |
| 860 if (result.windowsKeyCode == '\r') { | |
| 861 textStr = @"\r"; | |
| 862 unmodifiedStr = @"\r"; | |
| 863 } | |
| 864 | |
| 865 // The adjustments below are only needed in backward compatibility mode, | |
| 866 // but we cannot tell what mode we are in from here. | |
| 867 | |
| 868 // Turn 0x7F into 8, because backspace needs to always be 8. | |
| 869 if ([textStr isEqualToString:@"\x7F"]) | |
| 870 textStr = @"\x8"; | |
| 871 if ([unmodifiedStr isEqualToString:@"\x7F"]) | |
| 872 unmodifiedStr = @"\x8"; | |
| 873 // Always use 9 for tab -- we don't want to use AppKit's different character | |
| 874 // for shift-tab. | |
| 875 if (result.windowsKeyCode == 9) { | |
| 876 textStr = @"\x9"; | |
| 877 unmodifiedStr = @"\x9"; | |
| 878 } | |
| 879 | |
| 880 // End Apple code. | |
| 881 | |
| 882 if ([textStr length] < WebKeyboardEvent::textLengthCap && | |
| 883 [unmodifiedStr length] < WebKeyboardEvent::textLengthCap) { | |
| 884 [textStr getCharacters:&result.text[0]]; | |
| 885 [unmodifiedStr getCharacters:&result.unmodifiedText[0]]; | |
| 886 } else | |
| 887 ASSERT_NOT_REACHED(); | |
| 888 | |
| 889 [identifierStr getCString:&result.keyIdentifier[0] | |
| 890 maxLength:sizeof(result.keyIdentifier) | |
| 891 encoding:NSASCIIStringEncoding]; | |
| 892 | |
| 893 result.timeStampSeconds = [event timestamp]; | |
| 894 result.isSystemKey = isSystemKeyEvent(result); | |
| 895 | |
| 896 return result; | |
| 897 } | |
| 898 | |
| 899 // WebMouseEvent -------------------------------------------------------------- | |
| 900 | |
| 901 WebMouseEvent WebInputEventFactory::mouseEvent(NSEvent* event, NSView* view) | |
| 902 { | |
| 903 WebMouseEvent result; | |
| 904 | |
| 905 result.clickCount = 0; | |
| 906 | |
| 907 switch ([event type]) { | |
| 908 case NSMouseExited: | |
| 909 result.type = WebInputEvent::MouseLeave; | |
| 910 result.button = WebMouseEvent::ButtonNone; | |
| 911 break; | |
| 912 case NSLeftMouseDown: | |
| 913 result.type = WebInputEvent::MouseDown; | |
| 914 result.clickCount = [event clickCount]; | |
| 915 result.button = WebMouseEvent::ButtonLeft; | |
| 916 break; | |
| 917 case NSOtherMouseDown: | |
| 918 result.type = WebInputEvent::MouseDown; | |
| 919 result.clickCount = [event clickCount]; | |
| 920 result.button = WebMouseEvent::ButtonMiddle; | |
| 921 break; | |
| 922 case NSRightMouseDown: | |
| 923 result.type = WebInputEvent::MouseDown; | |
| 924 result.clickCount = [event clickCount]; | |
| 925 result.button = WebMouseEvent::ButtonRight; | |
| 926 break; | |
| 927 case NSLeftMouseUp: | |
| 928 result.type = WebInputEvent::MouseUp; | |
| 929 result.clickCount = [event clickCount]; | |
| 930 result.button = WebMouseEvent::ButtonLeft; | |
| 931 break; | |
| 932 case NSOtherMouseUp: | |
| 933 result.type = WebInputEvent::MouseUp; | |
| 934 result.clickCount = [event clickCount]; | |
| 935 result.button = WebMouseEvent::ButtonMiddle; | |
| 936 break; | |
| 937 case NSRightMouseUp: | |
| 938 result.type = WebInputEvent::MouseUp; | |
| 939 result.clickCount = [event clickCount]; | |
| 940 result.button = WebMouseEvent::ButtonRight; | |
| 941 break; | |
| 942 case NSMouseMoved: | |
| 943 case NSMouseEntered: | |
| 944 result.type = WebInputEvent::MouseMove; | |
| 945 break; | |
| 946 case NSLeftMouseDragged: | |
| 947 result.type = WebInputEvent::MouseMove; | |
| 948 result.button = WebMouseEvent::ButtonLeft; | |
| 949 break; | |
| 950 case NSOtherMouseDragged: | |
| 951 result.type = WebInputEvent::MouseMove; | |
| 952 result.button = WebMouseEvent::ButtonMiddle; | |
| 953 break; | |
| 954 case NSRightMouseDragged: | |
| 955 result.type = WebInputEvent::MouseMove; | |
| 956 result.button = WebMouseEvent::ButtonRight; | |
| 957 break; | |
| 958 default: | |
| 959 ASSERT_NOT_REACHED(); | |
| 960 } | |
| 961 | |
| 962 setWebEventLocationFromEventInView(&result, event, view); | |
| 963 | |
| 964 result.modifiers = modifiersFromEvent(event); | |
| 965 | |
| 966 result.timeStampSeconds = [event timestamp]; | |
| 967 | |
| 968 return result; | |
| 969 } | |
| 970 | |
| 971 // WebMouseWheelEvent --------------------------------------------------------- | |
| 972 | |
| 973 static WebMouseWheelEvent::Phase phaseForNSEventPhase(NSEventPhase eventPhase) | |
| 974 { | |
| 975 uint32_t phase = WebMouseWheelEvent::PhaseNone; | |
| 976 if (eventPhase & NSEventPhaseBegan) | |
| 977 phase |= WebMouseWheelEvent::PhaseBegan; | |
| 978 if (eventPhase & NSEventPhaseStationary) | |
| 979 phase |= WebMouseWheelEvent::PhaseStationary; | |
| 980 if (eventPhase & NSEventPhaseChanged) | |
| 981 phase |= WebMouseWheelEvent::PhaseChanged; | |
| 982 if (eventPhase & NSEventPhaseEnded) | |
| 983 phase |= WebMouseWheelEvent::PhaseEnded; | |
| 984 if (eventPhase & NSEventPhaseCancelled) | |
| 985 phase |= WebMouseWheelEvent::PhaseCancelled; | |
| 986 if (eventPhase & NSEventPhaseMayBegin) | |
| 987 phase |= WebMouseWheelEvent::PhaseMayBegin; | |
| 988 return static_cast<WebMouseWheelEvent::Phase>(phase); | |
| 989 } | |
| 990 | |
| 991 static WebMouseWheelEvent::Phase phaseForEvent(NSEvent *event) | |
| 992 { | |
| 993 if (![event respondsToSelector:@selector(phase)]) | |
| 994 return WebMouseWheelEvent::PhaseNone; | |
| 995 | |
| 996 NSEventPhase eventPhase = [event phase]; | |
| 997 return phaseForNSEventPhase(eventPhase); | |
| 998 } | |
| 999 | |
| 1000 static WebMouseWheelEvent::Phase momentumPhaseForEvent(NSEvent *event) | |
| 1001 { | |
| 1002 if (![event respondsToSelector:@selector(momentumPhase)]) | |
| 1003 return WebMouseWheelEvent::PhaseNone; | |
| 1004 | |
| 1005 NSEventPhase eventMomentumPhase = [event momentumPhase]; | |
| 1006 return phaseForNSEventPhase(eventMomentumPhase); | |
| 1007 } | |
| 1008 | |
| 1009 WebMouseWheelEvent WebInputEventFactory::mouseWheelEvent(NSEvent* event, NSView*
view, bool canRubberbandLeft, bool canRubberbandRight) | |
| 1010 { | |
| 1011 WebMouseWheelEvent result; | |
| 1012 | |
| 1013 result.type = WebInputEvent::MouseWheel; | |
| 1014 result.button = WebMouseEvent::ButtonNone; | |
| 1015 | |
| 1016 result.modifiers = modifiersFromEvent(event); | |
| 1017 | |
| 1018 setWebEventLocationFromEventInView(&result, event, view); | |
| 1019 | |
| 1020 result.canRubberbandLeft = canRubberbandLeft; | |
| 1021 result.canRubberbandRight = canRubberbandRight; | |
| 1022 | |
| 1023 // Of Mice and Men | |
| 1024 // --------------- | |
| 1025 // | |
| 1026 // There are three types of scroll data available on a scroll wheel CGEvent. | |
| 1027 // Apple's documentation ([1]) is rather vague in their differences, and not | |
| 1028 // terribly helpful in deciding which to use. This is what's really going on
. | |
| 1029 // | |
| 1030 // First, these events behave very differently depending on whether a standa
rd | |
| 1031 // wheel mouse is used (one that scrolls in discrete units) or a | |
| 1032 // trackpad/Mighty Mouse is used (which both provide continuous scrolling). | |
| 1033 // You must check to see which was used for the event by testing the | |
| 1034 // kCGScrollWheelEventIsContinuous field. | |
| 1035 // | |
| 1036 // Second, these events refer to "axes". Axis 1 is the y-axis, and axis 2 is | |
| 1037 // the x-axis. | |
| 1038 // | |
| 1039 // Third, there is a concept of mouse acceleration. Scrolling the same amoun
t | |
| 1040 // of physical distance will give you different results logically depending
on | |
| 1041 // whether you scrolled a little at a time or in one continuous motion. Some | |
| 1042 // fields account for this while others do not. | |
| 1043 // | |
| 1044 // Fourth, for trackpads there is a concept of chunkiness. When scrolling | |
| 1045 // continuously, events can be delivered in chunks. That is to say, lots of | |
| 1046 // scroll events with delta 0 will be delivered, and every so often an event | |
| 1047 // with a non-zero delta will be delivered, containing the accumulated delta
s | |
| 1048 // from all the intermediate moves. [2] | |
| 1049 // | |
| 1050 // For notchy wheel mice (kCGScrollWheelEventIsContinuous == 0) | |
| 1051 // ------------------------------------------------------------ | |
| 1052 // | |
| 1053 // kCGScrollWheelEventDeltaAxis* | |
| 1054 // This is the rawest of raw events. For each mouse notch you get a value
of | |
| 1055 // +1/-1. This does not take acceleration into account and thus is less | |
| 1056 // useful for building UIs. | |
| 1057 // | |
| 1058 // kCGScrollWheelEventPointDeltaAxis* | |
| 1059 // This is smarter. In general, for each mouse notch you get a value of | |
| 1060 // +1/-1, but this _does_ take acceleration into account, so you will get | |
| 1061 // larger values on longer scrolls. This field would be ideal for building | |
| 1062 // UIs except for one nasty bug: when the shift key is pressed, this set o
f | |
| 1063 // fields fails to move the value into the axis2 field (the other two type
s | |
| 1064 // of data do). This wouldn't be so bad except for the fact that while the | |
| 1065 // number of axes is used in the creation of a CGScrollWheelEvent, there i
s | |
| 1066 // no way to get that information out of the event once created. | |
| 1067 // | |
| 1068 // kCGScrollWheelEventFixedPtDeltaAxis* | |
| 1069 // This is a fixed value, and for each mouse notch you get a value of | |
| 1070 // +0.1/-0.1 (but, like above, scaled appropriately for acceleration). Thi
s | |
| 1071 // value takes acceleration into account, and in fact is identical to the | |
| 1072 // results you get from -[NSEvent delta*]. (That is, if you linked on Tige
r | |
| 1073 // or greater; see [2] for details.) | |
| 1074 // | |
| 1075 // A note about continuous devices | |
| 1076 // ------------------------------- | |
| 1077 // | |
| 1078 // There are two devices that provide continuous scrolling events (trackpads | |
| 1079 // and Mighty Mouses) and they behave rather differently. The Mighty Mouse | |
| 1080 // behaves a lot like a regular mouse. There is no chunking, and the | |
| 1081 // FixedPtDelta values are the PointDelta values multiplied by 0.1. With the | |
| 1082 // trackpad, though, there is chunking. While the FixedPtDelta values are | |
| 1083 // reasonable (they occur about every fifth event but have values five times | |
| 1084 // larger than usual) the Delta values are unreasonable. They don't appear t
o | |
| 1085 // accumulate properly. | |
| 1086 // | |
| 1087 // For continuous devices (kCGScrollWheelEventIsContinuous != 0) | |
| 1088 // ------------------------------------------------------------- | |
| 1089 // | |
| 1090 // kCGScrollWheelEventDeltaAxis* | |
| 1091 // This provides values with no acceleration. With a trackpad, these value
s | |
| 1092 // are chunked but each non-zero value does not appear to be cumulative. | |
| 1093 // This seems to be a bug. | |
| 1094 // | |
| 1095 // kCGScrollWheelEventPointDeltaAxis* | |
| 1096 // This provides values with acceleration. With a trackpad, these values a
re | |
| 1097 // not chunked and are highly accurate. | |
| 1098 // | |
| 1099 // kCGScrollWheelEventFixedPtDeltaAxis* | |
| 1100 // This provides values with acceleration. With a trackpad, these values a
re | |
| 1101 // chunked but unlike Delta events are properly cumulative. | |
| 1102 // | |
| 1103 // Summary | |
| 1104 // ------- | |
| 1105 // | |
| 1106 // In general the best approach to take is: determine if the event is | |
| 1107 // continuous. If it is not, then use the FixedPtDelta events (or just stick | |
| 1108 // with Cocoa events). They provide both acceleration and proper horizontal | |
| 1109 // scrolling. If the event is continuous, then doing pixel scrolling with th
e | |
| 1110 // PointDelta is the way to go. In general, avoid the Delta events. They're | |
| 1111 // the oldest (dating back to 10.4, before CGEvents were public) but they la
ck | |
| 1112 // acceleration and precision, making them useful only in specific edge case
s. | |
| 1113 // | |
| 1114 // References | |
| 1115 // ---------- | |
| 1116 // | |
| 1117 // [1] <http://developer.apple.com/documentation/Carbon/Reference/QuartzEven
tServicesRef/Reference/reference.html> | |
| 1118 // [2] <http://developer.apple.com/releasenotes/Cocoa/AppKitOlderNotes.html> | |
| 1119 // Scroll to the section headed "NSScrollWheel events". | |
| 1120 // | |
| 1121 // P.S. The "smooth scrolling" option in the system preferences is utterly | |
| 1122 // unrelated to any of this. | |
| 1123 | |
| 1124 CGEventRef cgEvent = [event CGEvent]; | |
| 1125 ASSERT(cgEvent); | |
| 1126 | |
| 1127 // Wheel ticks are supposed to be raw, unaccelerated values, one per physica
l | |
| 1128 // mouse wheel notch. The delta event is perfect for this (being a good | |
| 1129 // "specific edge case" as mentioned above). Trackpads, unfortunately, do | |
| 1130 // event chunking, and sending mousewheel events with 0 ticks causes some | |
| 1131 // websites to malfunction. Therefore, for all continuous input devices we u
se | |
| 1132 // the point delta data instead, since we cannot distinguish trackpad data | |
| 1133 // from data from any other continuous device. | |
| 1134 | |
| 1135 // Conversion between wheel delta amounts and number of pixels to scroll. | |
| 1136 static const double scrollbarPixelsPerCocoaTick = 40.0; | |
| 1137 | |
| 1138 if (CGEventGetIntegerValueField(cgEvent, kCGScrollWheelEventIsContinuous)) { | |
| 1139 result.deltaX = CGEventGetIntegerValueField(cgEvent, kCGScrollWheelEvent
PointDeltaAxis2); | |
| 1140 result.deltaY = CGEventGetIntegerValueField(cgEvent, kCGScrollWheelEvent
PointDeltaAxis1); | |
| 1141 result.wheelTicksX = result.deltaX / scrollbarPixelsPerCocoaTick; | |
| 1142 result.wheelTicksY = result.deltaY / scrollbarPixelsPerCocoaTick; | |
| 1143 result.hasPreciseScrollingDeltas = true; | |
| 1144 } else { | |
| 1145 result.deltaX = [event deltaX] * scrollbarPixelsPerCocoaTick; | |
| 1146 result.deltaY = [event deltaY] * scrollbarPixelsPerCocoaTick; | |
| 1147 result.wheelTicksY = CGEventGetIntegerValueField(cgEvent, kCGScrollWheel
EventDeltaAxis1); | |
| 1148 result.wheelTicksX = CGEventGetIntegerValueField(cgEvent, kCGScrollWheel
EventDeltaAxis2); | |
| 1149 } | |
| 1150 | |
| 1151 result.timeStampSeconds = [event timestamp]; | |
| 1152 | |
| 1153 result.phase = phaseForEvent(event); | |
| 1154 result.momentumPhase = momentumPhaseForEvent(event); | |
| 1155 | |
| 1156 return result; | |
| 1157 } | |
| 1158 | |
| 1159 WebGestureEvent WebInputEventFactory::gestureEvent(NSEvent *event, NSView *view) | |
| 1160 { | |
| 1161 WebGestureEvent result; | |
| 1162 | |
| 1163 // Use a temporary WebMouseEvent to get the location. | |
| 1164 WebMouseEvent temp; | |
| 1165 | |
| 1166 setWebEventLocationFromEventInView(&temp, event, view); | |
| 1167 result.x = temp.x; | |
| 1168 result.y = temp.y; | |
| 1169 result.globalX = temp.globalX; | |
| 1170 result.globalY = temp.globalY; | |
| 1171 | |
| 1172 result.modifiers = modifiersFromEvent(event); | |
| 1173 result.timeStampSeconds = [event timestamp]; | |
| 1174 | |
| 1175 result.sourceDevice = WebGestureDeviceTouchpad; | |
| 1176 switch ([event type]) { | |
| 1177 case NSEventTypeMagnify: | |
| 1178 result.type = WebInputEvent::GesturePinchUpdate; | |
| 1179 result.data.pinchUpdate.scale = [event magnification] + 1.0; | |
| 1180 break; | |
| 1181 case NSEventTypeSmartMagnify: | |
| 1182 // Map the Cocoa "double-tap with two fingers" zoom gesture to regular | |
| 1183 // GestureDoubleTap, because the effect is similar to single-finger | |
| 1184 // double-tap zoom on mobile platforms. Note that tapCount is set to 1 | |
| 1185 // because the gesture type already encodes that information. | |
| 1186 result.type = WebInputEvent::GestureDoubleTap; | |
| 1187 result.data.tap.tapCount = 1; | |
| 1188 break; | |
| 1189 case NSEventTypeBeginGesture: | |
| 1190 case NSEventTypeEndGesture: | |
| 1191 // The specific type of a gesture is not defined when the gesture begin | |
| 1192 // and end NSEvents come in. Leave them undefined. The caller will need | |
| 1193 // to specify them when the gesture is differentiated. | |
| 1194 result.type = WebInputEvent::Undefined; | |
| 1195 break; | |
| 1196 default: | |
| 1197 ASSERT_NOT_REACHED(); | |
| 1198 result.type = WebInputEvent::Undefined; | |
| 1199 } | |
| 1200 | |
| 1201 return result; | |
| 1202 } | |
| 1203 | |
| 1204 } // namespace blink | |
| OLD | NEW |