keyboard.h 9.5 KB

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