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