GShortcut.cpp 6.2 KB

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