keyboard_util.cc 7.9 KB

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