/src/3rdparty/webkit/Source/WebKit2/Shared/mac/WebEventFactory.mm

https://bitbucket.org/ultra_iter/qt-vtl · Objective C++ · 1149 lines · 724 code · 102 blank · 323 comment · 41 complexity · c8353b5bf1fc51179ba568604a19d5c0 MD5 · raw file

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