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