Key.h
1#ifndef PT_FORMS_KEYCODE_H
2#define PT_FORMS_KEYCODE_H
3
4#include <Pt/Forms/Api.h>
5#include <Pt/Types.h>
6#include <Pt/String.h>
7#include <iostream>
8#include <cassert>
9
10namespace Pt {
11
12namespace Forms {
13
22class Key
23{
24 public:
27 // see also: https://w3c.github.io/uievents-code/
28 enum Code
29 {
30 NoKey = 0x0,
31
32 // SPECIAL KEYS ASCII
33
34 Backspace = 0x08,
35
36 Tab = 0x09,
37
38 Return = 0x0D,
39
40 Escape = 0x1B,
41
42
43 // CHARACTER KEYS (match unicode)
44
45 // Space key.
46 Space = 32,
47
48 // The 0 key.
49 D0 = 48,
50
51 // The 1 key.
52 D1 = 49,
53
54 // The 2 key.
55 D2 = 50,
56
57 // The 3 key.
58 D3 = 51,
59
60 // The 4 key.
61 D4 = 52,
62
63 // The 5 key.
64 D5 = 53,
65
66 // The 6 key.
67 D6 = 54,
68
69 // The 7 key.
70 D7 = 55,
71
72 // The 8 key.
73 D8 = 56,
74
75 // The 9 key.
76 D9 = 57,
77
78 // The A key.
79 A = 65,
80
81 // The B key.
82 B = 66,
83
84 // The C key.
85 C = 67,
86
87 // The D key.
88 D = 68,
89
90 // The E key.
91 E = 69,
92
93 // The F key.
94 F = 70,
95
96 // The G key.
97 G = 71,
98
99 // The H key.
100 H = 72,
101
102 // The I key.
103 I = 73,
104
105 // The J key.
106 J = 74,
107
108 // The K key.
109 K = 75,
110
111 // The L key.
112 L = 76,
113
114 // The M key.
115 M = 77,
116
117 // The N key.
118 N = 78,
119
120 // The O key.
121 O = 79,
122
123 // The P key.
124 P = 80,
125
126 // The Q key.
127 Q = 81,
128
129 // The R key.
130 R = 82,
131
132 // The S key.
133 S = 83,
134
135 // The T key.
136 T = 84,
137
138 // The U key.
139 U = 85,
140
141 // The V key.
142 V = 86,
143
144 // The W key.
145 W = 87,
146
147 // The X key.
148 X = 88,
149
150 // The Y key.
151 Y = 89,
152
153 // The Z key.
154 Z = 90,
155
156
157 // MODIFIERS
158
159 Unknown = 0x100000,
160 ModifiersBegin = 0x100000,
161
162 // The Shift Key.
163 ShiftKey = 0x100001, // 1
164
165 // The Control Key.
166 ControlKey = 0x100002, // 2
167
168 // The Alt Key.
169 AltKey = 0x100004, // 4
170
171 // The Meta Key.
172 MetaKey = 0x100008, // 8
173
174 // Reserved.
175 Modifier5 = 0x100010, // 16
176
177 // Reserved.
178 Modifier6 = 0x100020, // 32
179
180 // Reserved.
181 Modifier7 = 0x100040, // 64
182
183 // Reserved.
184 Modifier8 = 0x100080, // 128
185
186 // Reserved.
187 Modifier9 = 0x100100, // 256
188
189
190 // NAVIGATION KEYS 33 to 63
191
192 ArrowLeft = 0x100021,
193
194 ArrowRight = 0x100022,
195
196 ArrowUp = 0x100023,
197
198 ArrowDown = 0x100024,
199
200
201 // NUMPAD 65 to 127
202
203 // The NUM LOCK key.
204 NumLock = 0x100041,
205
206 // The 0 key on the numeric keypad.
207 NumPad0 = 0x100042,
208
209 // The 1 key on the numeric keypad.
210 NumPad1 = 0x100043,
211
212 // The 2 key on the numeric keypad.
213 NumPad2 = 0x100044,
214
215 // The 3 key on the numeric keypad.
216 NumPad3 = 0x100045,
217
218 // The 4 key on the numeric keypad.
219 NumPad4 = 0x100046,
220
221 // The 5 key on the numeric keypad.
222 NumPad5 = 0x100047,
223
224 // The 6 key on the numeric keypad.
225 NumPad6 = 0x100048,
226
227 // The 7 key on the numeric keypad.
228 NumPad7 = 0x100049,
229
230 // The 8 key on the numeric keypad.
231 NumPad8 = 0x10004A,
232
233 // The 9 key on the numeric keypad.
234 NumPad9 = 0x10004B,
235
236 // The multiply key.
237 Multiply = 0x10004C,
238
239 // The add key.
240 Add = 0x10004D,
241
242 // The divide key.
243 Divide = 0x10004E,
244
245 // The subtract key.
246 Subtract = 0x10004F,
247
248 // The separator key.
249 Separator = 0x100050,
250
251 // The decimal key.
252 Decimal = 0x100051,
253
254
255 // FUNCTION KEYS 129 to 191
256
257 // The F1 key.
258 F1 = 0x100081, // 129
259
260 // The F2 key.
261 F2 = 0x100082,
262
263 // The F3 key.
264 F3 = 0x100083,
265
266 // The F4 key.
267 F4 = 0x100084,
268
269 // The F5 key.
270 F5 = 0x100085,
271
272 // The F6 key.
273 F6 = 0x100086,
274
275 // The F7 key.
276 F7 = 0x100087,
277
278 // The F8 key.
279 F8 = 0x100088,
280
281 // The F9 key.
282 F9 = 0x100089,
283
284 // The F10 key.
285 F10 = 0x10008A,
286
287 // The F11 key.
288 F11 = 0x10008B,
289
290 // The F12 key.
291 F12 = 0x10008C,
292
293 // The F13 key.
294 F13 = 0x10008D,
295
296 // The F14 key.
297 F14 = 0x10008E,
298
299 // The F15 key.
300 F15 = 0x10008F,
301
302 // The F16 key.
303 F16 = 0x100090,
304
305 // The F17 key.
306 F17 = 0x100091,
307
308 // The F18 key.
309 F18 = 0x100092,
310
311 // The F19 key.
312 F19 = 0x100093,
313
314 // The F20 key.
315 F20 = 0x100094,
316
317 // The F21 key.
318 F21 = 0x100095,
319
320 // The F22 key.
321 F22 = 0x100096,
322
323 // The F23 key.
324 F23 = 0x100097,
325
326 // The F24 key.
327 F24 = 0x100098,
328
329
330 // SPECIAL KEYS 192 to 255
331
332 Insert = 0x1000C0, // 192
333
334 Delete = 0x1000C1,
335
336 Home = 0x1000C2,
337
338 End = 0x1000C3,
339
340 PageUp = 0x1000C4,
341
342 PageDown = 0x1000C5,
343
344 CapsLock = 0x1000C6,
345
346 PrintScreen = 0x1000C7,
347
348 SysReq = 0x1000C8,
349
350 ScrollLock = 0x1000C9,
351
352 Pause = 0x1000CA,
353
354 Break = 0x1000CB, // 204
355
356 Clear = 0x1000E0, // 224
357
358 Sleep = 0x1000E1,
359
360 Select = 0x1000E2,
361
362 Print = 0x1000E3,
363
364 Execute = 0x1000E4,
365
366 Help = 0x1000E5,
367
368 AppsMenu = 0x1000E6,
369
370 ModeChange = 0x1000E7, // 231
371
372
373 // SPECIAL APPLICATION KEYS 257 to 511
374
375 Play = 0x100101,
376
377 Zoom = 0x100102,
378
379 BrowserBack = 0x100103,
380
381 BrowserForward = 0x100104,
382
383 BrowserRefresh = 0x100105,
384
385 BrowserStop = 0x100106,
386
387 BrowserSearch = 0x100107,
388
389 BrowserFavorites = 0x100108,
390
391 BrowserHome = 0x100109,
392
393 VolumeMute = 0x10010A,
394
395 VolumeDown = 0x10010B,
396
397 VolumeUp = 0x10010C,
398
399 MediaNext = 0x10010D,
400
401 MediaPrev = 0x10010E,
402
403 MediaStop = 0x10010F,
404
405 MediaPlay = 0x100111,
406
407 LaunchMail = 0x100112,
408
409 LaunchMedia = 0x100113,
410
411 LaunchApp1 = 0x100114,
412
413 LaunchApp2 = 0x100115,
414
415 KeyMax = 0x10FFFF
416 };
417
421 {
422 // No modifier pressed.
423 NoModifier = ModifiersBegin,
424
425 // The SHIFT modifier key.
426 Shift = ShiftKey,
427
428 // The CTRL modifier key.
429 Control = ControlKey,
430
431 // The ALT modifier key.
432 Alt = AltKey,
433
434 // The Meta modifier key.
435 Meta = MetaKey,
436
437 ModifierMax = KeyMax
438 };
439
443 {
444 friend Modifiers operator|(Modifier m1, Modifier m2);
445
446 public:
450 : _value(NoModifier)
451 { }
452
455 explicit Modifiers(Modifier m)
456 : _value(m)
457 { }
458
462 {
463 _value = m;
464 return *this;
465 }
466
469 void clear()
470 {
471 _value = NoModifier;
472 }
473
476 bool empty() const
477 {
478 return _value == NoModifier;
479 }
480
483 void add(Modifier m)
484 {
485 _value |= m;
486 }
487
490 bool has(Modifier m) const
491 {
492 return (_value & m) == m;
493 }
494
497 bool has(Modifiers m) const
498 {
499 return (_value & m._value) == m._value;
500 }
501
505 {
506 return Modifiers(_value | m);
507 }
508
511 bool operator==(Modifier m) const
512 {
513 return _value == static_cast<Pt::uint32_t>(m);
514 }
515
518 bool operator==(Modifiers m) const
519 {
520 return _value == m._value;
521 }
522
525 bool operator!=(Modifier m) const
526 {
527 return _value != static_cast<Pt::uint32_t>(m);
528 }
529
532 bool operator!=(Modifiers m) const
533 {
534 return _value != m._value;
535 }
536
539 bool operator<(Modifiers m) const
540 {
541 return _value < m._value;
542 }
543
544 private:
545 explicit Modifiers(Pt::uint32_t value)
546 : _value(value)
547 { }
548
549 private:
550 Pt::uint32_t _value;
551 };
552
553 public:
557 : _code(NoKey)
558 {}
559
562 explicit Key(Pt::uint32_t c)
563 : _code(c)
564 {}
565
569 : _code(c)
570 , _modifier(m)
571 {}
572
576 : _code(c)
577 , _modifier( m )
578 {}
579
582 void clear()
583 {
584 _code = NoKey;
585 _modifier = Modifiers();
586 }
587
591 {
592 _code = c;
593 _modifier = Modifiers();
594 }
595
599 {
600 _code = c;
601 _modifier = m;
602 }
603
607 {
608 _code = c;
609 _modifier = m;
610 }
611
615 {
616 return _code;
617 }
618
622 {
623 return _modifier;
624 }
625
628 bool operator==(const Key& k) const
629 {
630 return ! (*this != k);
631 }
632
635 bool operator!=(const Key& k) const
636 {
637 return _code != k._code ||
638 _modifier != k._modifier;
639 }
640
643 bool operator<(const Key& k) const
644 {
645 return _code < k._code ||
646 (_code == k._code && _modifier < k._modifier);
647 }
648
654 {
655 if(code < ModifiersBegin)
656 return Pt::String(1, code);
657
658 switch(code)
659 {
660 case F1: return "F1";
661 case F2: return "F2";
662 case F3: return "F3";
663 case F4: return "F4";
664 case F5: return "F5";
665 case F6: return "F6";
666 case F7: return "F7";
667 case F8: return "F8";
668 case F9: return "F9";
669 case F10: return "F10";
670 case F11: return "F11";
671 case F12: return "F12";
672 case AltKey: return "Alt";
673 case ShiftKey: return "Shift";
674 case ControlKey: return "Ctrl";
675 case MetaKey: return "Meta";
676 default: return "";
677 }
678
679 return Pt::String();
680 }
681
684 bool empty() const
685 {
686 return _code == Key::NoKey;
687 }
688
689
690 private:
691 Pt::uint32_t _code;
692 Modifiers _modifier;
693};
694
698{
699 Key::Modifiers m(m1);
700 return m|m2;
701}
702
703} // namespace
704
705} // namespace
706
707#endif
Stores a set of key modifier flags.
Definition Key.h:443
bool operator==(Modifier m) const
Returns true when this set equals m.
Definition Key.h:511
Modifiers operator|(Modifier m) const
Returns this set combined with m.
Definition Key.h:504
friend Modifiers operator|(Modifier m1, Modifier m2)
Returns a modifier set containing m1 and m2.
Definition Key.h:697
bool has(Modifier m) const
Returns true when this set contains m.
Definition Key.h:490
bool empty() const
Returns true when no modifier is set.
Definition Key.h:476
bool operator<(Modifiers m) const
Returns true when this set sorts before m.
Definition Key.h:539
Modifiers operator=(Modifier m)
Replaces this set with m and returns it.
Definition Key.h:461
bool operator==(Modifiers m) const
Returns true when this set equals m.
Definition Key.h:518
void clear()
Removes all modifiers.
Definition Key.h:469
Modifiers(Modifier m)
Creates a modifier set containing m.
Definition Key.h:455
bool operator!=(Modifier m) const
Returns true when this set does not equal m.
Definition Key.h:525
bool operator!=(Modifiers m) const
Returns true when this set does not equal m.
Definition Key.h:532
bool has(Modifiers m) const
Returns true when this set contains every modifier in m.
Definition Key.h:497
void add(Modifier m)
Adds m to this set.
Definition Key.h:483
Modifiers()
Creates an empty modifier set.
Definition Key.h:449
void set(Modifier m, Pt::uint32_t c)
Sets the modifier to m and the code to c.
Definition Key.h:598
void set(Modifiers m, Pt::uint32_t c)
Sets the modifiers to m and the code to c.
Definition Key.h:606
bool operator==(const Key &k) const
Returns true when this key equals k.
Definition Key.h:628
Key(Modifiers m, Pt::uint32_t c)
Creates a key with modifiers m and code c.
Definition Key.h:575
void set(Pt::uint32_t c)
Sets the code to c and removes all modifiers.
Definition Key.h:590
bool empty() const
Returns true when the key code is NoKey.
Definition Key.h:684
bool operator!=(const Key &k) const
Returns true when this key does not equal k.
Definition Key.h:635
Pt::uint32_t code() const
Returns the key code.
Definition Key.h:614
Modifier
Defines key modifier flags.
Definition Key.h:421
Key(Modifier m, Pt::uint32_t c)
Creates a key with modifier m and code c.
Definition Key.h:568
Key()
Creates an empty key.
Definition Key.h:556
Key(Pt::uint32_t c)
Creates a key with code c and no modifiers.
Definition Key.h:562
Modifiers modifiers() const
Returns the key modifiers.
Definition Key.h:621
bool operator<(const Key &k) const
Returns true when this key sorts before k.
Definition Key.h:643
void clear()
Resets this key to NoKey with no modifiers.
Definition Key.h:582
Code
Defines key codes.
Definition Key.h:29
static Pt::String toString(Pt::uint32_t code)
Converts the key code to a string.
Definition Key.h:653
Unicode capable basic_string.
Definition Api-String.h:67
uint_type uint32_t
Unsigned 32-bit integer type.
Definition Api-Types.h:52
Graphical user interfaces.
Definition ActivateEvent.h:40
Core module.
Definition Allocator.h:33