KeyEventCocoa.mm 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585
  1. /*
  2. * Copyright (C) 2004, 2006, 2007, 2008, 2009, 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 COMPUTER, INC. ``AS IS'' AND ANY
  14. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  15. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  16. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
  17. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  18. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  19. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  20. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  21. * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  22. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  23. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. #import "config.h"
  26. #import "KeyEventCocoa.h"
  27. #import "Logging.h"
  28. #import "WindowsKeyboardCodes.h"
  29. #import <wtf/ASCIICType.h>
  30. #import <wtf/text/WTFString.h>
  31. #if PLATFORM(IOS)
  32. #import "KeyEventCodesIOS.h"
  33. #endif
  34. using namespace WTF;
  35. namespace WebCore {
  36. String keyIdentifierForCharCode(unichar charCode)
  37. {
  38. switch (charCode) {
  39. // Each identifier listed in the DOM spec is listed here.
  40. // Many are simply commented out since they do not appear on standard Macintosh keyboards
  41. // or are on a key that doesn't have a corresponding character.
  42. // "Accept"
  43. // "AllCandidates"
  44. // "Alt"
  45. case NSMenuFunctionKey:
  46. return "Alt";
  47. // "Apps"
  48. // "BrowserBack"
  49. // "BrowserForward"
  50. // "BrowserHome"
  51. // "BrowserRefresh"
  52. // "BrowserSearch"
  53. // "BrowserStop"
  54. // "CapsLock"
  55. // "Clear"
  56. case NSClearLineFunctionKey:
  57. return "Clear";
  58. // "CodeInput"
  59. // "Compose"
  60. // "Control"
  61. // "Crsel"
  62. // "Convert"
  63. // "Copy"
  64. // "Cut"
  65. // "Down"
  66. case NSDownArrowFunctionKey:
  67. return "Down";
  68. // "End"
  69. case NSEndFunctionKey:
  70. return "End";
  71. // "Enter"
  72. 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
  73. return "Enter";
  74. // "EraseEof"
  75. // "Execute"
  76. case NSExecuteFunctionKey:
  77. return "Execute";
  78. // "Exsel"
  79. // "F1"
  80. case NSF1FunctionKey:
  81. return "F1";
  82. // "F2"
  83. case NSF2FunctionKey:
  84. return "F2";
  85. // "F3"
  86. case NSF3FunctionKey:
  87. return "F3";
  88. // "F4"
  89. case NSF4FunctionKey:
  90. return "F4";
  91. // "F5"
  92. case NSF5FunctionKey:
  93. return "F5";
  94. // "F6"
  95. case NSF6FunctionKey:
  96. return "F6";
  97. // "F7"
  98. case NSF7FunctionKey:
  99. return "F7";
  100. // "F8"
  101. case NSF8FunctionKey:
  102. return "F8";
  103. // "F9"
  104. case NSF9FunctionKey:
  105. return "F9";
  106. // "F10"
  107. case NSF10FunctionKey:
  108. return "F10";
  109. // "F11"
  110. case NSF11FunctionKey:
  111. return "F11";
  112. // "F12"
  113. case NSF12FunctionKey:
  114. return "F12";
  115. // "F13"
  116. case NSF13FunctionKey:
  117. return "F13";
  118. // "F14"
  119. case NSF14FunctionKey:
  120. return "F14";
  121. // "F15"
  122. case NSF15FunctionKey:
  123. return "F15";
  124. // "F16"
  125. case NSF16FunctionKey:
  126. return "F16";
  127. // "F17"
  128. case NSF17FunctionKey:
  129. return "F17";
  130. // "F18"
  131. case NSF18FunctionKey:
  132. return "F18";
  133. // "F19"
  134. case NSF19FunctionKey:
  135. return "F19";
  136. // "F20"
  137. case NSF20FunctionKey:
  138. return "F20";
  139. // "F21"
  140. case NSF21FunctionKey:
  141. return "F21";
  142. // "F22"
  143. case NSF22FunctionKey:
  144. return "F22";
  145. // "F23"
  146. case NSF23FunctionKey:
  147. return "F23";
  148. // "F24"
  149. case NSF24FunctionKey:
  150. return "F24";
  151. // "FinalMode"
  152. // "Find"
  153. case NSFindFunctionKey:
  154. return "Find";
  155. // "FullWidth"
  156. // "HalfWidth"
  157. // "HangulMode"
  158. // "HanjaMode"
  159. // "Help"
  160. case NSHelpFunctionKey:
  161. return "Help";
  162. // "Hiragana"
  163. // "Home"
  164. case NSHomeFunctionKey:
  165. return "Home";
  166. // "Insert"
  167. case NSInsertFunctionKey:
  168. return "Insert";
  169. // "JapaneseHiragana"
  170. // "JapaneseKatakana"
  171. // "JapaneseRomaji"
  172. // "JunjaMode"
  173. // "KanaMode"
  174. // "KanjiMode"
  175. // "Katakana"
  176. // "LaunchApplication1"
  177. // "LaunchApplication2"
  178. // "LaunchMail"
  179. // "Left"
  180. case NSLeftArrowFunctionKey:
  181. return "Left";
  182. // "Meta"
  183. // "MediaNextTrack"
  184. // "MediaPlayPause"
  185. // "MediaPreviousTrack"
  186. // "MediaStop"
  187. // "ModeChange"
  188. case NSModeSwitchFunctionKey:
  189. return "ModeChange";
  190. // "Nonconvert"
  191. // "NumLock"
  192. // "PageDown"
  193. case NSPageDownFunctionKey:
  194. return "PageDown";
  195. // "PageUp"
  196. case NSPageUpFunctionKey:
  197. return "PageUp";
  198. // "Paste"
  199. // "Pause"
  200. case NSPauseFunctionKey:
  201. return "Pause";
  202. // "Play"
  203. // "PreviousCandidate"
  204. // "PrintScreen"
  205. case NSPrintScreenFunctionKey:
  206. return "PrintScreen";
  207. // "Process"
  208. // "Props"
  209. // "Right"
  210. case NSRightArrowFunctionKey:
  211. return "Right";
  212. // "RomanCharacters"
  213. // "Scroll"
  214. case NSScrollLockFunctionKey:
  215. return "Scroll";
  216. // "Select"
  217. case NSSelectFunctionKey:
  218. return "Select";
  219. // "SelectMedia"
  220. // "Shift"
  221. // "Stop"
  222. case NSStopFunctionKey:
  223. return "Stop";
  224. // "Up"
  225. case NSUpArrowFunctionKey:
  226. return "Up";
  227. // "Undo"
  228. case NSUndoFunctionKey:
  229. return "Undo";
  230. // "VolumeDown"
  231. // "VolumeMute"
  232. // "VolumeUp"
  233. // "Win"
  234. // "Zoom"
  235. // More function keys, not in the key identifier specification.
  236. case NSF25FunctionKey:
  237. return "F25";
  238. case NSF26FunctionKey:
  239. return "F26";
  240. case NSF27FunctionKey:
  241. return "F27";
  242. case NSF28FunctionKey:
  243. return "F28";
  244. case NSF29FunctionKey:
  245. return "F29";
  246. case NSF30FunctionKey:
  247. return "F30";
  248. case NSF31FunctionKey:
  249. return "F31";
  250. case NSF32FunctionKey:
  251. return "F32";
  252. case NSF33FunctionKey:
  253. return "F33";
  254. case NSF34FunctionKey:
  255. return "F34";
  256. case NSF35FunctionKey:
  257. return "F35";
  258. // Turn 0x7F into 0x08, because backspace needs to always be 0x08.
  259. case 0x7F:
  260. return "U+0008";
  261. // Standard says that DEL becomes U+007F.
  262. case NSDeleteFunctionKey:
  263. return "U+007F";
  264. // Always use 0x09 for tab instead of AppKit's backtab character.
  265. case NSBackTabCharacter:
  266. return "U+0009";
  267. case NSBeginFunctionKey:
  268. case NSBreakFunctionKey:
  269. case NSClearDisplayFunctionKey:
  270. case NSDeleteCharFunctionKey:
  271. case NSDeleteLineFunctionKey:
  272. case NSInsertCharFunctionKey:
  273. case NSInsertLineFunctionKey:
  274. case NSNextFunctionKey:
  275. case NSPrevFunctionKey:
  276. case NSPrintFunctionKey:
  277. case NSRedoFunctionKey:
  278. case NSResetFunctionKey:
  279. case NSSysReqFunctionKey:
  280. case NSSystemFunctionKey:
  281. case NSUserFunctionKey:
  282. // FIXME: We should use something other than the vendor-area Unicode values for the above keys.
  283. // For now, just fall through to the default.
  284. default:
  285. return String::format("U+%04X", toASCIIUpper(charCode));
  286. }
  287. }
  288. int windowsKeyCodeForKeyCode(uint16_t keyCode)
  289. {
  290. static const int windowsKeyCode[] = {
  291. /* 0 */ VK_A,
  292. /* 1 */ VK_S,
  293. /* 2 */ VK_D,
  294. /* 3 */ VK_F,
  295. /* 4 */ VK_H,
  296. /* 5 */ VK_G,
  297. /* 6 */ VK_Z,
  298. /* 7 */ VK_X,
  299. /* 8 */ VK_C,
  300. /* 9 */ VK_V,
  301. /* 0x0A */ VK_OEM_3, // "Section" - key to the left from 1 (ISO Keyboard Only)
  302. /* 0x0B */ VK_B,
  303. /* 0x0C */ VK_Q,
  304. /* 0x0D */ VK_W,
  305. /* 0x0E */ VK_E,
  306. /* 0x0F */ VK_R,
  307. /* 0x10 */ VK_Y,
  308. /* 0x11 */ VK_T,
  309. /* 0x12 */ VK_1,
  310. /* 0x13 */ VK_2,
  311. /* 0x14 */ VK_3,
  312. /* 0x15 */ VK_4,
  313. /* 0x16 */ VK_6,
  314. /* 0x17 */ VK_5,
  315. /* 0x18 */ VK_OEM_PLUS, // =+
  316. /* 0x19 */ VK_9,
  317. /* 0x1A */ VK_7,
  318. /* 0x1B */ VK_OEM_MINUS, // -_
  319. /* 0x1C */ VK_8,
  320. /* 0x1D */ VK_0,
  321. /* 0x1E */ VK_OEM_6, // ]}
  322. /* 0x1F */ VK_O,
  323. /* 0x20 */ VK_U,
  324. /* 0x21 */ VK_OEM_4, // {[
  325. /* 0x22 */ VK_I,
  326. /* 0x23 */ VK_P,
  327. /* 0x24 */ VK_RETURN, // Return
  328. /* 0x25 */ VK_L,
  329. /* 0x26 */ VK_J,
  330. /* 0x27 */ VK_OEM_7, // '"
  331. /* 0x28 */ VK_K,
  332. /* 0x29 */ VK_OEM_1, // ;:
  333. /* 0x2A */ VK_OEM_5, // \|
  334. /* 0x2B */ VK_OEM_COMMA, // ,<
  335. /* 0x2C */ VK_OEM_2, // /?
  336. /* 0x2D */ VK_N,
  337. /* 0x2E */ VK_M,
  338. /* 0x2F */ VK_OEM_PERIOD, // .>
  339. /* 0x30 */ VK_TAB,
  340. /* 0x31 */ VK_SPACE,
  341. /* 0x32 */ VK_OEM_3, // `~
  342. /* 0x33 */ VK_BACK, // Backspace
  343. /* 0x34 */ 0, // n/a
  344. /* 0x35 */ VK_ESCAPE,
  345. /* 0x36 */ VK_APPS, // Right Command
  346. /* 0x37 */ VK_LWIN, // Left Command
  347. /* 0x38 */ VK_LSHIFT, // Left Shift
  348. /* 0x39 */ VK_CAPITAL, // Caps Lock
  349. /* 0x3A */ VK_LMENU, // Left Option
  350. /* 0x3B */ VK_LCONTROL, // Left Ctrl
  351. /* 0x3C */ VK_RSHIFT, // Right Shift
  352. /* 0x3D */ VK_RMENU, // Right Option
  353. /* 0x3E */ VK_RCONTROL, // Right Ctrl
  354. /* 0x3F */ 0, // fn
  355. /* 0x40 */ VK_F17,
  356. /* 0x41 */ VK_DECIMAL, // Num Pad .
  357. /* 0x42 */ 0, // n/a
  358. /* 0x43 */ VK_MULTIPLY, // Num Pad *
  359. /* 0x44 */ 0, // n/a
  360. /* 0x45 */ VK_ADD, // Num Pad +
  361. /* 0x46 */ 0, // n/a
  362. /* 0x47 */ VK_CLEAR, // Num Pad Clear
  363. /* 0x48 */ VK_VOLUME_UP,
  364. /* 0x49 */ VK_VOLUME_DOWN,
  365. /* 0x4A */ VK_VOLUME_MUTE,
  366. /* 0x4B */ VK_DIVIDE, // Num Pad /
  367. /* 0x4C */ VK_RETURN, // Num Pad Enter
  368. /* 0x4D */ 0, // n/a
  369. /* 0x4E */ VK_SUBTRACT, // Num Pad -
  370. /* 0x4F */ VK_F18,
  371. /* 0x50 */ VK_F19,
  372. /* 0x51 */ VK_OEM_PLUS, // Num Pad =. There is no such key on common PC keyboards, mapping to normal "+=".
  373. /* 0x52 */ VK_NUMPAD0,
  374. /* 0x53 */ VK_NUMPAD1,
  375. /* 0x54 */ VK_NUMPAD2,
  376. /* 0x55 */ VK_NUMPAD3,
  377. /* 0x56 */ VK_NUMPAD4,
  378. /* 0x57 */ VK_NUMPAD5,
  379. /* 0x58 */ VK_NUMPAD6,
  380. /* 0x59 */ VK_NUMPAD7,
  381. /* 0x5A */ VK_F20,
  382. /* 0x5B */ VK_NUMPAD8,
  383. /* 0x5C */ VK_NUMPAD9,
  384. /* 0x5D */ 0, // Yen (JIS Keyboard Only)
  385. /* 0x5E */ 0, // Underscore (JIS Keyboard Only)
  386. /* 0x5F */ 0, // KeypadComma (JIS Keyboard Only)
  387. /* 0x60 */ VK_F5,
  388. /* 0x61 */ VK_F6,
  389. /* 0x62 */ VK_F7,
  390. /* 0x63 */ VK_F3,
  391. /* 0x64 */ VK_F8,
  392. /* 0x65 */ VK_F9,
  393. /* 0x66 */ 0, // Eisu (JIS Keyboard Only)
  394. /* 0x67 */ VK_F11,
  395. /* 0x68 */ 0, // Kana (JIS Keyboard Only)
  396. /* 0x69 */ VK_F13,
  397. /* 0x6A */ VK_F16,
  398. /* 0x6B */ VK_F14,
  399. /* 0x6C */ 0, // n/a
  400. /* 0x6D */ VK_F10,
  401. /* 0x6E */ 0, // n/a (Windows95 key?)
  402. /* 0x6F */ VK_F12,
  403. /* 0x70 */ 0, // n/a
  404. /* 0x71 */ VK_F15,
  405. /* 0x72 */ VK_INSERT, // Help
  406. /* 0x73 */ VK_HOME, // Home
  407. /* 0x74 */ VK_PRIOR, // Page Up
  408. /* 0x75 */ VK_DELETE, // Forward Delete
  409. /* 0x76 */ VK_F4,
  410. /* 0x77 */ VK_END, // End
  411. /* 0x78 */ VK_F2,
  412. /* 0x79 */ VK_NEXT, // Page Down
  413. /* 0x7A */ VK_F1,
  414. /* 0x7B */ VK_LEFT, // Left Arrow
  415. /* 0x7C */ VK_RIGHT, // Right Arrow
  416. /* 0x7D */ VK_DOWN, // Down Arrow
  417. /* 0x7E */ VK_UP, // Up Arrow
  418. /* 0x7F */ 0 // n/a
  419. };
  420. if (keyCode >= 0x80)
  421. return 0;
  422. return windowsKeyCode[keyCode];
  423. }
  424. int windowsKeyCodeForCharCode(unichar charCode)
  425. {
  426. switch (charCode) {
  427. #if PLATFORM(IOS)
  428. case 8: case 0x7F: return VK_BACK;
  429. case 9: return VK_TAB;
  430. case 0xD: case 3: return VK_RETURN;
  431. case 0x1B: return VK_ESCAPE;
  432. case ' ': return VK_SPACE;
  433. case NSHomeFunctionKey: return VK_HOME;
  434. case NSEndFunctionKey: return VK_END;
  435. case NSPageUpFunctionKey: return VK_PRIOR;
  436. case NSPageDownFunctionKey: return VK_NEXT;
  437. case NSUpArrowFunctionKey: return VK_UP;
  438. case NSDownArrowFunctionKey: return VK_DOWN;
  439. case NSLeftArrowFunctionKey: return VK_LEFT;
  440. case NSRightArrowFunctionKey: return VK_RIGHT;
  441. case NSDeleteFunctionKey: return VK_DELETE;
  442. case '0': case ')': return VK_0;
  443. case '1': case '!': return VK_1;
  444. case '2': case '@': return VK_2;
  445. case '3': case '#': return VK_3;
  446. case '4': case '$': return VK_4;
  447. case '5': case '%': return VK_5;
  448. case '6': case '^': return VK_6;
  449. case '7': case '&': return VK_7;
  450. case '8': case '*': return VK_8;
  451. case '9': case '(': return VK_9;
  452. #endif
  453. case 'a': case 'A': return VK_A;
  454. case 'b': case 'B': return VK_B;
  455. case 'c': case 'C': return VK_C;
  456. case 'd': case 'D': return VK_D;
  457. case 'e': case 'E': return VK_E;
  458. case 'f': case 'F': return VK_F;
  459. case 'g': case 'G': return VK_G;
  460. case 'h': case 'H': return VK_H;
  461. case 'i': case 'I': return VK_I;
  462. case 'j': case 'J': return VK_J;
  463. case 'k': case 'K': return VK_K;
  464. case 'l': case 'L': return VK_L;
  465. case 'm': case 'M': return VK_M;
  466. case 'n': case 'N': return VK_N;
  467. case 'o': case 'O': return VK_O;
  468. case 'p': case 'P': return VK_P;
  469. case 'q': case 'Q': return VK_Q;
  470. case 'r': case 'R': return VK_R;
  471. case 's': case 'S': return VK_S;
  472. case 't': case 'T': return VK_T;
  473. case 'u': case 'U': return VK_U;
  474. case 'v': case 'V': return VK_V;
  475. case 'w': case 'W': return VK_W;
  476. case 'x': case 'X': return VK_X;
  477. case 'y': case 'Y': return VK_Y;
  478. case 'z': case 'Z': return VK_Z;
  479. // AppKit generates Unicode PUA character codes for some function keys; using these when key code is not known.
  480. case NSPauseFunctionKey: return VK_PAUSE;
  481. case NSSelectFunctionKey: return VK_SELECT;
  482. case NSPrintFunctionKey: return VK_PRINT;
  483. case NSExecuteFunctionKey: return VK_EXECUTE;
  484. case NSPrintScreenFunctionKey: return VK_SNAPSHOT;
  485. case NSInsertFunctionKey: return VK_INSERT;
  486. #if PLATFORM(IOS)
  487. case NSHelpFunctionKey: return VK_INSERT;
  488. case NSF1FunctionKey: return VK_F1;
  489. case NSF2FunctionKey: return VK_F2;
  490. case NSF3FunctionKey: return VK_F3;
  491. case NSF4FunctionKey: return VK_F4;
  492. case NSF5FunctionKey: return VK_F5;
  493. case NSF6FunctionKey: return VK_F6;
  494. case NSF7FunctionKey: return VK_F7;
  495. case NSF8FunctionKey: return VK_F8;
  496. case NSF9FunctionKey: return VK_F9;
  497. case NSF10FunctionKey: return VK_F10;
  498. case NSF11FunctionKey: return VK_F11;
  499. case NSF12FunctionKey: return VK_F12;
  500. case NSF13FunctionKey: return VK_F13;
  501. case NSF14FunctionKey: return VK_F14;
  502. case NSF15FunctionKey: return VK_F15;
  503. case NSF16FunctionKey: return VK_F16;
  504. case NSF17FunctionKey: return VK_F17;
  505. case NSF18FunctionKey: return VK_F18;
  506. case NSF19FunctionKey: return VK_F19;
  507. case NSF20FunctionKey: return VK_F20;
  508. #endif
  509. case NSF21FunctionKey: return VK_F21;
  510. case NSF22FunctionKey: return VK_F22;
  511. case NSF23FunctionKey: return VK_F23;
  512. case NSF24FunctionKey: return VK_F24;
  513. case NSScrollLockFunctionKey: return VK_SCROLL;
  514. // This is for U.S. keyboard mapping, and doesn't necessarily make sense for different keyboard layouts.
  515. // For example, '"' on Windows Russian layout is VK_2, not VK_OEM_7.
  516. case ';': case ':': return VK_OEM_1;
  517. case '=': case '+': return VK_OEM_PLUS;
  518. case ',': case '<': return VK_OEM_COMMA;
  519. case '-': case '_': return VK_OEM_MINUS;
  520. case '.': case '>': return VK_OEM_PERIOD;
  521. case '/': case '?': return VK_OEM_2;
  522. case '`': case '~': return VK_OEM_3;
  523. case '[': case '{': return VK_OEM_4;
  524. case '\\': case '|': return VK_OEM_5;
  525. case ']': case '}': return VK_OEM_6;
  526. case '\'': case '"': return VK_OEM_7;
  527. }
  528. return 0;
  529. }
  530. }