Shortcut.cpp 6.3 KB

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