keyboard_util.cc 8.0 KB

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