Gamedev Framework (gf)  0.3.0
A C++11 framework for 2D games
Keyboard.h
1 /*
2  * Gamedev Framework (gf)
3  * Copyright (C) 2016 Julien Bernard
4  *
5  * This software is provided 'as-is', without any express or implied
6  * warranty. In no event will the authors be held liable for any damages
7  * arising from the use of this software.
8  *
9  * Permission is granted to anyone to use this software for any purpose,
10  * including commercial applications, and to alter it and redistribute it
11  * freely, subject to the following restrictions:
12  *
13  * 1. The origin of this software must not be misrepresented; you must not
14  * claim that you wrote the original software. If you use this software
15  * in a product, an acknowledgment in the product documentation would be
16  * appreciated but is not required.
17  * 2. Altered source versions must be plainly marked as such, and must not be
18  * misrepresented as being the original software.
19  * 3. This notice may not be removed or altered from any source distribution.
20  */
21 #ifndef GF_KEYBOARD_H
22 #define GF_KEYBOARD_H
23 
24 #include "Flags.h"
25 #include "Portability.h"
26 
27 namespace gf {
28 #ifndef DOXYGEN_SHOULD_SKIP_THIS
29 inline namespace v1 {
30 #endif
31 
32  /**
33  * @ingroup window
34  * @brief Modifier enumeration
35  */
36  enum class Mod : unsigned {
37  Shift = 0x0001, ///< Is the Shift key pressed?
38  Control = 0x0002, ///< Is the Control key pressed?
39  Alt = 0x0004, ///< Is the Alt key pressed?
40  Super = 0x0008, ///< Is the Super key pressed?
41  };
42 
43  /**
44  * @ingroup window
45  * @brief Keyboard modifiers
46  */
47  using Modifiers = Flags<Mod>;
48 
49 #ifndef DOXYGEN_SHOULD_SKIP_THIS
50 }
51 
52 template<>
53 struct EnableBitmaskOperators<Modifiers> {
54  static constexpr bool value = true;
55 };
56 
57 inline namespace v1 {
58 #endif
59 
60  /**
61  * @ingroup window
62  * @brief Scancodes
63  *
64  * These codes corresponds to [USB key codes](http://www.usb.org/developers/hidpage/Hut1_12v2.pdf).
65  */
66  enum class Scancode : int {
67  Unknown = 0,
68  A = 4,
69  B = 5,
70  C = 6,
71  D = 7,
72  E = 8,
73  F = 9,
74  G = 10,
75  H = 11,
76  I = 12,
77  J = 13,
78  K = 14,
79  L = 15,
80  M = 16,
81  N = 17,
82  O = 18,
83  P = 19,
84  Q = 20,
85  R = 21,
86  S = 22,
87  T = 23,
88  U = 24,
89  V = 25,
90  W = 26,
91  X = 27,
92  Y = 28,
93  Z = 29,
94  Num1 = 30,
95  Num2 = 31,
96  Num3 = 32,
97  Num4 = 33,
98  Num5 = 34,
99  Num6 = 35,
100  Num7 = 36,
101  Num8 = 37,
102  Num9 = 38,
103  Num0 = 39,
104  Return = 40,
105  Escape = 41,
106  Backspace = 42,
107  Tab = 43,
108  Space = 44,
109  Minus = 45,
110  Equals = 46,
111  LeftBracket = 47,
112  RightBracket = 48,
113  Backslash = 49,
114  NonUsHash = 50,
115  Semicolon = 51,
116  Apostrophe = 52,
117  Grave = 53,
118  Comma = 54,
119  Period = 55,
120  Slash = 56,
121  CapsLock = 57,
122  F1 = 58,
123  F2 = 59,
124  F3 = 60,
125  F4 = 61,
126  F5 = 62,
127  F6 = 63,
128  F7 = 64,
129  F8 = 65,
130  F9 = 66,
131  F10 = 67,
132  F11 = 68,
133  F12 = 69,
134  PrintScreen = 70,
135  ScrollLock = 71,
136  Pause = 72,
137  Insert = 73,
138  Home = 74,
139  PageUp = 75,
140  Delete = 76,
141  End = 77,
142  PageDown = 78,
143  Right = 79,
144  Left = 80,
145  Down = 81,
146  Up = 82,
147  NumLockClear = 83,
148  NumpadDivide = 84,
149  NumpadMultiply = 85,
150  NumpadMinus = 86,
151  NumpadPlus = 87,
152  NumpadEnter = 88,
153  Numpad1 = 89,
154  Numpad2 = 90,
155  Numpad3 = 91,
156  Numpad4 = 92,
157  Numpad5 = 93,
158  Numpad6 = 94,
159  Numpad7 = 95,
160  Numpad8 = 96,
161  Numpad9 = 97,
162  Numpad0 = 98,
163  NumpadPeriod = 99,
164  NonUsBackslash = 100,
165  Application = 101,
166  Power = 102,
167  NumpadEquals = 103,
168  F13 = 104,
169  F14 = 105,
170  F15 = 106,
171  F16 = 107,
172  F17 = 108,
173  F18 = 109,
174  F19 = 110,
175  F20 = 111,
176  F21 = 112,
177  F22 = 113,
178  F23 = 114,
179  F24 = 115,
180  Execute = 116,
181  Help = 117,
182  Menu = 118,
183  Select = 119,
184  Stop = 120,
185  Again = 121,
186  Undo = 122,
187  Cut = 123,
188  Copy = 124,
189  Paste = 125,
190  Find = 126,
191  Mute = 127,
192  VolumeUp = 128,
193  VolumeDown = 129,
194  NumpadComma = 133,
195  NumpadEqualsAs400 = 134,
196  International1 = 135,
197  International2 = 136,
198  International3 = 137,
199  International4 = 138,
200  International5 = 139,
201  International6 = 140,
202  International7 = 141,
203  International8 = 142,
204  International9 = 143,
205  Lang1 = 144,
206  Lang2 = 145,
207  Lang3 = 146,
208  Lang4 = 147,
209  Lang5 = 148,
210  Lang6 = 149,
211  Lang7 = 150,
212  Lang8 = 151,
213  Lang9 = 152,
214  AltErase = 153,
215  SysReq = 154,
216  Cancel = 155,
217  Clear = 156,
218  Prior = 157,
219  Return2 = 158,
220  Separator = 159,
221  Out = 160,
222  Oper = 161,
223  ClearAgain = 162,
224  CrSel = 163,
225  ExSel = 164,
226  Numpad00 = 176,
227  Numpad000 = 177,
228  ThousandsSeparator = 178,
229  DecimalSeparator = 179,
230  CurrencyUnit = 180,
231  CurrencySubUnit = 181,
232  NumpadLeftParen = 182,
233  NumpadRightParen = 183,
234  NumpadLeftBrace = 184,
235  NumpadRightBrace = 185,
236  NumpadTab = 186,
237  NumpadBackspace = 187,
238  NumpadA = 188,
239  NumpadB = 189,
240  NumpadC = 190,
241  NumpadD = 191,
242  NumpadE = 192,
243  NumpadF = 193,
244  NumpadXor = 194,
245  NumpadPower = 195,
246  NumpadPercent = 196,
247  NumpadLess = 197,
248  NumpadGreater = 198,
249  NumpadAmpersand = 199,
250  NumpadDblAmpersand = 200,
251  NumpadVerticalBar = 201,
252  NumpadDblVerticalBar = 202,
253  NumpadColon = 203,
254  NumpadHash = 204,
255  NumpadSpace = 205,
256  NumpadAt = 206,
257  NumpadExclam = 207,
258  NumpadMemStore = 208,
259  NumpadMemRecall = 209,
260  NumpadMemClear = 210,
261  NumpadMemAdd = 211,
262  NumpadMemSubtract = 212,
263  NumpadMemMultiply = 213,
264  NumpadMemDivide = 214,
265  NumpadPlusMinus = 215,
266  NumpadClear = 216,
267  NumpadClearEntry = 217,
268  NumpadBinary = 218,
269  NumpadOctal = 219,
270  NumpadDecimal = 220,
271  NumpadHexadecimal = 221,
272  LeftCtrl = 224,
273  LeftShift = 225,
274  LeftAlt = 226,
275  LeftGui = 227,
276  RightCtrl = 228,
277  RightShift = 229,
278  RightAlt = 230,
279  RightGui = 231,
280  Mode = 257,
281  };
282 
283  #ifndef DOXYGEN_SHOULD_SKIP_THIS
284  constexpr int getKeycodeFromScancode(Scancode scancode) {
285  return static_cast<int>(scancode) | (1 << 30);
286  }
287  #endif
288 
289  /**
290  * @ingroup window
291  * @brief Keycodes
292  */
293  enum class Keycode : int {
294  Unknown = 0,
295  Return = '\r',
296  Escape = '\033',
297  Backspace = '\b',
298  Tab = '\t',
299  Space = ' ',
300  Exclaim = '!',
301  QuoteDbl = '"',
302  Hash = '#',
303  Percent = '%',
304  Dollar = '$',
305  Ampersand = '&',
306  Quote = '\'',
307  LeftParen = '(',
308  RightParen = ')',
309  Asterisk = '*',
310  Plus = '+',
311  Comma = ',',
312  Minus = '-',
313  Period = '.',
314  Slash = '/',
315  Num0 = '0',
316  Num1 = '1',
317  Num2 = '2',
318  Num3 = '3',
319  Num4 = '4',
320  Num5 = '5',
321  Num6 = '6',
322  Num7 = '7',
323  Num8 = '8',
324  Num9 = '9',
325  Colon = ':',
326  SemiColon = ';',
327  Less = '<',
328  Equals = '=',
329  Greater = '>',
330  Question = '?',
331  At = '@',
332  LeftBracket = '[',
333  Backslash = '\\',
334  RightBracket = ']',
335  Caret = '^',
336  Underscore = '_',
337  Backquote = '`',
338  A = 'a',
339  B = 'b',
340  C = 'c',
341  D = 'd',
342  E = 'e',
343  F = 'f',
344  G = 'g',
345  H = 'h',
346  I = 'i',
347  J = 'j',
348  K = 'k',
349  L = 'l',
350  M = 'm',
351  N = 'n',
352  O = 'o',
353  P = 'p',
354  Q = 'q',
355  R = 'r',
356  S = 's',
357  T = 't',
358  U = 'u',
359  V = 'v',
360  W = 'w',
361  X = 'x',
362  Y = 'y',
363  Z = 'z',
364  CapsLock = getKeycodeFromScancode(Scancode::CapsLock),
365  F1 = getKeycodeFromScancode(Scancode::F1),
366  F2 = getKeycodeFromScancode(Scancode::F2),
367  F3 = getKeycodeFromScancode(Scancode::F3),
368  F4 = getKeycodeFromScancode(Scancode::F4),
369  F5 = getKeycodeFromScancode(Scancode::F5),
370  F6 = getKeycodeFromScancode(Scancode::F6),
371  F7 = getKeycodeFromScancode(Scancode::F7),
372  F8 = getKeycodeFromScancode(Scancode::F8),
373  F9 = getKeycodeFromScancode(Scancode::F9),
374  F10 = getKeycodeFromScancode(Scancode::F10),
375  F11 = getKeycodeFromScancode(Scancode::F11),
376  F12 = getKeycodeFromScancode(Scancode::F12),
377  PrintScreen = getKeycodeFromScancode(Scancode::PrintScreen),
378  ScrollLock = getKeycodeFromScancode(Scancode::ScrollLock),
379  Pause = getKeycodeFromScancode(Scancode::Pause),
380  Insert = getKeycodeFromScancode(Scancode::Insert),
381  Home = getKeycodeFromScancode(Scancode::Home),
382  PageUp = getKeycodeFromScancode(Scancode::PageUp),
383  Delete = '\177',
384  End = getKeycodeFromScancode(Scancode::End),
385  PageDown = getKeycodeFromScancode(Scancode::PageDown),
386  Right = getKeycodeFromScancode(Scancode::Right),
387  Left = getKeycodeFromScancode(Scancode::Left),
388  Down = getKeycodeFromScancode(Scancode::Down),
389  Up = getKeycodeFromScancode(Scancode::Up),
390  NumLockClear = getKeycodeFromScancode(Scancode::NumLockClear),
391  NumpadDivide = getKeycodeFromScancode(Scancode::NumpadDivide),
392  NumpadMultiply = getKeycodeFromScancode(Scancode::NumpadMultiply),
393  NumpadMinus = getKeycodeFromScancode(Scancode::NumpadMinus),
394  NumpadPlus = getKeycodeFromScancode(Scancode::NumpadPlus),
395  NumpadEnter = getKeycodeFromScancode(Scancode::NumpadEnter),
396  Numpad1 = getKeycodeFromScancode(Scancode::Numpad1),
397  Numpad2 = getKeycodeFromScancode(Scancode::Numpad2),
398  Numpad3 = getKeycodeFromScancode(Scancode::Numpad3),
399  Numpad4 = getKeycodeFromScancode(Scancode::Numpad4),
400  Numpad5 = getKeycodeFromScancode(Scancode::Numpad5),
401  Numpad6 = getKeycodeFromScancode(Scancode::Numpad6),
402  Numpad7 = getKeycodeFromScancode(Scancode::Numpad7),
403  Numpad8 = getKeycodeFromScancode(Scancode::Numpad8),
404  Numpad9 = getKeycodeFromScancode(Scancode::Numpad9),
405  Numpad0 = getKeycodeFromScancode(Scancode::Numpad0),
406  NumpadPeriod = getKeycodeFromScancode(Scancode::NumpadPeriod),
407  Application = getKeycodeFromScancode(Scancode::Application),
408  Power = getKeycodeFromScancode(Scancode::Power),
409  NumpadEquals = getKeycodeFromScancode(Scancode::NumpadEquals),
410  F13 = getKeycodeFromScancode(Scancode::F13),
411  F14 = getKeycodeFromScancode(Scancode::F14),
412  F15 = getKeycodeFromScancode(Scancode::F15),
413  F16 = getKeycodeFromScancode(Scancode::F16),
414  F17 = getKeycodeFromScancode(Scancode::F17),
415  F18 = getKeycodeFromScancode(Scancode::F18),
416  F19 = getKeycodeFromScancode(Scancode::F19),
417  F20 = getKeycodeFromScancode(Scancode::F20),
418  F21 = getKeycodeFromScancode(Scancode::F21),
419  F22 = getKeycodeFromScancode(Scancode::F22),
420  F23 = getKeycodeFromScancode(Scancode::F23),
421  F24 = getKeycodeFromScancode(Scancode::F24),
422  Execute = getKeycodeFromScancode(Scancode::Execute),
423  Help = getKeycodeFromScancode(Scancode::Help),
424  Menu = getKeycodeFromScancode(Scancode::Menu),
425  Select = getKeycodeFromScancode(Scancode::Select),
426  Stop = getKeycodeFromScancode(Scancode::Stop),
427  Again = getKeycodeFromScancode(Scancode::Again),
428  Undo = getKeycodeFromScancode(Scancode::Undo),
429  Cut = getKeycodeFromScancode(Scancode::Cut),
430  Copy = getKeycodeFromScancode(Scancode::Copy),
431  Paste = getKeycodeFromScancode(Scancode::Paste),
432  Find = getKeycodeFromScancode(Scancode::Find),
433  Mute = getKeycodeFromScancode(Scancode::Mute),
434  VolumeUp = getKeycodeFromScancode(Scancode::VolumeUp),
435  VolumeDown = getKeycodeFromScancode(Scancode::VolumeDown),
436  NumpadComma = getKeycodeFromScancode(Scancode::NumpadComma),
437  NumpadEqualsAs400 = getKeycodeFromScancode(Scancode::NumpadEqualsAs400),
438  AltErase = getKeycodeFromScancode(Scancode::AltErase),
439  SysReq = getKeycodeFromScancode(Scancode::SysReq),
440  Cancel = getKeycodeFromScancode(Scancode::Cancel),
441  Clear = getKeycodeFromScancode(Scancode::Clear),
442  Prior = getKeycodeFromScancode(Scancode::Prior),
443  Return2 = getKeycodeFromScancode(Scancode::Return2),
444  Separator = getKeycodeFromScancode(Scancode::Separator),
445  Out = getKeycodeFromScancode(Scancode::Out),
446  Oper = getKeycodeFromScancode(Scancode::Oper),
447  ClearAgain = getKeycodeFromScancode(Scancode::ClearAgain),
448  CrSel = getKeycodeFromScancode(Scancode::CrSel),
449  ExSel = getKeycodeFromScancode(Scancode::ExSel),
450  Numpad00 = getKeycodeFromScancode(Scancode::Numpad00),
451  Numpad000 = getKeycodeFromScancode(Scancode::Numpad000),
452  ThousandsSeparator = getKeycodeFromScancode(Scancode::ThousandsSeparator),
453  DecimalSeparator = getKeycodeFromScancode(Scancode::DecimalSeparator),
454  CurrencyUnit = getKeycodeFromScancode(Scancode::CurrencyUnit),
455  CurrencySubUnit = getKeycodeFromScancode(Scancode::CurrencySubUnit),
456  NumpadLeftParen = getKeycodeFromScancode(Scancode::NumpadLeftParen),
457  NumpadRightParen = getKeycodeFromScancode(Scancode::NumpadRightParen),
458  NumpadLeftBrace = getKeycodeFromScancode(Scancode::NumpadLeftBrace),
459  NumpadRightBrace = getKeycodeFromScancode(Scancode::NumpadRightBrace),
460  NumpadTab = getKeycodeFromScancode(Scancode::NumpadTab),
461  NumpadBackspace = getKeycodeFromScancode(Scancode::NumpadBackspace),
462  NumpadA = getKeycodeFromScancode(Scancode::NumpadA),
463  NumpadB = getKeycodeFromScancode(Scancode::NumpadB),
464  NumpadC = getKeycodeFromScancode(Scancode::NumpadC),
465  NumpadD = getKeycodeFromScancode(Scancode::NumpadD),
466  NumpadE = getKeycodeFromScancode(Scancode::NumpadE),
467  NumpadF = getKeycodeFromScancode(Scancode::NumpadF),
468  NumpadXor = getKeycodeFromScancode(Scancode::NumpadXor),
469  NumpadPower = getKeycodeFromScancode(Scancode::NumpadPower),
470  NumpadPercent = getKeycodeFromScancode(Scancode::NumpadPercent),
471  NumpadLess = getKeycodeFromScancode(Scancode::NumpadLess),
472  NumpadGreater = getKeycodeFromScancode(Scancode::NumpadGreater),
473  NumpadAmpersand = getKeycodeFromScancode(Scancode::NumpadAmpersand),
474  NumpadDblAmpersand = getKeycodeFromScancode(Scancode::NumpadDblAmpersand),
475  NumpadVerticalBar = getKeycodeFromScancode(Scancode::NumpadVerticalBar),
476  NumpadDblVerticalbar = getKeycodeFromScancode(Scancode::NumpadDblVerticalBar),
477  NumpadColon = getKeycodeFromScancode(Scancode::NumpadColon),
478  NumpadHash = getKeycodeFromScancode(Scancode::NumpadHash),
479  NumpadSpace = getKeycodeFromScancode(Scancode::NumpadSpace),
480  NumpadAt = getKeycodeFromScancode(Scancode::NumpadAt),
481  NumpadExclam = getKeycodeFromScancode(Scancode::NumpadExclam),
482  NumpadMemStore = getKeycodeFromScancode(Scancode::NumpadMemStore),
483  NumpadMemRecall = getKeycodeFromScancode(Scancode::NumpadMemRecall),
484  NumpadMemClear = getKeycodeFromScancode(Scancode::NumpadMemClear),
485  NumpadMemAdd = getKeycodeFromScancode(Scancode::NumpadMemAdd),
486  NumpadMemSubtract = getKeycodeFromScancode(Scancode::NumpadMemSubtract),
487  NumpadMemMultiply = getKeycodeFromScancode(Scancode::NumpadMemMultiply),
488  NumpadMemDivide = getKeycodeFromScancode(Scancode::NumpadMemDivide),
489  NumpadPlusMinus = getKeycodeFromScancode(Scancode::NumpadPlusMinus),
490  NumpadClear = getKeycodeFromScancode(Scancode::NumpadClear),
491  NumpadClearEntry = getKeycodeFromScancode(Scancode::NumpadClearEntry),
492  NumpadBinary = getKeycodeFromScancode(Scancode::NumpadBinary),
493  NumpadOctal = getKeycodeFromScancode(Scancode::NumpadOctal),
494  NumpadDecimal = getKeycodeFromScancode(Scancode::NumpadDecimal),
495  NumpadHexadecimal = getKeycodeFromScancode(Scancode::NumpadHexadecimal),
496  LeftCtrl = getKeycodeFromScancode(Scancode::LeftCtrl),
497  LeftShift = getKeycodeFromScancode(Scancode::LeftShift),
498  LeftAlt = getKeycodeFromScancode(Scancode::LeftAlt),
499  LeftGui = getKeycodeFromScancode(Scancode::LeftGui),
500  RightCtrl = getKeycodeFromScancode(Scancode::RightCtrl),
501  RightShift = getKeycodeFromScancode(Scancode::RightShift),
502  RightAlt = getKeycodeFromScancode(Scancode::RightAlt),
503  RightGui = getKeycodeFromScancode(Scancode::RightGui),
504  Mode = getKeycodeFromScancode(Scancode::Mode),
505  };
506 
507  /**
508  * @ingroup window
509  * @brief Some keyboard related functions
510  */
511  struct GF_API Keyboard {
512  /**
513  * @brief Get the representation of a scancode
514  *
515  * @param scancode The scancode
516  * @return A string representing the scancode
517  * @sa getScancodeFromName()
518  */
519  static const char *getScancodeName(Scancode scancode);
520 
521  /**
522  * @brief Get the scancode associated to a name
523  *
524  * @param name The name of the scancode
525  * @return A scancode
526  * @sa getScancodeName()
527  */
528  static Scancode getScancodeFromName(const char *name);
529 
530  /**
531  * @brief Get the representation of a keycode
532  *
533  * @param keycode The keycode
534  * @return A string representing the keycode
535  * @sa getKeycodeFromName()
536  */
537  static const char *getKeycodeName(Keycode keycode);
538 
539  /**
540  * @brief Get the keycode associated to a name
541  *
542  * @param name The name of the keycode
543  * @return A keycode
544  * @sa getKeycodeName()
545  */
546  static Keycode getKeycodeFromName(const char *name);
547 
548  /**
549  * @brief Deleted constructor
550  */
551  Keyboard() = delete;
552 
553  };
554 
555 #ifndef DOXYGEN_SHOULD_SKIP_THIS
556 }
557 #endif
558 }
559 
560 #endif // GF_KEYBOARD_H
Bitfield relying on an enumeration.
Definition: Flags.h:68
Some keyboard related functions.
Definition: Keyboard.h:511
Keyboard()=delete
Deleted constructor.
Is the Control key pressed?
static Scancode getScancodeFromName(const char *name)
Get the scancode associated to a name.
static const char * getKeycodeName(Keycode keycode)
Get the representation of a keycode.
static const char * getScancodeName(Scancode scancode)
Get the representation of a scancode.
Is the Super key pressed?
Scancode
Scancodes.
Definition: Keyboard.h:66
Is the Shift key pressed?
Definition: Action.h:34
Is the Alt key pressed?
static Keycode getKeycodeFromName(const char *name)
Get the keycode associated to a name.
#define GF_API
Definition: Portability.h:35
Mod
Modifier enumeration.
Definition: Keyboard.h:36
Keycode
Keycodes.
Definition: Keyboard.h:293