utility 2026.1.9
A comprehensive C++ utilities library tailored for the development of modern desktop and extended reality (XR) applications.
Loading...
Searching...
No Matches
keyboard_event.hpp
1/*
2 Copyright (c) 2026 ETIB Corporation
3
4 Permission is hereby granted, free of charge, to any person obtaining a copy of
5 this software and associated documentation files (the "Software"), to deal in
6 the Software without restriction, including without limitation the rights to
7 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
8 of the Software, and to permit persons to whom the Software is furnished to do
9 so, subject to the following conditions:
10
11 The above copyright notice and this permission notice shall be included in all
12 copies or substantial portions of the Software.
13
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 SOFTWARE.
21 */
22
23#pragma once
24
25#include <bitset>
26#include <cstdint>
27#include <memory>
28
29#include "utility/event/event.hpp"
30
31namespace utility::event
32{
33
39 class KeyboardEvent: public Event
40 {
41 public:
45 enum class ScanCode : std::uint16_t {
46 Unknown = 0,
48 A = 4,
49 B = 5,
50 C = 6,
51 D = 7,
52 E = 8,
53 F = 9,
54 G = 10,
55 H = 11,
56 I = 12,
57 J = 13,
58 K = 14,
59 L = 15,
60 M = 16,
61 N = 17,
62 O = 18,
63 P = 19,
64 Q = 20,
65 R = 21,
66 S = 22,
67 T = 23,
68 U = 24,
69 V = 25,
70 W = 26,
71 X = 27,
72 Y = 28,
73 Z = 29,
75 Num1 = 30,
76 Num2 = 31,
77 Num3 = 32,
78 Num4 = 33,
79 Num5 = 34,
80 Num6 = 35,
81 Num7 = 36,
82 Num8 = 37,
83 Num9 = 38,
84 Num0 = 39,
86 Return = 40,
87 Escape = 41,
88 Backspace = 42,
89 Tab = 43,
90 Space = 44,
92 Minus = 45,
93 Equals = 46,
94 LeftBracket = 47,
95 RightBracket = 48,
96 Backslash =
97 49,
107 NonUsHash = 50,
111 Semicolon = 51,
112 Apostrophe = 52,
113 Grave = 53,
131 Comma = 54,
132 Period = 55,
133 Slash = 56,
135 CapsLock = 57,
137 F1 = 58,
138 F2 = 59,
139 F3 = 60,
140 F4 = 61,
141 F5 = 62,
142 F6 = 63,
143 F7 = 64,
144 F8 = 65,
145 F9 = 66,
146 F10 = 67,
147 F11 = 68,
148 F12 = 69,
150 PrintScreen = 70,
151 ScrollLock = 71,
152 Pause = 72,
153 Insert = 73,
154 Home = 74,
155 PageUp = 75,
156 Delete = 76,
157 End = 77,
158 PageDown = 78,
159 Right = 79,
160 Left = 80,
161 Down = 81,
162 Up = 82,
164 NumLockClear = 83,
165 KeyPadDivide = 84,
166 KeyPadMultiply = 85,
167 KeyPadMinus = 86,
168 KeyPadPlus = 87,
169 KeyPadEnter = 88,
170 KeyPad1 = 89,
171 KeyPad2 = 90,
172 KeyPad3 = 91,
173 KeyPad4 = 92,
174 KeyPad5 = 93,
175 KeyPad6 = 94,
176 KeyPad7 = 95,
177 KeyPad8 = 96,
178 KeyPad9 = 97,
179 KeyPad0 = 98,
180 KeyPadPeriod = 99,
183 100,
190 Application = 101,
191 Power = 102,
192 KeyPadEquals = 103,
193 F13 = 104,
194 F14 = 105,
195 F15 = 106,
196 F16 = 107,
197 F17 = 108,
198 F18 = 109,
199 F19 = 110,
200 F20 = 111,
201 F21 = 112,
202 F22 = 113,
203 F23 = 114,
204 F24 = 115,
205 Execute = 116,
206 Help = 117,
207 Menu = 118,
208 Select = 119,
209 Stop = 120,
210 Again = 121,
211 Undo = 122,
212 Cut = 123,
213 Copy = 124,
214 Paste = 125,
215 Find = 126,
216 Mute = 127,
217 VolumeUp = 128,
218 VolumeDown = 129,
219 KeyPadComma = 133,
220 KeyPadEqualsAs400 = 134,
222 International1 = 135,
223 International2 = 136,
224 International3 = 137,
225 International4 = 138,
226 International5 = 139,
227 International6 = 140,
228 International7 = 141,
229 International8 = 142,
230 International9 = 143,
231 Lang1 = 144,
232 Lang2 = 145,
233 Lang3 = 146,
234 Lang4 = 147,
235 Lang5 = 148,
236 Lang6 = 149,
237 Lang7 = 150,
238 Lang8 = 151,
239 Lang9 = 152,
241 Alterase = 153,
242 Sysreq = 154,
243 Cancel = 155,
244 Clear = 156,
245 Prior = 157,
246 Return2 = 158,
247 Separator = 159,
248 Out = 160,
249 Oper = 161,
250 ClearAgain = 162,
251 Crsel = 163,
252 Exsel = 164,
254 KeyPad00 = 176,
255 KeyPad000 = 177,
256 ThousandsSeparator = 178,
257 DecimalSeparator = 179,
258 CurrencyUnit = 180,
259 CurrencySubunit = 181,
260 KeyPadLeftParen = 182,
261 KeyPadRightParen = 183,
262 KeyPadLeftBrace = 184,
263 KeyPadRightBrace = 185,
264 KeyPadTab = 186,
265 KeyPadBackspace = 187,
266 KeyPadA = 188,
267 KeyPadB = 189,
268 KeyPadC = 190,
269 KeyPadD = 191,
270 KeyPadE = 192,
271 KeyPadF = 193,
272 KeyPadXor = 194,
273 KeyPadPower = 195,
274 KeyPadPercent = 196,
275 KeyPadLess = 197,
276 KeyPadGreater = 198,
277 KeyPadAmpersand = 199,
279 KeyPadVerticalbar = 201,
281 202,
282 KeyPadColon = 203,
283 KeyPadHash = 204,
284 KeyPadSpace = 205,
285 KeyPadAt = 206,
286 KeyPadExclam = 207,
287 KeyPadMemoryStore = 208,
288 KeyPadMemoryRecall = 209,
289 KeyPadMemoryClear = 210,
290 KeyPadMemoryAdd = 211,
293 KeyPadMemoryDivide = 214,
294 KeyPadPlusMinus = 215,
295 KeyPadClear = 216,
296 KeyPadClearEntry = 217,
297 KeyPadBinary = 218,
298 KeyPadOctal = 219,
299 KeyPadDecimal = 220,
300 KeyPadHexadecimal = 221,
302 LeftControl = 224,
303 LeftShift = 225,
304 LeftAlt = 226,
305 LeftGui = 227,
306 RightControl = 228,
307 RightShift = 229,
308 RightAlt = 230,
309 RightGui = 231,
311 Mode = 257,
313 /* Usage page 0x0C */
314 Sleep = 258,
315 Wake = 259,
317 ChannelIncrement = 260,
318 ChannelDecrement = 261,
320 MediaPlay = 262,
321 MediaPause = 263,
322 MediaRecord = 264,
323 MediaFastForward = 265,
324 MediaRewind = 266,
325 MediaNextTrack = 267,
326 MediaPreviousTrack = 268,
327 MediaStop = 269,
328 MediaEject = 270,
329 MediaPlayPause = 271,
330 MediaSelect = 272,
332 AcNew = 273,
333 AcOpen = 274,
334 AcClose = 275,
335 AcExit = 276,
336 AcSave = 277,
337 AcPrint = 278,
338 AcProperties = 279,
340 AcSearch = 280,
341 AcHome = 281,
342 AcBack = 282,
343 AcForward = 283,
344 AcStop = 284,
345 AcRefresh = 285,
346 AcBookmarks = 286,
348 /* Mobile keys */
349 SoftLeft = 287,
350 SoftRight = 288,
351 Call = 289,
352 EndCall = 290,
354 Reserved = 400,
356 Count = 512
359 };
360
367 enum class KeyCode : std::int32_t {
368 Unknown = -0x00000001,
369 Space = 0x00000020u,
370 Apostrophe = 0x00000027u,
371 Comma = 0x0000002Cu,
372 Minus = 0x0000002Du,
373 Period = 0x0000002Eu,
374 Slash = 0x0000002Fu,
375 Num0 = 0x00000030u,
376 Num1 = 0x00000031u,
377 Num2 = 0x00000032u,
378 Num3 = 0x00000033u,
379 Num4 = 0x00000034u,
380 Num5 = 0x00000035u,
381 Num6 = 0x00000036u,
382 Num7 = 0x00000037u,
383 Num8 = 0x00000038u,
384 Num9 = 0x00000039u,
385 Semicolon = 0x0000003Bu,
386 Equals = 0x0000003Du,
387 A = 0x00000041u,
388 B = 0x00000042u,
389 C = 0x00000043u,
390 D = 0x00000044u,
391 E = 0x00000045u,
392 F = 0x00000046u,
393 G = 0x00000047u,
394 H = 0x00000048u,
395 I = 0x00000049u,
396 J = 0x0000004Au,
397 K = 0x0000004Bu,
398 L = 0x0000004Cu,
399 M = 0x0000004Du,
400 N = 0x0000004Eu,
401 O = 0x0000004Fu,
402 P = 0x00000050u,
403 Q = 0x00000051u,
404 R = 0x00000052u,
405 S = 0x00000053u,
406 T = 0x00000054u,
407 U = 0x00000055u,
408 V = 0x00000056u,
409 W = 0x00000057u,
410 X = 0x00000058u,
411 Y = 0x00000059u,
412 Z = 0x0000005Au,
413 LeftBracket = 0x0000005Bu,
414 Backslash = 0x0000005Cu,
415 RightBracket = 0x0000005Du,
416 Grave = 0x00000060u,
417 Escape = 0x00000100u,
418 Enter = 0x00000101u,
419 Tab = 0x00000102u,
420 Backspace = 0x00000103u,
421 Insert = 0x00000104u,
422 Delete = 0x00000105u,
423 Right = 0x00000106u,
424 Left = 0x00000107u,
425 Down = 0x00000108u,
426 Up = 0x00000109u,
427 PageUp = 0x0000010Au,
428 PageDown = 0x0000010Bu,
429 Home = 0x0000010Cu,
430 End = 0x0000010Du,
431 CapsLock = 0x00000118u,
432 ScrollLock = 0x00000119u,
433 NumLockClear = 0x0000011Au,
434 PrintScreen = 0x0000011Bu,
435 Pause = 0x0000011Cu,
436 F1 = 0x00000122u,
437 F2 = 0x00000123u,
438 F3 = 0x00000124u,
439 F4 = 0x00000125u,
440 F5 = 0x00000126u,
441 F6 = 0x00000127u,
442 F7 = 0x00000128u,
443 F8 = 0x00000129u,
444 F9 = 0x0000012Au,
445 F10 = 0x0000012Bu,
446 F11 = 0x0000012Cu,
447 F12 = 0x0000012Du,
448 F13 = 0x0000012Eu,
449 F14 = 0x0000012Fu,
450 F15 = 0x00000130u,
451 F16 = 0x00000131u,
452 F17 = 0x00000132u,
453 F18 = 0x00000133u,
454 F19 = 0x00000134u,
455 F20 = 0x00000135u,
456 F21 = 0x00000136u,
457 F22 = 0x00000137u,
458 F23 = 0x00000138u,
459 F24 = 0x00000139u,
460 KeyPad0 = 0x00000140u,
461 KeyPad1 = 0x00000141u,
462 KeyPad2 = 0x00000142u,
463 KeyPad3 = 0x00000143u,
464 KeyPad4 = 0x00000144u,
465 KeyPad5 = 0x00000145u,
466 KeyPad6 = 0x00000146u,
467 KeyPad7 = 0x00000147u,
468 KeyPad8 = 0x00000148u,
469 KeyPad9 = 0x00000149u,
470 KeyPadPeriod = 0x0000014Au,
471 KeyPadDivide = 0x0000014Bu,
472 KeyPadMultiply = 0x0000014Cu,
473 KeyPadMinus = 0x0000014Du,
474 KeyPadPlus = 0x0000014Eu,
475 KeyPadEnter = 0x0000014Fu,
476 KeyPadEquals = 0x00000150u,
477 LeftShift = 0x00000154u,
478 LeftControl = 0x00000155u,
479 LeftAlt = 0x00000156u,
480 LeftGui = 0x00000157u,
481 RightShift = 0x00000158u,
482 RightControl = 0x00000159u,
483 RightAlt = 0x0000015Au,
484 RightGui = 0x0000015Bu,
485 };
486
493 enum class KeyModifiers : std::uint16_t {
494 Unknown = 0x0000u,
495 LeftShift = 0x0001u,
496 RightShift = 0x0002u,
497 Level5Shift = 0x0004u,
498 LeftCtrl = 0x0040u,
499 RightCtrl = 0x0080u,
500 LeftAlt = 0x0100u,
501 RightAlt = 0x0200u,
502 LeftGUI = 0x0400u,
504 RightGUI = 0x0800u,
506 NumLock = 0x1000u,
508 CapsLock = 0x2000u,
509 AltGr = 0x4000u,
510 ScrollLock = 0x8000u,
514 Alt = LeftAlt | RightAlt,
515 Gui = LeftGUI | RightGUI
516 };
517
522 {
523 public:
524 ~Factory(void) override;
525
530 std::shared_ptr<Event> create(void) const override;
531
537 std::shared_ptr<KeyboardEvent> createTyped(void) const;
538 };
539
540 private:
541 ScanCode _scancode { ScanCode::Unknown };
542 KeyCode _keycode { KeyCode::Unknown };
543 std::bitset<sizeof(KeyModifiers) * 8> _modifiers {
544 static_cast<std::size_t>(KeyModifiers::Unknown)
545 };
546 bool isDownEvent { true };
547 bool isRepeatEvent { false };
548
549 public:
553 explicit KeyboardEvent(void);
554
558 ~KeyboardEvent(void) override;
559
565 ScanCode getScancode(void) const noexcept;
566
572 KeyboardEvent &setScancode(const ScanCode scancode) noexcept;
573
579 KeyboardEvent &setKeycode(const KeyCode keycode) noexcept;
580
585 KeyCode getKeycode(void) const noexcept;
586
592 KeyboardEvent &setModifiers(const KeyModifiers modifiers) noexcept;
593
599 bool isModifierSet(const KeyModifiers modifier) const noexcept;
600
606 KeyboardEvent &setIsDownEvent(const bool isDown) noexcept;
607
612 bool getIsDownEvent(void) const noexcept;
613
620 KeyboardEvent &setIsRepeatEvent(const bool isRepeat) noexcept;
621
626 bool getIsRepeatEvent(void) const noexcept;
627 };
628
629} // namespace utility::event
Abstract factory interface for creating events.
Definition event.hpp:44
Base class for events in the event system.
Definition event.hpp:38
Factory for creating KeyboardEvent instances.
std::shared_ptr< KeyboardEvent > createTyped(void) const
Create a strongly-typed KeyboardEvent.
std::shared_ptr< Event > create(void) const override
Create a KeyboardEvent as a base Event pointer.
Keyboard event representation.
bool isModifierSet(const KeyModifiers modifier) const noexcept
Check if a specific key modifier is set.
KeyboardEvent & setIsRepeatEvent(const bool isRepeat) noexcept
Set whether the event is a key repeat event.
KeyboardEvent & setModifiers(const KeyModifiers modifiers) noexcept
Set the key modifiers.
KeyboardEvent & setScancode(const ScanCode scancode) noexcept
Set the keyboard scancode.
bool getIsDownEvent(void) const noexcept
Check if the event is a key down event.
KeyboardEvent(void)
Default constructor.
bool getIsRepeatEvent(void) const noexcept
Check if the event is a key repeat event.
KeyCode
Virtual key codes derived from keyboard scancodes.
KeyboardEvent & setIsDownEvent(const bool isDown) noexcept
Set whether the event is a key down event.
ScanCode getScancode(void) const noexcept
Get the keyboard scancode.
KeyboardEvent & setKeycode(const KeyCode keycode) noexcept
Set the virtual key code.
~KeyboardEvent(void) override
Default destructor.
KeyCode getKeycode(void) const noexcept
Get the virtual key code.