keyboard_util.cc 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. namespace atom {
  9. namespace {
  10. // Return key code of the char, and also determine whether the SHIFT key is
  11. // pressed.
  12. ui::KeyboardCode KeyboardCodeFromCharCode(base::char16 c, bool* shifted) {
  13. c = base::ToLowerASCII(c);
  14. *shifted = false;
  15. switch (c) {
  16. case 0x08: return ui::VKEY_BACK;
  17. case 0x7F: return ui::VKEY_DELETE;
  18. case 0x09: return ui::VKEY_TAB;
  19. case 0x0D: return ui::VKEY_RETURN;
  20. case 0x1B: return ui::VKEY_ESCAPE;
  21. case ' ': return ui::VKEY_SPACE;
  22. case 'a': return ui::VKEY_A;
  23. case 'b': return ui::VKEY_B;
  24. case 'c': return ui::VKEY_C;
  25. case 'd': return ui::VKEY_D;
  26. case 'e': return ui::VKEY_E;
  27. case 'f': return ui::VKEY_F;
  28. case 'g': return ui::VKEY_G;
  29. case 'h': return ui::VKEY_H;
  30. case 'i': return ui::VKEY_I;
  31. case 'j': return ui::VKEY_J;
  32. case 'k': return ui::VKEY_K;
  33. case 'l': return ui::VKEY_L;
  34. case 'm': return ui::VKEY_M;
  35. case 'n': return ui::VKEY_N;
  36. case 'o': return ui::VKEY_O;
  37. case 'p': return ui::VKEY_P;
  38. case 'q': return ui::VKEY_Q;
  39. case 'r': return ui::VKEY_R;
  40. case 's': return ui::VKEY_S;
  41. case 't': return ui::VKEY_T;
  42. case 'u': return ui::VKEY_U;
  43. case 'v': return ui::VKEY_V;
  44. case 'w': return ui::VKEY_W;
  45. case 'x': return ui::VKEY_X;
  46. case 'y': return ui::VKEY_Y;
  47. case 'z': return ui::VKEY_Z;
  48. case ')': *shifted = true; case '0': return ui::VKEY_0;
  49. case '!': *shifted = true; case '1': return ui::VKEY_1;
  50. case '@': *shifted = true; case '2': return ui::VKEY_2;
  51. case '#': *shifted = true; case '3': return ui::VKEY_3;
  52. case '$': *shifted = true; case '4': return ui::VKEY_4;
  53. case '%': *shifted = true; case '5': return ui::VKEY_5;
  54. case '^': *shifted = true; case '6': return ui::VKEY_6;
  55. case '&': *shifted = true; case '7': return ui::VKEY_7;
  56. case '*': *shifted = true; case '8': return ui::VKEY_8;
  57. case '(': *shifted = true; case '9': return ui::VKEY_9;
  58. case ':': *shifted = true; case ';': return ui::VKEY_OEM_1;
  59. case '+': *shifted = true; case '=': return ui::VKEY_OEM_PLUS;
  60. case '<': *shifted = true; case ',': return ui::VKEY_OEM_COMMA;
  61. case '_': *shifted = true; case '-': return ui::VKEY_OEM_MINUS;
  62. case '>': *shifted = true; case '.': return ui::VKEY_OEM_PERIOD;
  63. case '?': *shifted = true; case '/': return ui::VKEY_OEM_2;
  64. case '~': *shifted = true; case '`': return ui::VKEY_OEM_3;
  65. case '{': *shifted = true; case '[': return ui::VKEY_OEM_4;
  66. case '|': *shifted = true; case '\\': return ui::VKEY_OEM_5;
  67. case '}': *shifted = true; case ']': return ui::VKEY_OEM_6;
  68. case '"': *shifted = true; case '\'': return ui::VKEY_OEM_7;
  69. default: return ui::VKEY_UNKNOWN;
  70. }
  71. }
  72. // Return key code represented by |str|.
  73. ui::KeyboardCode KeyboardCodeFromKeyIdentifier(const std::string& s,
  74. bool* shifted) {
  75. std::string str = base::ToLowerASCII(s);
  76. if (str == "ctrl" || str == "control") {
  77. return ui::VKEY_CONTROL;
  78. } else if (str == "super" || str == "cmd" || str == "command" ||
  79. str == "meta") {
  80. return ui::VKEY_COMMAND;
  81. } else if (str == "commandorcontrol" || str == "cmdorctrl") {
  82. #if defined(OS_MACOSX)
  83. return ui::VKEY_COMMAND;
  84. #else
  85. return ui::VKEY_CONTROL;
  86. #endif
  87. } else if (str == "alt" || str == "option") {
  88. return ui::VKEY_MENU;
  89. } else if (str == "shift") {
  90. return ui::VKEY_SHIFT;
  91. } else if (str == "altgr") {
  92. return ui::VKEY_ALTGR;
  93. } else if (str == "plus") {
  94. *shifted = true;
  95. return ui::VKEY_OEM_PLUS;
  96. } else if (str == "tab") {
  97. return ui::VKEY_TAB;
  98. } else if (str == "space") {
  99. return ui::VKEY_SPACE;
  100. } else if (str == "backspace") {
  101. return ui::VKEY_BACK;
  102. } else if (str == "delete") {
  103. return ui::VKEY_DELETE;
  104. } else if (str == "insert") {
  105. return ui::VKEY_INSERT;
  106. } else if (str == "enter" || str == "return") {
  107. return ui::VKEY_RETURN;
  108. } else if (str == "up") {
  109. return ui::VKEY_UP;
  110. } else if (str == "down") {
  111. return ui::VKEY_DOWN;
  112. } else if (str == "left") {
  113. return ui::VKEY_LEFT;
  114. } else if (str == "right") {
  115. return ui::VKEY_RIGHT;
  116. } else if (str == "home") {
  117. return ui::VKEY_HOME;
  118. } else if (str == "end") {
  119. return ui::VKEY_END;
  120. } else if (str == "pageup") {
  121. return ui::VKEY_PRIOR;
  122. } else if (str == "pagedown") {
  123. return ui::VKEY_NEXT;
  124. } else if (str == "esc" || str == "escape") {
  125. return ui::VKEY_ESCAPE;
  126. } else if (str == "volumemute") {
  127. return ui::VKEY_VOLUME_MUTE;
  128. } else if (str == "volumeup") {
  129. return ui::VKEY_VOLUME_UP;
  130. } else if (str == "volumedown") {
  131. return ui::VKEY_VOLUME_DOWN;
  132. } else if (str == "medianexttrack") {
  133. return ui::VKEY_MEDIA_NEXT_TRACK;
  134. } else if (str == "mediaprevioustrack") {
  135. return ui::VKEY_MEDIA_PREV_TRACK;
  136. } else if (str == "mediastop") {
  137. return ui::VKEY_MEDIA_STOP;
  138. } else if (str == "mediaplaypause") {
  139. return ui::VKEY_MEDIA_PLAY_PAUSE;
  140. } else if (str == "printscreen") {
  141. return ui::VKEY_SNAPSHOT;
  142. } else if (str.size() > 1 && str[0] == 'f') {
  143. // F1 - F24.
  144. int n;
  145. if (base::StringToInt(str.c_str() + 1, &n) && n > 0 && n < 25) {
  146. return static_cast<ui::KeyboardCode>(ui::VKEY_F1 + n - 1);
  147. } else {
  148. LOG(WARNING) << str << "is not available on keyboard";
  149. return ui::VKEY_UNKNOWN;
  150. }
  151. } else {
  152. if (str.size() > 2)
  153. LOG(WARNING) << "Invalid accelerator token: " << str;
  154. return ui::VKEY_UNKNOWN;
  155. }
  156. }
  157. } // namespace
  158. ui::KeyboardCode KeyboardCodeFromStr(const std::string& str, bool* shifted) {
  159. if (str.size() == 1)
  160. return KeyboardCodeFromCharCode(str[0], shifted);
  161. else
  162. return KeyboardCodeFromKeyIdentifier(str, shifted);
  163. }
  164. } // namespace atom