keyboard_util.cc 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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/WebKit/public/platform/WebInputEvent.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. case '0':
  85. return ui::VKEY_0;
  86. case '!':
  87. *shifted = true;
  88. case '1':
  89. return ui::VKEY_1;
  90. case '@':
  91. *shifted = true;
  92. case '2':
  93. return ui::VKEY_2;
  94. case '#':
  95. *shifted = true;
  96. case '3':
  97. return ui::VKEY_3;
  98. case '$':
  99. *shifted = true;
  100. case '4':
  101. return ui::VKEY_4;
  102. case '%':
  103. *shifted = true;
  104. case '5':
  105. return ui::VKEY_5;
  106. case '^':
  107. *shifted = true;
  108. case '6':
  109. return ui::VKEY_6;
  110. case '&':
  111. *shifted = true;
  112. case '7':
  113. return ui::VKEY_7;
  114. case '*':
  115. *shifted = true;
  116. case '8':
  117. return ui::VKEY_8;
  118. case '(':
  119. *shifted = true;
  120. case '9':
  121. return ui::VKEY_9;
  122. case ':':
  123. *shifted = true;
  124. case ';':
  125. return ui::VKEY_OEM_1;
  126. case '+':
  127. *shifted = true;
  128. case '=':
  129. return ui::VKEY_OEM_PLUS;
  130. case '<':
  131. *shifted = true;
  132. case ',':
  133. return ui::VKEY_OEM_COMMA;
  134. case '_':
  135. *shifted = true;
  136. case '-':
  137. return ui::VKEY_OEM_MINUS;
  138. case '>':
  139. *shifted = true;
  140. case '.':
  141. return ui::VKEY_OEM_PERIOD;
  142. case '?':
  143. *shifted = true;
  144. case '/':
  145. return ui::VKEY_OEM_2;
  146. case '~':
  147. *shifted = true;
  148. case '`':
  149. return ui::VKEY_OEM_3;
  150. case '{':
  151. *shifted = true;
  152. case '[':
  153. return ui::VKEY_OEM_4;
  154. case '|':
  155. *shifted = true;
  156. case '\\':
  157. return ui::VKEY_OEM_5;
  158. case '}':
  159. *shifted = true;
  160. case ']':
  161. return ui::VKEY_OEM_6;
  162. case '"':
  163. *shifted = true;
  164. case '\'':
  165. return ui::VKEY_OEM_7;
  166. default:
  167. return ui::VKEY_UNKNOWN;
  168. }
  169. }
  170. // Return key code represented by |str|.
  171. ui::KeyboardCode KeyboardCodeFromKeyIdentifier(const std::string& s,
  172. bool* shifted) {
  173. std::string str = base::ToLowerASCII(s);
  174. if (str == "ctrl" || str == "control") {
  175. return ui::VKEY_CONTROL;
  176. } else if (str == "super" || str == "cmd" || str == "command" ||
  177. str == "meta") {
  178. return ui::VKEY_COMMAND;
  179. } else if (str == "commandorcontrol" || str == "cmdorctrl") {
  180. #if defined(OS_MACOSX)
  181. return ui::VKEY_COMMAND;
  182. #else
  183. return ui::VKEY_CONTROL;
  184. #endif
  185. } else if (str == "alt" || str == "option") {
  186. return ui::VKEY_MENU;
  187. } else if (str == "shift") {
  188. return ui::VKEY_SHIFT;
  189. } else if (str == "altgr") {
  190. return ui::VKEY_ALTGR;
  191. } else if (str == "plus") {
  192. *shifted = true;
  193. return ui::VKEY_OEM_PLUS;
  194. } else if (str == "tab") {
  195. return ui::VKEY_TAB;
  196. } else if (str == "space") {
  197. return ui::VKEY_SPACE;
  198. } else if (str == "backspace") {
  199. return ui::VKEY_BACK;
  200. } else if (str == "delete") {
  201. return ui::VKEY_DELETE;
  202. } else if (str == "insert") {
  203. return ui::VKEY_INSERT;
  204. } else if (str == "enter" || str == "return") {
  205. return ui::VKEY_RETURN;
  206. } else if (str == "up") {
  207. return ui::VKEY_UP;
  208. } else if (str == "down") {
  209. return ui::VKEY_DOWN;
  210. } else if (str == "left") {
  211. return ui::VKEY_LEFT;
  212. } else if (str == "right") {
  213. return ui::VKEY_RIGHT;
  214. } else if (str == "home") {
  215. return ui::VKEY_HOME;
  216. } else if (str == "end") {
  217. return ui::VKEY_END;
  218. } else if (str == "pageup") {
  219. return ui::VKEY_PRIOR;
  220. } else if (str == "pagedown") {
  221. return ui::VKEY_NEXT;
  222. } else if (str == "esc" || str == "escape") {
  223. return ui::VKEY_ESCAPE;
  224. } else if (str == "volumemute") {
  225. return ui::VKEY_VOLUME_MUTE;
  226. } else if (str == "volumeup") {
  227. return ui::VKEY_VOLUME_UP;
  228. } else if (str == "volumedown") {
  229. return ui::VKEY_VOLUME_DOWN;
  230. } else if (str == "medianexttrack") {
  231. return ui::VKEY_MEDIA_NEXT_TRACK;
  232. } else if (str == "mediaprevioustrack") {
  233. return ui::VKEY_MEDIA_PREV_TRACK;
  234. } else if (str == "mediastop") {
  235. return ui::VKEY_MEDIA_STOP;
  236. } else if (str == "mediaplaypause") {
  237. return ui::VKEY_MEDIA_PLAY_PAUSE;
  238. } else if (str == "printscreen") {
  239. return ui::VKEY_SNAPSHOT;
  240. } else if (str.size() > 1 && str[0] == 'f') {
  241. // F1 - F24.
  242. int n;
  243. if (base::StringToInt(str.c_str() + 1, &n) && n > 0 && n < 25) {
  244. return static_cast<ui::KeyboardCode>(ui::VKEY_F1 + n - 1);
  245. } else {
  246. LOG(WARNING) << str << "is not available on keyboard";
  247. return ui::VKEY_UNKNOWN;
  248. }
  249. } else {
  250. if (str.size() > 2)
  251. LOG(WARNING) << "Invalid accelerator token: " << str;
  252. return ui::VKEY_UNKNOWN;
  253. }
  254. }
  255. } // namespace
  256. ui::KeyboardCode KeyboardCodeFromStr(const std::string& str, bool* shifted) {
  257. if (str.size() == 1)
  258. return KeyboardCodeFromCharCode(str[0], shifted);
  259. else
  260. return KeyboardCodeFromKeyIdentifier(str, shifted);
  261. }
  262. int WebEventModifiersToEventFlags(int modifiers) {
  263. int flags = 0;
  264. if (modifiers & blink::WebInputEvent::kShiftKey)
  265. flags |= ui::EF_SHIFT_DOWN;
  266. if (modifiers & blink::WebInputEvent::kControlKey)
  267. flags |= ui::EF_CONTROL_DOWN;
  268. if (modifiers & blink::WebInputEvent::kAltKey)
  269. flags |= ui::EF_ALT_DOWN;
  270. if (modifiers & blink::WebInputEvent::kMetaKey)
  271. flags |= ui::EF_COMMAND_DOWN;
  272. if (modifiers & blink::WebInputEvent::kCapsLockOn)
  273. flags |= ui::EF_CAPS_LOCK_ON;
  274. if (modifiers & blink::WebInputEvent::kNumLockOn)
  275. flags |= ui::EF_NUM_LOCK_ON;
  276. if (modifiers & blink::WebInputEvent::kScrollLockOn)
  277. flags |= ui::EF_SCROLL_LOCK_ON;
  278. if (modifiers & blink::WebInputEvent::kLeftButtonDown)
  279. flags |= ui::EF_LEFT_MOUSE_BUTTON;
  280. if (modifiers & blink::WebInputEvent::kMiddleButtonDown)
  281. flags |= ui::EF_MIDDLE_MOUSE_BUTTON;
  282. if (modifiers & blink::WebInputEvent::kRightButtonDown)
  283. flags |= ui::EF_RIGHT_MOUSE_BUTTON;
  284. if (modifiers & blink::WebInputEvent::kIsAutoRepeat)
  285. flags |= ui::EF_IS_REPEAT;
  286. return flags;
  287. }
  288. } // namespace atom