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