keyboard_util.cc 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. // Copyright (c) 2015 GitHub, Inc.
  2. // Use of this source code is governed by the MIT license that can be
  3. // found in the LICENSE file.
  4. #include <string>
  5. #include "atom/common/keyboard_util.h"
  6. #include "base/strings/string_number_conversions.h"
  7. #include "base/strings/string_util.h"
  8. #include "third_party/blink/public/platform/web_input_event.h"
  9. #include "ui/events/event_constants.h"
  10. namespace atom {
  11. namespace {
  12. // Return key code represented by |str|.
  13. ui::KeyboardCode KeyboardCodeFromKeyIdentifier(const std::string& s,
  14. bool* shifted) {
  15. std::string str = base::ToLowerASCII(s);
  16. if (str == "ctrl" || str == "control") {
  17. return ui::VKEY_CONTROL;
  18. } else if (str == "super" || str == "cmd" || str == "command" ||
  19. str == "meta") {
  20. return ui::VKEY_COMMAND;
  21. } else if (str == "commandorcontrol" || str == "cmdorctrl") {
  22. #if defined(OS_MACOSX)
  23. return ui::VKEY_COMMAND;
  24. #else
  25. return ui::VKEY_CONTROL;
  26. #endif
  27. } else if (str == "alt" || str == "option") {
  28. return ui::VKEY_MENU;
  29. } else if (str == "shift") {
  30. return ui::VKEY_SHIFT;
  31. } else if (str == "altgr") {
  32. return ui::VKEY_ALTGR;
  33. } else if (str == "plus") {
  34. *shifted = true;
  35. return ui::VKEY_OEM_PLUS;
  36. } else if (str == "capslock") {
  37. return ui::VKEY_CAPITAL;
  38. } else if (str == "numlock") {
  39. return ui::VKEY_NUMLOCK;
  40. } else if (str == "scrolllock") {
  41. return ui::VKEY_SCROLL;
  42. } else if (str == "tab") {
  43. return ui::VKEY_TAB;
  44. } else if (str == "num0") {
  45. return ui::VKEY_NUMPAD0;
  46. } else if (str == "num1") {
  47. return ui::VKEY_NUMPAD1;
  48. } else if (str == "num2") {
  49. return ui::VKEY_NUMPAD2;
  50. } else if (str == "num3") {
  51. return ui::VKEY_NUMPAD3;
  52. } else if (str == "num4") {
  53. return ui::VKEY_NUMPAD4;
  54. } else if (str == "num5") {
  55. return ui::VKEY_NUMPAD5;
  56. } else if (str == "num6") {
  57. return ui::VKEY_NUMPAD6;
  58. } else if (str == "num7") {
  59. return ui::VKEY_NUMPAD7;
  60. } else if (str == "num8") {
  61. return ui::VKEY_NUMPAD8;
  62. } else if (str == "num9") {
  63. return ui::VKEY_NUMPAD9;
  64. } else if (str == "numadd") {
  65. return ui::VKEY_ADD;
  66. } else if (str == "nummult") {
  67. return ui::VKEY_MULTIPLY;
  68. } else if (str == "numdec") {
  69. return ui::VKEY_DECIMAL;
  70. } else if (str == "numsub") {
  71. return ui::VKEY_SUBTRACT;
  72. } else if (str == "numdiv") {
  73. return ui::VKEY_DIVIDE;
  74. } else if (str == "space") {
  75. return ui::VKEY_SPACE;
  76. } else if (str == "backspace") {
  77. return ui::VKEY_BACK;
  78. } else if (str == "delete") {
  79. return ui::VKEY_DELETE;
  80. } else if (str == "insert") {
  81. return ui::VKEY_INSERT;
  82. } else if (str == "enter" || str == "return") {
  83. return ui::VKEY_RETURN;
  84. } else if (str == "up") {
  85. return ui::VKEY_UP;
  86. } else if (str == "down") {
  87. return ui::VKEY_DOWN;
  88. } else if (str == "left") {
  89. return ui::VKEY_LEFT;
  90. } else if (str == "right") {
  91. return ui::VKEY_RIGHT;
  92. } else if (str == "home") {
  93. return ui::VKEY_HOME;
  94. } else if (str == "end") {
  95. return ui::VKEY_END;
  96. } else if (str == "pageup") {
  97. return ui::VKEY_PRIOR;
  98. } else if (str == "pagedown") {
  99. return ui::VKEY_NEXT;
  100. } else if (str == "esc" || str == "escape") {
  101. return ui::VKEY_ESCAPE;
  102. } else if (str == "volumemute") {
  103. return ui::VKEY_VOLUME_MUTE;
  104. } else if (str == "volumeup") {
  105. return ui::VKEY_VOLUME_UP;
  106. } else if (str == "volumedown") {
  107. return ui::VKEY_VOLUME_DOWN;
  108. } else if (str == "medianexttrack") {
  109. return ui::VKEY_MEDIA_NEXT_TRACK;
  110. } else if (str == "mediaprevioustrack") {
  111. return ui::VKEY_MEDIA_PREV_TRACK;
  112. } else if (str == "mediastop") {
  113. return ui::VKEY_MEDIA_STOP;
  114. } else if (str == "mediaplaypause") {
  115. return ui::VKEY_MEDIA_PLAY_PAUSE;
  116. } else if (str == "printscreen") {
  117. return ui::VKEY_SNAPSHOT;
  118. } else if (str.size() > 1 && str[0] == 'f') {
  119. // F1 - F24.
  120. int n;
  121. if (base::StringToInt(str.c_str() + 1, &n) && n > 0 && n < 25) {
  122. return static_cast<ui::KeyboardCode>(ui::VKEY_F1 + n - 1);
  123. } else {
  124. LOG(WARNING) << str << "is not available on keyboard";
  125. return ui::VKEY_UNKNOWN;
  126. }
  127. } else {
  128. if (str.size() > 2)
  129. LOG(WARNING) << "Invalid accelerator token: " << str;
  130. return ui::VKEY_UNKNOWN;
  131. }
  132. }
  133. } // namespace
  134. ui::KeyboardCode KeyboardCodeFromCharCode(base::char16 c, bool* shifted) {
  135. c = base::ToLowerASCII(c);
  136. *shifted = false;
  137. switch (c) {
  138. case 0x08:
  139. return ui::VKEY_BACK;
  140. case 0x7F:
  141. return ui::VKEY_DELETE;
  142. case 0x09:
  143. return ui::VKEY_TAB;
  144. case 0x0D:
  145. return ui::VKEY_RETURN;
  146. case 0x1B:
  147. return ui::VKEY_ESCAPE;
  148. case ' ':
  149. return ui::VKEY_SPACE;
  150. case 'a':
  151. return ui::VKEY_A;
  152. case 'b':
  153. return ui::VKEY_B;
  154. case 'c':
  155. return ui::VKEY_C;
  156. case 'd':
  157. return ui::VKEY_D;
  158. case 'e':
  159. return ui::VKEY_E;
  160. case 'f':
  161. return ui::VKEY_F;
  162. case 'g':
  163. return ui::VKEY_G;
  164. case 'h':
  165. return ui::VKEY_H;
  166. case 'i':
  167. return ui::VKEY_I;
  168. case 'j':
  169. return ui::VKEY_J;
  170. case 'k':
  171. return ui::VKEY_K;
  172. case 'l':
  173. return ui::VKEY_L;
  174. case 'm':
  175. return ui::VKEY_M;
  176. case 'n':
  177. return ui::VKEY_N;
  178. case 'o':
  179. return ui::VKEY_O;
  180. case 'p':
  181. return ui::VKEY_P;
  182. case 'q':
  183. return ui::VKEY_Q;
  184. case 'r':
  185. return ui::VKEY_R;
  186. case 's':
  187. return ui::VKEY_S;
  188. case 't':
  189. return ui::VKEY_T;
  190. case 'u':
  191. return ui::VKEY_U;
  192. case 'v':
  193. return ui::VKEY_V;
  194. case 'w':
  195. return ui::VKEY_W;
  196. case 'x':
  197. return ui::VKEY_X;
  198. case 'y':
  199. return ui::VKEY_Y;
  200. case 'z':
  201. return ui::VKEY_Z;
  202. case ')':
  203. *shifted = true;
  204. FALLTHROUGH;
  205. case '0':
  206. return ui::VKEY_0;
  207. case '!':
  208. *shifted = true;
  209. FALLTHROUGH;
  210. case '1':
  211. return ui::VKEY_1;
  212. case '@':
  213. *shifted = true;
  214. FALLTHROUGH;
  215. case '2':
  216. return ui::VKEY_2;
  217. case '#':
  218. *shifted = true;
  219. FALLTHROUGH;
  220. case '3':
  221. return ui::VKEY_3;
  222. case '$':
  223. *shifted = true;
  224. FALLTHROUGH;
  225. case '4':
  226. return ui::VKEY_4;
  227. case '%':
  228. *shifted = true;
  229. FALLTHROUGH;
  230. case '5':
  231. return ui::VKEY_5;
  232. case '^':
  233. *shifted = true;
  234. FALLTHROUGH;
  235. case '6':
  236. return ui::VKEY_6;
  237. case '&':
  238. *shifted = true;
  239. FALLTHROUGH;
  240. case '7':
  241. return ui::VKEY_7;
  242. case '*':
  243. *shifted = true;
  244. FALLTHROUGH;
  245. case '8':
  246. return ui::VKEY_8;
  247. case '(':
  248. *shifted = true;
  249. FALLTHROUGH;
  250. case '9':
  251. return ui::VKEY_9;
  252. case ':':
  253. *shifted = true;
  254. FALLTHROUGH;
  255. case ';':
  256. return ui::VKEY_OEM_1;
  257. case '+':
  258. *shifted = true;
  259. FALLTHROUGH;
  260. case '=':
  261. return ui::VKEY_OEM_PLUS;
  262. case '<':
  263. *shifted = true;
  264. FALLTHROUGH;
  265. case ',':
  266. return ui::VKEY_OEM_COMMA;
  267. case '_':
  268. *shifted = true;
  269. FALLTHROUGH;
  270. case '-':
  271. return ui::VKEY_OEM_MINUS;
  272. case '>':
  273. *shifted = true;
  274. FALLTHROUGH;
  275. case '.':
  276. return ui::VKEY_OEM_PERIOD;
  277. case '?':
  278. *shifted = true;
  279. FALLTHROUGH;
  280. case '/':
  281. return ui::VKEY_OEM_2;
  282. case '~':
  283. *shifted = true;
  284. FALLTHROUGH;
  285. case '`':
  286. return ui::VKEY_OEM_3;
  287. case '{':
  288. *shifted = true;
  289. FALLTHROUGH;
  290. case '[':
  291. return ui::VKEY_OEM_4;
  292. case '|':
  293. *shifted = true;
  294. FALLTHROUGH;
  295. case '\\':
  296. return ui::VKEY_OEM_5;
  297. case '}':
  298. *shifted = true;
  299. FALLTHROUGH;
  300. case ']':
  301. return ui::VKEY_OEM_6;
  302. case '"':
  303. *shifted = true;
  304. FALLTHROUGH;
  305. case '\'':
  306. return ui::VKEY_OEM_7;
  307. default:
  308. return ui::VKEY_UNKNOWN;
  309. }
  310. }
  311. ui::KeyboardCode KeyboardCodeFromStr(const std::string& str, bool* shifted) {
  312. if (str.size() == 1)
  313. return KeyboardCodeFromCharCode(str[0], shifted);
  314. else
  315. return KeyboardCodeFromKeyIdentifier(str, shifted);
  316. }
  317. int WebEventModifiersToEventFlags(int modifiers) {
  318. int flags = 0;
  319. if (modifiers & blink::WebInputEvent::kShiftKey)
  320. flags |= ui::EF_SHIFT_DOWN;
  321. if (modifiers & blink::WebInputEvent::kControlKey)
  322. flags |= ui::EF_CONTROL_DOWN;
  323. if (modifiers & blink::WebInputEvent::kAltKey)
  324. flags |= ui::EF_ALT_DOWN;
  325. if (modifiers & blink::WebInputEvent::kMetaKey)
  326. flags |= ui::EF_COMMAND_DOWN;
  327. if (modifiers & blink::WebInputEvent::kCapsLockOn)
  328. flags |= ui::EF_CAPS_LOCK_ON;
  329. if (modifiers & blink::WebInputEvent::kNumLockOn)
  330. flags |= ui::EF_NUM_LOCK_ON;
  331. if (modifiers & blink::WebInputEvent::kScrollLockOn)
  332. flags |= ui::EF_SCROLL_LOCK_ON;
  333. if (modifiers & blink::WebInputEvent::kLeftButtonDown)
  334. flags |= ui::EF_LEFT_MOUSE_BUTTON;
  335. if (modifiers & blink::WebInputEvent::kMiddleButtonDown)
  336. flags |= ui::EF_MIDDLE_MOUSE_BUTTON;
  337. if (modifiers & blink::WebInputEvent::kRightButtonDown)
  338. flags |= ui::EF_RIGHT_MOUSE_BUTTON;
  339. if (modifiers & blink::WebInputEvent::kIsAutoRepeat)
  340. flags |= ui::EF_IS_REPEAT;
  341. return flags;
  342. }
  343. } // namespace atom