Shortcut.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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/StringBuilder.h>
  27. #include <LibGUI/Shortcut.h>
  28. namespace GUI {
  29. static String key_code_to_string(KeyCode key)
  30. {
  31. switch (key) {
  32. case Key_Escape:
  33. return "Escape";
  34. case Key_Tab:
  35. return "Tab";
  36. case Key_Backspace:
  37. return "Backspace";
  38. case Key_Return:
  39. return "Return";
  40. case Key_Insert:
  41. return "Insert";
  42. case Key_Delete:
  43. return "Delete";
  44. case Key_PrintScreen:
  45. return "PrintScreen";
  46. case Key_SysRq:
  47. return "SysRq";
  48. case Key_Home:
  49. return "Home";
  50. case Key_End:
  51. return "End";
  52. case Key_Left:
  53. return "Left";
  54. case Key_Up:
  55. return "Up";
  56. case Key_Right:
  57. return "Right";
  58. case Key_Down:
  59. return "Down";
  60. case Key_PageUp:
  61. return "PageUp";
  62. case Key_PageDown:
  63. return "PageDown";
  64. case Key_Shift:
  65. return "Shift";
  66. case Key_Control:
  67. return "Control";
  68. case Key_Alt:
  69. return "Alt";
  70. case Key_CapsLock:
  71. return "CapsLock";
  72. case Key_NumLock:
  73. return "NumLock";
  74. case Key_ScrollLock:
  75. return "ScrollLock";
  76. case Key_F1:
  77. return "F1";
  78. case Key_F2:
  79. return "F2";
  80. case Key_F3:
  81. return "F3";
  82. case Key_F4:
  83. return "F4";
  84. case Key_F5:
  85. return "F5";
  86. case Key_F6:
  87. return "F6";
  88. case Key_F7:
  89. return "F7";
  90. case Key_F8:
  91. return "F8";
  92. case Key_F9:
  93. return "F9";
  94. case Key_F10:
  95. return "F10";
  96. case Key_F11:
  97. return "F11";
  98. case Key_F12:
  99. return "F12";
  100. case Key_Space:
  101. return "Space";
  102. case Key_ExclamationPoint:
  103. return "!";
  104. case Key_DoubleQuote:
  105. return "\"";
  106. case Key_Hashtag:
  107. return "#";
  108. case Key_Dollar:
  109. return "$";
  110. case Key_Percent:
  111. return "%";
  112. case Key_Ampersand:
  113. return "&";
  114. case Key_Apostrophe:
  115. return "'";
  116. case Key_LeftParen:
  117. return "(";
  118. case Key_RightParen:
  119. return ")";
  120. case Key_Asterisk:
  121. return "*";
  122. case Key_Plus:
  123. return "+";
  124. case Key_Comma:
  125. return ",";
  126. case Key_Minus:
  127. return "-";
  128. case Key_Period:
  129. return ",";
  130. case Key_Slash:
  131. return "/";
  132. case Key_0:
  133. return "0";
  134. case Key_1:
  135. return "1";
  136. case Key_2:
  137. return "2";
  138. case Key_3:
  139. return "3";
  140. case Key_4:
  141. return "4";
  142. case Key_5:
  143. return "5";
  144. case Key_6:
  145. return "6";
  146. case Key_7:
  147. return "7";
  148. case Key_8:
  149. return "8";
  150. case Key_9:
  151. return "9";
  152. case Key_Colon:
  153. return ":";
  154. case Key_Semicolon:
  155. return ";";
  156. case Key_LessThan:
  157. return "<";
  158. case Key_Equal:
  159. return "=";
  160. case Key_GreaterThan:
  161. return ">";
  162. case Key_QuestionMark:
  163. return "?";
  164. case Key_AtSign:
  165. return "@";
  166. case Key_A:
  167. return "A";
  168. case Key_B:
  169. return "B";
  170. case Key_C:
  171. return "C";
  172. case Key_D:
  173. return "D";
  174. case Key_E:
  175. return "E";
  176. case Key_F:
  177. return "F";
  178. case Key_G:
  179. return "G";
  180. case Key_H:
  181. return "H";
  182. case Key_I:
  183. return "I";
  184. case Key_J:
  185. return "J";
  186. case Key_K:
  187. return "K";
  188. case Key_L:
  189. return "L";
  190. case Key_M:
  191. return "M";
  192. case Key_N:
  193. return "N";
  194. case Key_O:
  195. return "O";
  196. case Key_P:
  197. return "P";
  198. case Key_Q:
  199. return "Q";
  200. case Key_R:
  201. return "R";
  202. case Key_S:
  203. return "S";
  204. case Key_T:
  205. return "T";
  206. case Key_U:
  207. return "U";
  208. case Key_V:
  209. return "V";
  210. case Key_W:
  211. return "W";
  212. case Key_X:
  213. return "X";
  214. case Key_Y:
  215. return "Y";
  216. case Key_Z:
  217. return "Z";
  218. case Key_LeftBracket:
  219. return "[";
  220. case Key_RightBracket:
  221. return "]";
  222. case Key_Backslash:
  223. return "\\";
  224. case Key_Circumflex:
  225. return "^";
  226. case Key_Underscore:
  227. return "_";
  228. case Key_LeftBrace:
  229. return "{";
  230. case Key_RightBrace:
  231. return "}";
  232. case Key_Pipe:
  233. return "|";
  234. case Key_Tilde:
  235. return "~";
  236. case Key_Backtick:
  237. return "`";
  238. case Key_Invalid:
  239. return "Invalid";
  240. default:
  241. ASSERT_NOT_REACHED();
  242. }
  243. }
  244. String Shortcut::to_string() const
  245. {
  246. Vector<String, 8> parts;
  247. if (m_modifiers & Mod_Ctrl)
  248. parts.append("Ctrl");
  249. if (m_modifiers & Mod_Shift)
  250. parts.append("Shift");
  251. if (m_modifiers & Mod_Alt)
  252. parts.append("Alt");
  253. if (m_modifiers & Mod_Logo)
  254. parts.append("Logo");
  255. parts.append(key_code_to_string(m_key));
  256. StringBuilder builder;
  257. for (int i = 0; i < parts.size(); ++i) {
  258. builder.append(parts[i]);
  259. if (i != parts.size() - 1)
  260. builder.append('+');
  261. }
  262. return builder.to_string();
  263. }
  264. }