Shortcut.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <AK/String.h>
  27. #include <AK/StringBuilder.h>
  28. #include <AK/Vector.h>
  29. #include <LibGUI/Shortcut.h>
  30. namespace GUI {
  31. static String key_code_to_string(KeyCode key)
  32. {
  33. switch (key) {
  34. case Key_Escape:
  35. return "Escape";
  36. case Key_Tab:
  37. return "Tab";
  38. case Key_Backspace:
  39. return "Backspace";
  40. case Key_Return:
  41. return "Return";
  42. case Key_Insert:
  43. return "Insert";
  44. case Key_Delete:
  45. return "Delete";
  46. case Key_PrintScreen:
  47. return "PrintScreen";
  48. case Key_SysRq:
  49. return "SysRq";
  50. case Key_Home:
  51. return "Home";
  52. case Key_End:
  53. return "End";
  54. case Key_Left:
  55. return "Left";
  56. case Key_Up:
  57. return "Up";
  58. case Key_Right:
  59. return "Right";
  60. case Key_Down:
  61. return "Down";
  62. case Key_PageUp:
  63. return "PageUp";
  64. case Key_PageDown:
  65. return "PageDown";
  66. case Key_Shift:
  67. return "Shift";
  68. case Key_Control:
  69. return "Control";
  70. case Key_Alt:
  71. return "Alt";
  72. case Key_CapsLock:
  73. return "CapsLock";
  74. case Key_NumLock:
  75. return "NumLock";
  76. case Key_ScrollLock:
  77. return "ScrollLock";
  78. case Key_F1:
  79. return "F1";
  80. case Key_F2:
  81. return "F2";
  82. case Key_F3:
  83. return "F3";
  84. case Key_F4:
  85. return "F4";
  86. case Key_F5:
  87. return "F5";
  88. case Key_F6:
  89. return "F6";
  90. case Key_F7:
  91. return "F7";
  92. case Key_F8:
  93. return "F8";
  94. case Key_F9:
  95. return "F9";
  96. case Key_F10:
  97. return "F10";
  98. case Key_F11:
  99. return "F11";
  100. case Key_F12:
  101. return "F12";
  102. case Key_Space:
  103. return "Space";
  104. case Key_ExclamationPoint:
  105. return "!";
  106. case Key_DoubleQuote:
  107. return "\"";
  108. case Key_Hashtag:
  109. return "#";
  110. case Key_Dollar:
  111. return "$";
  112. case Key_Percent:
  113. return "%";
  114. case Key_Ampersand:
  115. return "&";
  116. case Key_Apostrophe:
  117. return "'";
  118. case Key_LeftParen:
  119. return "(";
  120. case Key_RightParen:
  121. return ")";
  122. case Key_Asterisk:
  123. return "*";
  124. case Key_Plus:
  125. return "+";
  126. case Key_Comma:
  127. return ",";
  128. case Key_Minus:
  129. return "-";
  130. case Key_Period:
  131. return ",";
  132. case Key_Slash:
  133. return "/";
  134. case Key_0:
  135. return "0";
  136. case Key_1:
  137. return "1";
  138. case Key_2:
  139. return "2";
  140. case Key_3:
  141. return "3";
  142. case Key_4:
  143. return "4";
  144. case Key_5:
  145. return "5";
  146. case Key_6:
  147. return "6";
  148. case Key_7:
  149. return "7";
  150. case Key_8:
  151. return "8";
  152. case Key_9:
  153. return "9";
  154. case Key_Colon:
  155. return ":";
  156. case Key_Semicolon:
  157. return ";";
  158. case Key_LessThan:
  159. return "<";
  160. case Key_Equal:
  161. return "=";
  162. case Key_GreaterThan:
  163. return ">";
  164. case Key_QuestionMark:
  165. return "?";
  166. case Key_AtSign:
  167. return "@";
  168. case Key_A:
  169. return "A";
  170. case Key_B:
  171. return "B";
  172. case Key_C:
  173. return "C";
  174. case Key_D:
  175. return "D";
  176. case Key_E:
  177. return "E";
  178. case Key_F:
  179. return "F";
  180. case Key_G:
  181. return "G";
  182. case Key_H:
  183. return "H";
  184. case Key_I:
  185. return "I";
  186. case Key_J:
  187. return "J";
  188. case Key_K:
  189. return "K";
  190. case Key_L:
  191. return "L";
  192. case Key_M:
  193. return "M";
  194. case Key_N:
  195. return "N";
  196. case Key_O:
  197. return "O";
  198. case Key_P:
  199. return "P";
  200. case Key_Q:
  201. return "Q";
  202. case Key_R:
  203. return "R";
  204. case Key_S:
  205. return "S";
  206. case Key_T:
  207. return "T";
  208. case Key_U:
  209. return "U";
  210. case Key_V:
  211. return "V";
  212. case Key_W:
  213. return "W";
  214. case Key_X:
  215. return "X";
  216. case Key_Y:
  217. return "Y";
  218. case Key_Z:
  219. return "Z";
  220. case Key_LeftBracket:
  221. return "[";
  222. case Key_RightBracket:
  223. return "]";
  224. case Key_Backslash:
  225. return "\\";
  226. case Key_Circumflex:
  227. return "^";
  228. case Key_Underscore:
  229. return "_";
  230. case Key_LeftBrace:
  231. return "{";
  232. case Key_RightBrace:
  233. return "}";
  234. case Key_Pipe:
  235. return "|";
  236. case Key_Tilde:
  237. return "~";
  238. case Key_Backtick:
  239. return "`";
  240. case Key_Invalid:
  241. return "Invalid";
  242. default:
  243. ASSERT_NOT_REACHED();
  244. }
  245. }
  246. String Shortcut::to_string() const
  247. {
  248. Vector<String, 8> parts;
  249. if (m_modifiers & Mod_Ctrl)
  250. parts.append("Ctrl");
  251. if (m_modifiers & Mod_Shift)
  252. parts.append("Shift");
  253. if (m_modifiers & Mod_Alt)
  254. parts.append("Alt");
  255. if (m_modifiers & Mod_Logo)
  256. parts.append("Logo");
  257. parts.append(key_code_to_string(m_key));
  258. StringBuilder builder;
  259. for (size_t i = 0; i < parts.size(); ++i) {
  260. builder.append(parts[i]);
  261. if (i != parts.size() - 1)
  262. builder.append('+');
  263. }
  264. return builder.to_string();
  265. }
  266. }