keyboard.h 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. /**************************************************************************/
  2. /* keyboard.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #pragma once
  31. #include "core/string/ustring.h"
  32. // Keep the values in this enum in sync with `_keycodes` in `keyboard.cpp`,
  33. // and the bindings in `core_constants.cpp`.
  34. enum class Key {
  35. NONE = 0,
  36. // Special key: The strategy here is similar to the one used by toolkits,
  37. // which consists in leaving the 21 bits unicode range for printable
  38. // characters, and use the upper 11 bits for special keys and modifiers.
  39. // This way everything (char/keycode) can fit nicely in one 32-bit
  40. // integer (the enum's underlying type is `int` by default).
  41. SPECIAL = (1 << 22),
  42. /* CURSOR/FUNCTION/BROWSER/MULTIMEDIA/MISC KEYS */
  43. ESCAPE = SPECIAL | 0x01,
  44. TAB = SPECIAL | 0x02,
  45. BACKTAB = SPECIAL | 0x03,
  46. BACKSPACE = SPECIAL | 0x04,
  47. ENTER = SPECIAL | 0x05,
  48. KP_ENTER = SPECIAL | 0x06,
  49. INSERT = SPECIAL | 0x07,
  50. KEY_DELETE = SPECIAL | 0x08, // "DELETE" is a reserved word on Windows.
  51. PAUSE = SPECIAL | 0x09,
  52. PRINT = SPECIAL | 0x0A,
  53. SYSREQ = SPECIAL | 0x0B,
  54. CLEAR = SPECIAL | 0x0C,
  55. HOME = SPECIAL | 0x0D,
  56. END = SPECIAL | 0x0E,
  57. LEFT = SPECIAL | 0x0F,
  58. UP = SPECIAL | 0x10,
  59. RIGHT = SPECIAL | 0x11,
  60. DOWN = SPECIAL | 0x12,
  61. PAGEUP = SPECIAL | 0x13,
  62. PAGEDOWN = SPECIAL | 0x14,
  63. SHIFT = SPECIAL | 0x15,
  64. CTRL = SPECIAL | 0x16,
  65. META = SPECIAL | 0x17,
  66. #if defined(MACOS_ENABLED)
  67. CMD_OR_CTRL = META,
  68. #else
  69. CMD_OR_CTRL = CTRL,
  70. #endif
  71. ALT = SPECIAL | 0x18,
  72. CAPSLOCK = SPECIAL | 0x19,
  73. NUMLOCK = SPECIAL | 0x1A,
  74. SCROLLLOCK = SPECIAL | 0x1B,
  75. F1 = SPECIAL | 0x1C,
  76. F2 = SPECIAL | 0x1D,
  77. F3 = SPECIAL | 0x1E,
  78. F4 = SPECIAL | 0x1F,
  79. F5 = SPECIAL | 0x20,
  80. F6 = SPECIAL | 0x21,
  81. F7 = SPECIAL | 0x22,
  82. F8 = SPECIAL | 0x23,
  83. F9 = SPECIAL | 0x24,
  84. F10 = SPECIAL | 0x25,
  85. F11 = SPECIAL | 0x26,
  86. F12 = SPECIAL | 0x27,
  87. F13 = SPECIAL | 0x28,
  88. F14 = SPECIAL | 0x29,
  89. F15 = SPECIAL | 0x2A,
  90. F16 = SPECIAL | 0x2B,
  91. F17 = SPECIAL | 0x2C,
  92. F18 = SPECIAL | 0x2D,
  93. F19 = SPECIAL | 0x2E,
  94. F20 = SPECIAL | 0x2F,
  95. F21 = SPECIAL | 0x30,
  96. F22 = SPECIAL | 0x31,
  97. F23 = SPECIAL | 0x32,
  98. F24 = SPECIAL | 0x33,
  99. F25 = SPECIAL | 0x34,
  100. F26 = SPECIAL | 0x35,
  101. F27 = SPECIAL | 0x36,
  102. F28 = SPECIAL | 0x37,
  103. F29 = SPECIAL | 0x38,
  104. F30 = SPECIAL | 0x39,
  105. F31 = SPECIAL | 0x3A,
  106. F32 = SPECIAL | 0x3B,
  107. F33 = SPECIAL | 0x3C,
  108. F34 = SPECIAL | 0x3D,
  109. F35 = SPECIAL | 0x3E,
  110. KP_MULTIPLY = SPECIAL | 0x81,
  111. KP_DIVIDE = SPECIAL | 0x82,
  112. KP_SUBTRACT = SPECIAL | 0x83,
  113. KP_PERIOD = SPECIAL | 0x84,
  114. KP_ADD = SPECIAL | 0x85,
  115. KP_0 = SPECIAL | 0x86,
  116. KP_1 = SPECIAL | 0x87,
  117. KP_2 = SPECIAL | 0x88,
  118. KP_3 = SPECIAL | 0x89,
  119. KP_4 = SPECIAL | 0x8A,
  120. KP_5 = SPECIAL | 0x8B,
  121. KP_6 = SPECIAL | 0x8C,
  122. KP_7 = SPECIAL | 0x8D,
  123. KP_8 = SPECIAL | 0x8E,
  124. KP_9 = SPECIAL | 0x8F,
  125. MENU = SPECIAL | 0x42,
  126. HYPER = SPECIAL | 0x43,
  127. HELP = SPECIAL | 0x45,
  128. BACK = SPECIAL | 0x48,
  129. FORWARD = SPECIAL | 0x49,
  130. STOP = SPECIAL | 0x4A,
  131. REFRESH = SPECIAL | 0x4B,
  132. VOLUMEDOWN = SPECIAL | 0x4C,
  133. VOLUMEMUTE = SPECIAL | 0x4D,
  134. VOLUMEUP = SPECIAL | 0x4E,
  135. MEDIAPLAY = SPECIAL | 0x54,
  136. MEDIASTOP = SPECIAL | 0x55,
  137. MEDIAPREVIOUS = SPECIAL | 0x56,
  138. MEDIANEXT = SPECIAL | 0x57,
  139. MEDIARECORD = SPECIAL | 0x58,
  140. HOMEPAGE = SPECIAL | 0x59,
  141. FAVORITES = SPECIAL | 0x5A,
  142. SEARCH = SPECIAL | 0x5B,
  143. STANDBY = SPECIAL | 0x5C,
  144. OPENURL = SPECIAL | 0x5D,
  145. LAUNCHMAIL = SPECIAL | 0x5E,
  146. LAUNCHMEDIA = SPECIAL | 0x5F,
  147. LAUNCH0 = SPECIAL | 0x60,
  148. LAUNCH1 = SPECIAL | 0x61,
  149. LAUNCH2 = SPECIAL | 0x62,
  150. LAUNCH3 = SPECIAL | 0x63,
  151. LAUNCH4 = SPECIAL | 0x64,
  152. LAUNCH5 = SPECIAL | 0x65,
  153. LAUNCH6 = SPECIAL | 0x66,
  154. LAUNCH7 = SPECIAL | 0x67,
  155. LAUNCH8 = SPECIAL | 0x68,
  156. LAUNCH9 = SPECIAL | 0x69,
  157. LAUNCHA = SPECIAL | 0x6A,
  158. LAUNCHB = SPECIAL | 0x6B,
  159. LAUNCHC = SPECIAL | 0x6C,
  160. LAUNCHD = SPECIAL | 0x6D,
  161. LAUNCHE = SPECIAL | 0x6E,
  162. LAUNCHF = SPECIAL | 0x6F,
  163. GLOBE = SPECIAL | 0x70,
  164. KEYBOARD = SPECIAL | 0x71,
  165. JIS_EISU = SPECIAL | 0x72,
  166. JIS_KANA = SPECIAL | 0x73,
  167. UNKNOWN = SPECIAL | 0x7FFFFF,
  168. /* PRINTABLE LATIN 1 CODES */
  169. SPACE = 0x0020,
  170. EXCLAM = 0x0021,
  171. QUOTEDBL = 0x0022,
  172. NUMBERSIGN = 0x0023,
  173. DOLLAR = 0x0024,
  174. PERCENT = 0x0025,
  175. AMPERSAND = 0x0026,
  176. APOSTROPHE = 0x0027,
  177. PARENLEFT = 0x0028,
  178. PARENRIGHT = 0x0029,
  179. ASTERISK = 0x002A,
  180. PLUS = 0x002B,
  181. COMMA = 0x002C,
  182. MINUS = 0x002D,
  183. PERIOD = 0x002E,
  184. SLASH = 0x002F,
  185. KEY_0 = 0x0030,
  186. KEY_1 = 0x0031,
  187. KEY_2 = 0x0032,
  188. KEY_3 = 0x0033,
  189. KEY_4 = 0x0034,
  190. KEY_5 = 0x0035,
  191. KEY_6 = 0x0036,
  192. KEY_7 = 0x0037,
  193. KEY_8 = 0x0038,
  194. KEY_9 = 0x0039,
  195. COLON = 0x003A,
  196. SEMICOLON = 0x003B,
  197. LESS = 0x003C,
  198. EQUAL = 0x003D,
  199. GREATER = 0x003E,
  200. QUESTION = 0x003F,
  201. AT = 0x0040,
  202. A = 0x0041,
  203. B = 0x0042,
  204. C = 0x0043,
  205. D = 0x0044,
  206. E = 0x0045,
  207. F = 0x0046,
  208. G = 0x0047,
  209. H = 0x0048,
  210. I = 0x0049,
  211. J = 0x004A,
  212. K = 0x004B,
  213. L = 0x004C,
  214. M = 0x004D,
  215. N = 0x004E,
  216. O = 0x004F,
  217. P = 0x0050,
  218. Q = 0x0051,
  219. R = 0x0052,
  220. S = 0x0053,
  221. T = 0x0054,
  222. U = 0x0055,
  223. V = 0x0056,
  224. W = 0x0057,
  225. X = 0x0058,
  226. Y = 0x0059,
  227. Z = 0x005A,
  228. BRACKETLEFT = 0x005B,
  229. BACKSLASH = 0x005C,
  230. BRACKETRIGHT = 0x005D,
  231. ASCIICIRCUM = 0x005E,
  232. UNDERSCORE = 0x005F,
  233. QUOTELEFT = 0x0060,
  234. BRACELEFT = 0x007B,
  235. BAR = 0x007C,
  236. BRACERIGHT = 0x007D,
  237. ASCIITILDE = 0x007E,
  238. YEN = 0x00A5,
  239. SECTION = 0x00A7,
  240. };
  241. enum class KeyModifierMask {
  242. CODE_MASK = ((1 << 23) - 1), ///< Apply this mask to any keycode to remove modifiers.
  243. MODIFIER_MASK = (0x7F << 24), ///< Apply this mask to isolate modifiers.
  244. //RESERVED = (1 << 23),
  245. CMD_OR_CTRL = (1 << 24),
  246. SHIFT = (1 << 25),
  247. ALT = (1 << 26),
  248. META = (1 << 27),
  249. CTRL = (1 << 28),
  250. KPAD = (1 << 29),
  251. GROUP_SWITCH = (1 << 30)
  252. };
  253. enum class KeyLocation {
  254. UNSPECIFIED,
  255. LEFT,
  256. RIGHT
  257. };
  258. // To avoid having unnecessary operators, only define the ones that are needed.
  259. constexpr Key operator-(uint32_t a, Key b) {
  260. return (Key)(a - (uint32_t)b);
  261. }
  262. constexpr Key &operator-=(Key &a, int b) {
  263. a = static_cast<Key>(static_cast<int>(a) - static_cast<int>(b));
  264. return a;
  265. }
  266. constexpr Key operator+(Key a, int b) {
  267. return (Key)((int)a + (int)b);
  268. }
  269. constexpr Key operator+(Key a, Key b) {
  270. return (Key)((int)a + (int)b);
  271. }
  272. constexpr Key operator-(Key a, Key b) {
  273. return (Key)((int)a - (int)b);
  274. }
  275. constexpr Key operator&(Key a, Key b) {
  276. return (Key)((int)a & (int)b);
  277. }
  278. constexpr Key operator|(Key a, Key b) {
  279. return (Key)((int)a | (int)b);
  280. }
  281. constexpr Key &operator|=(Key &a, Key b) {
  282. a = static_cast<Key>(static_cast<int>(a) | static_cast<int>(b));
  283. return a;
  284. }
  285. constexpr Key &operator|=(Key &a, KeyModifierMask b) {
  286. a = static_cast<Key>(static_cast<int>(a) | static_cast<int>(b));
  287. return a;
  288. }
  289. constexpr Key &operator&=(Key &a, KeyModifierMask b) {
  290. a = static_cast<Key>(static_cast<int>(a) & static_cast<int>(b));
  291. return a;
  292. }
  293. constexpr Key operator|(Key a, KeyModifierMask b) {
  294. return (Key)((int)a | (int)b);
  295. }
  296. constexpr Key operator&(Key a, KeyModifierMask b) {
  297. return (Key)((int)a & (int)b);
  298. }
  299. constexpr Key operator+(KeyModifierMask a, Key b) {
  300. return (Key)((int)a + (int)b);
  301. }
  302. constexpr Key operator|(KeyModifierMask a, Key b) {
  303. return (Key)((int)a | (int)b);
  304. }
  305. constexpr KeyModifierMask operator+(KeyModifierMask a, KeyModifierMask b) {
  306. return (KeyModifierMask)((int)a + (int)b);
  307. }
  308. constexpr KeyModifierMask operator|(KeyModifierMask a, KeyModifierMask b) {
  309. return (KeyModifierMask)((int)a | (int)b);
  310. }
  311. String keycode_get_string(Key p_code);
  312. bool keycode_has_unicode(Key p_keycode);
  313. Key find_keycode(const String &p_codestr);
  314. const char *find_keycode_name(Key p_keycode);
  315. int keycode_get_count();
  316. int keycode_get_value_by_index(int p_index);
  317. const char *keycode_get_name_by_index(int p_index);
  318. char32_t fix_unicode(char32_t p_char);
  319. Key fix_keycode(char32_t p_char, Key p_key);
  320. Key fix_key_label(char32_t p_char, Key p_key);