1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
|
/*
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/Types.h>
#define ENUMERATE_KEY_CODES \
__ENUMERATE_KEY_CODE(Invalid, "Invalid") \
__ENUMERATE_KEY_CODE(Escape, "Escape") \
__ENUMERATE_KEY_CODE(Tab, "Tab") \
__ENUMERATE_KEY_CODE(Backspace, "Backspace") \
__ENUMERATE_KEY_CODE(Return, "Return") \
__ENUMERATE_KEY_CODE(Insert, "Insert") \
__ENUMERATE_KEY_CODE(Delete, "Delete") \
__ENUMERATE_KEY_CODE(PrintScreen, "PrintScreen") \
__ENUMERATE_KEY_CODE(SysRq, "SysRq") \
__ENUMERATE_KEY_CODE(Home, "Home") \
__ENUMERATE_KEY_CODE(End, "End") \
__ENUMERATE_KEY_CODE(Left, "Left") \
__ENUMERATE_KEY_CODE(Up, "Up") \
__ENUMERATE_KEY_CODE(Right, "Right") \
__ENUMERATE_KEY_CODE(Down, "Down") \
__ENUMERATE_KEY_CODE(PageUp, "PageUp") \
__ENUMERATE_KEY_CODE(PageDown, "PageDown") \
__ENUMERATE_KEY_CODE(LeftShift, "LeftShift") \
__ENUMERATE_KEY_CODE(RightShift, "RightShift") \
__ENUMERATE_KEY_CODE(Control, "Ctrl") \
__ENUMERATE_KEY_CODE(Alt, "Alt") \
__ENUMERATE_KEY_CODE(CapsLock, "CapsLock") \
__ENUMERATE_KEY_CODE(NumLock, "NumLock") \
__ENUMERATE_KEY_CODE(ScrollLock, "ScrollLock") \
__ENUMERATE_KEY_CODE(F1, "F1") \
__ENUMERATE_KEY_CODE(F2, "F2") \
__ENUMERATE_KEY_CODE(F3, "F3") \
__ENUMERATE_KEY_CODE(F4, "F4") \
__ENUMERATE_KEY_CODE(F5, "F5") \
__ENUMERATE_KEY_CODE(F6, "F6") \
__ENUMERATE_KEY_CODE(F7, "F7") \
__ENUMERATE_KEY_CODE(F8, "F8") \
__ENUMERATE_KEY_CODE(F9, "F9") \
__ENUMERATE_KEY_CODE(F10, "F10") \
__ENUMERATE_KEY_CODE(F11, "F11") \
__ENUMERATE_KEY_CODE(F12, "F12") \
__ENUMERATE_KEY_CODE(Space, "Space") \
__ENUMERATE_KEY_CODE(ExclamationPoint, "!") \
__ENUMERATE_KEY_CODE(DoubleQuote, "\"") \
__ENUMERATE_KEY_CODE(Hashtag, "#") \
__ENUMERATE_KEY_CODE(Dollar, "$") \
__ENUMERATE_KEY_CODE(Percent, "%") \
__ENUMERATE_KEY_CODE(Ampersand, "&") \
__ENUMERATE_KEY_CODE(Apostrophe, "'") \
__ENUMERATE_KEY_CODE(LeftParen, "(") \
__ENUMERATE_KEY_CODE(RightParen, ")") \
__ENUMERATE_KEY_CODE(Asterisk, "*") \
__ENUMERATE_KEY_CODE(Plus, "+") \
__ENUMERATE_KEY_CODE(Comma, ",") \
__ENUMERATE_KEY_CODE(Minus, "-") \
__ENUMERATE_KEY_CODE(Period, ".") \
__ENUMERATE_KEY_CODE(Slash, "/") \
__ENUMERATE_KEY_CODE(0, "0") \
__ENUMERATE_KEY_CODE(1, "1") \
__ENUMERATE_KEY_CODE(2, "2") \
__ENUMERATE_KEY_CODE(3, "3") \
__ENUMERATE_KEY_CODE(4, "4") \
__ENUMERATE_KEY_CODE(5, "5") \
__ENUMERATE_KEY_CODE(6, "6") \
__ENUMERATE_KEY_CODE(7, "7") \
__ENUMERATE_KEY_CODE(8, "8") \
__ENUMERATE_KEY_CODE(9, "9") \
__ENUMERATE_KEY_CODE(Colon, ":") \
__ENUMERATE_KEY_CODE(Semicolon, ";") \
__ENUMERATE_KEY_CODE(LessThan, "<") \
__ENUMERATE_KEY_CODE(Equal, "=") \
__ENUMERATE_KEY_CODE(GreaterThan, ">") \
__ENUMERATE_KEY_CODE(QuestionMark, "?") \
__ENUMERATE_KEY_CODE(AtSign, "@") \
__ENUMERATE_KEY_CODE(A, "A") \
__ENUMERATE_KEY_CODE(B, "B") \
__ENUMERATE_KEY_CODE(C, "C") \
__ENUMERATE_KEY_CODE(D, "D") \
__ENUMERATE_KEY_CODE(E, "E") \
__ENUMERATE_KEY_CODE(F, "F") \
__ENUMERATE_KEY_CODE(G, "G") \
__ENUMERATE_KEY_CODE(H, "H") \
__ENUMERATE_KEY_CODE(I, "I") \
__ENUMERATE_KEY_CODE(J, "J") \
__ENUMERATE_KEY_CODE(K, "K") \
__ENUMERATE_KEY_CODE(L, "L") \
__ENUMERATE_KEY_CODE(M, "M") \
__ENUMERATE_KEY_CODE(N, "N") \
__ENUMERATE_KEY_CODE(O, "O") \
__ENUMERATE_KEY_CODE(P, "P") \
__ENUMERATE_KEY_CODE(Q, "Q") \
__ENUMERATE_KEY_CODE(R, "R") \
__ENUMERATE_KEY_CODE(S, "S") \
__ENUMERATE_KEY_CODE(T, "T") \
__ENUMERATE_KEY_CODE(U, "U") \
__ENUMERATE_KEY_CODE(V, "V") \
__ENUMERATE_KEY_CODE(W, "W") \
__ENUMERATE_KEY_CODE(X, "X") \
__ENUMERATE_KEY_CODE(Y, "Y") \
__ENUMERATE_KEY_CODE(Z, "Z") \
__ENUMERATE_KEY_CODE(LeftBracket, "[") \
__ENUMERATE_KEY_CODE(RightBracket, "]") \
__ENUMERATE_KEY_CODE(Backslash, "\\") \
__ENUMERATE_KEY_CODE(Circumflex, "^") \
__ENUMERATE_KEY_CODE(Underscore, "_") \
__ENUMERATE_KEY_CODE(LeftBrace, "{") \
__ENUMERATE_KEY_CODE(RightBrace, "}") \
__ENUMERATE_KEY_CODE(Pipe, "|") \
__ENUMERATE_KEY_CODE(Tilde, "~") \
__ENUMERATE_KEY_CODE(Backtick, "`") \
__ENUMERATE_KEY_CODE(Super, "Super") \
__ENUMERATE_KEY_CODE(Menu, "Menu")
enum KeyCode : u8 {
#define __ENUMERATE_KEY_CODE(name, ui_name) Key_##name,
ENUMERATE_KEY_CODES
#undef __ENUMERATE_KEY_CODE
Key_Shift
= Key_LeftShift,
};
const int key_code_count = Key_Super;
enum KeyModifier {
Mod_None = 0x00,
Mod_Alt = 0x01,
Mod_Ctrl = 0x02,
Mod_Shift = 0x04,
Mod_Super = 0x08,
Mod_AltGr = 0x10,
Mod_Mask = 0x1f,
Is_Press = 0x80,
};
struct KeyEvent {
KeyCode key { Key_Invalid };
u32 scancode { 0 };
u32 code_point { 0 };
u8 flags { 0 };
bool caps_lock_on { false };
bool e0_prefix { false };
bool alt() const { return flags & Mod_Alt; }
bool ctrl() const { return flags & Mod_Ctrl; }
bool shift() const { return flags & Mod_Shift; }
bool super() const { return flags & Mod_Super; }
bool altgr() const { return flags & Mod_AltGr; }
unsigned modifiers() const { return flags & Mod_Mask; }
bool is_press() const { return flags & Is_Press; }
};
inline const char* key_code_to_string(KeyCode key)
{
switch (key) {
#define __ENUMERATE_KEY_CODE(name, ui_name) \
case Key_##name: \
return ui_name;
ENUMERATE_KEY_CODES
#undef __ENUMERATE_KEY_CODE
default:
return nullptr;
}
}
inline KeyCode code_point_to_key_code(u32 code_point)
{
switch (code_point) {
#define MATCH_ALPHA(letter) \
case #letter[0]: \
case #letter[0] + 32: \
return KeyCode::Key_##letter;
MATCH_ALPHA(A)
MATCH_ALPHA(B)
MATCH_ALPHA(C)
MATCH_ALPHA(D)
MATCH_ALPHA(E)
MATCH_ALPHA(F)
MATCH_ALPHA(G)
MATCH_ALPHA(H)
MATCH_ALPHA(I)
MATCH_ALPHA(J)
MATCH_ALPHA(K)
MATCH_ALPHA(L)
MATCH_ALPHA(M)
MATCH_ALPHA(N)
MATCH_ALPHA(O)
MATCH_ALPHA(P)
MATCH_ALPHA(Q)
MATCH_ALPHA(R)
MATCH_ALPHA(S)
MATCH_ALPHA(T)
MATCH_ALPHA(U)
MATCH_ALPHA(V)
MATCH_ALPHA(W)
MATCH_ALPHA(X)
MATCH_ALPHA(Y)
MATCH_ALPHA(Z)
#undef MATCH_ALPHA
#define MATCH_KEY(name, character) \
case character: \
return KeyCode::Key_##name;
MATCH_KEY(ExclamationPoint, '!')
MATCH_KEY(DoubleQuote, '"')
MATCH_KEY(Hashtag, '#')
MATCH_KEY(Dollar, '$')
MATCH_KEY(Percent, '%')
MATCH_KEY(Ampersand, '&')
MATCH_KEY(Apostrophe, '\'')
MATCH_KEY(LeftParen, '(')
MATCH_KEY(RightParen, ')')
MATCH_KEY(Asterisk, '*')
MATCH_KEY(Plus, '+')
MATCH_KEY(Comma, ',')
MATCH_KEY(Minus, '-')
MATCH_KEY(Period, '.')
MATCH_KEY(Slash, '/')
MATCH_KEY(0, '0')
MATCH_KEY(1, '1')
MATCH_KEY(2, '2')
MATCH_KEY(3, '3')
MATCH_KEY(4, '4')
MATCH_KEY(5, '5')
MATCH_KEY(6, '6')
MATCH_KEY(7, '7')
MATCH_KEY(8, '8')
MATCH_KEY(9, '9')
MATCH_KEY(Colon, ':')
MATCH_KEY(Semicolon, ';')
MATCH_KEY(LessThan, '<')
MATCH_KEY(Equal, '=')
MATCH_KEY(GreaterThan, '>')
MATCH_KEY(QuestionMark, '?')
MATCH_KEY(AtSign, '@')
MATCH_KEY(LeftBracket, '[')
MATCH_KEY(RightBracket, ']')
MATCH_KEY(Backslash, '\\')
MATCH_KEY(Circumflex, '^')
MATCH_KEY(Underscore, '_')
MATCH_KEY(LeftBrace, '{')
MATCH_KEY(RightBrace, '}')
MATCH_KEY(Pipe, '|')
MATCH_KEY(Tilde, '~')
MATCH_KEY(Backtick, '`')
MATCH_KEY(Space, ' ')
MATCH_KEY(Tab, '\t')
MATCH_KEY(Backspace, '\b')
#undef MATCH_KEY
default:
return KeyCode::Key_Invalid;
}
}
|