R-Type
R-Type project
Key.hpp
1 /*
2 ** EPITECH PROJECT, 2022
3 ** gameEngine2
4 ** File description:
5 ** Key
6 */
7 
8 #ifndef KEY_HPP_
9 #define KEY_HPP_
10 
11 #include "Profiler/KapProfiler.hpp"
12 #include <string>
13 
14 namespace KapEngine {
15  namespace Events {
16 
17  class Key {
18  public:
19  Key() {
20  }
21  ~Key() {
22  }
23 
29  enum EKey {
30  UNKNOWN = -1, // Key: NULL, used for no key pressed
31  APOSTROPHE = 39, // Key: '
32  COMMA = 44, // Key: ,
33  MINUS = 45, // Key: -
34  PERIOD = 46, // Key: .
35  SLASH = 47, // Key: /
36  NUM0 = 48, // Key: 0
37  NUM1 = 49, // Key: 1
38  NUM2 = 50, // Key: 2
39  NUM3 = 51, // Key: 3
40  NUM4 = 52, // Key: 4
41  NUM5 = 53, // Key: 5
42  NUM6 = 54, // Key: 6
43  NUM7 = 55, // Key: 7
44  NUM8 = 56, // Key: 8
45  NUM9 = 57, // Key: 9
46  SEMICOLON = 59, // Key: ;
47  EQUAL = 61, // Key: =
48  A = 65, // Key: A | a
49  B = 66, // Key: B | b
50  C = 67, // Key: C | c
51  D = 68, // Key: D | d
52  E = 69, // Key: E | e
53  F = 70, // Key: F | f
54  G = 71, // Key: G | g
55  H = 72, // Key: H | h
56  I = 73, // Key: I | i
57  J = 74, // Key: J | j
58  K = 75, // Key: K | k
59  L = 76, // Key: L | l
60  M = 77, // Key: M | m
61  N = 78, // Key: N | n
62  O = 79, // Key: O | o
63  P = 80, // Key: P | p
64  Q = 81, // Key: Q | q
65  R = 82, // Key: R | r
66  S = 83, // Key: S | s
67  T = 84, // Key: T | t
68  U = 85, // Key: U | u
69  V = 86, // Key: V | v
70  W = 87, // Key: W | w
71  X = 88, // Key: X | x
72  Y = 89, // Key: Y | y
73  Z = 90, // Key: Z | z
74  LEFT_BRACKET = 91, // Key: [
75  BACKSLASH = 92, // Key: '\'
76  RIGHT_BRACKET = 93, // Key: ]
77  GRAVE = 96, // Key: `
78  SPACE = 32, // Key: Space
79  ESCAPE = 256, // Key: Esc
80  ENTER = 257, // Key: Enter
81  TAB = 258, // Key: Tab
82  BACKSPACE = 259, // Key: Backspace
83  INSERT = 260, // Key: Ins
84  DELETE = 261, // Key: Del
85  RIGHT = 262, // Key: Cursor right
86  LEFT = 263, // Key: Cursor left
87  DOWN = 264, // Key: Cursor down
88  UP = 265, // Key: Cursor up
89  PAGE_UP = 266, // Key: Page up
90  PAGE_DOWN = 267, // Key: Page down
91  HOME = 268, // Key: Home
92  END = 269, // Key: End
93  CAPS_LOCK = 280, // Key: Caps lock
94  SCROLL_LOCK = 281, // Key: Scroll down
95  NUM_LOCK = 282, // Key: NUM lock
96  PRINT_SCREEN = 283, // Key: Print screen
97  PAUSE = 284, // Key: Pause
98  F1 = 290, // Key: F1
99  F2 = 291, // Key: F2
100  F3 = 292, // Key: F3
101  F4 = 293, // Key: F4
102  F5 = 294, // Key: F5
103  F6 = 295, // Key: F6
104  F7 = 296, // Key: F7
105  F8 = 297, // Key: F8
106  F9 = 298, // Key: F9
107  F10 = 299, // Key: F10
108  F11 = 300, // Key: F11
109  F12 = 301, // Key: F12
110  LEFT_SHIFT = 340, // Key: Shift left
111  LEFT_CONTROL = 341, // Key: Control left
112  LEFT_ALT = 342, // Key: Alt left
113  LEFT_SUPER = 343, // Key: Super left
114  RIGHT_SHIFT = 344, // Key: Shift right
115  RIGHT_CONTROL = 345, // Key: Control right
116  RIGHT_ALT = 346, // Key: Alt right
117  RIGHT_SUPER = 347, // Key: Super right
118  KB_MENU = 348, // Key: KB menu
119  KP_0 = 320, // Key: Keypad 0
120  KP_1 = 321, // Key: Keypad 1
121  KP_2 = 322, // Key: Keypad 2
122  KP_3 = 323, // Key: Keypad 3
123  KP_4 = 324, // Key: Keypad 4
124  KP_5 = 325, // Key: Keypad 5
125  KP_6 = 326, // Key: Keypad 6
126  KP_7 = 327, // Key: Keypad 7
127  KP_8 = 328, // Key: Keypad 8
128  KP_9 = 329, // Key: Keypad 9
129  KP_DECIMAL = 330, // Key: Keypad .
130  KP_DIVIDE = 331, // Key: Keypad /
131  KP_MULTIPLY = 332, // Key: Keypad *
132  KP_SUBTRACT = 333, // Key: Keypad -
133  KP_ADD = 334, // Key: Keypad +
134  KP_ENTER = 335, // Key: Keypad Enter
135  KP_EQUAL = 336, // Key: Keypad =
136  BACK = 349, // Key: Android back button
137  MENU, // Key: Android menu button
138  VOLUME_UP, // Key: Android volume up button
139  VOLUME_DOWN, // Key: Android volume down button
140  MOUSE_LEFT, // Mouse button left
141  MOUSE_RIGHT, // Mouse button right
142  MOUSE_MIDDLE, // Mouse button middle (pressed wheel)
143  MOUSE_SIDE, // Mouse button side (advanced mouse device)
144  MOUSE_EXTRA, // Mouse button extra (advanced mouse device)
145  MOUSE_FORWARD, // Mouse button fordward (advanced mouse device)
146  MOUSE_BACK, // Mouse button back (advanced mouse device)
147  //all gamepads
148  GAMEPAD_LEFT_FACE_UP, // Gamepad left DPAD up button
149  GAMEPAD_LEFT_FACE_RIGHT, // Gamepad left DPAD right button
150  GAMEPAD_LEFT_FACE_DOWN, // Gamepad left DPAD down button
151  GAMEPAD_LEFT_FACE_LEFT, // Gamepad left DPAD left button
152  GAMEPAD_RIGHT_FACE_UP, // Gamepad right button up (i.e. PS3: Triangle, Xbox: Y)
153  GAMEPAD_RIGHT_FACE_RIGHT, // Gamepad right button right (i.e. PS3: Square, Xbox: X)
154  GAMEPAD_RIGHT_FACE_DOWN, // Gamepad right button down (i.e. PS3: Cross, Xbox: A)
155  GAMEPAD_RIGHT_FACE_LEFT, // Gamepad right button left (i.e. PS3: Circle, Xbox: B)
156  GAMEPAD_LEFT_TRIGGER_1, // Gamepad top/back trigger left (first), it could be a trailing button
157  GAMEPAD_LEFT_TRIGGER_2, // Gamepad top/back trigger left (second), it could be a trailing button
158  GAMEPAD_RIGHT_TRIGGER_1, // Gamepad top/back trigger right (one), it could be a trailing button
159  GAMEPAD_RIGHT_TRIGGER_2, // Gamepad top/back trigger right (second), it could be a trailing button
160  GAMEPAD_MIDDLE_LEFT, // Gamepad center buttons, left one (i.e. PS3: Select)
161  GAMEPAD_MIDDLE, // Gamepad center buttons, middle one (i.e. PS3: PS, Xbox: XBOX)
162  GAMEPAD_MIDDLE_RIGHT, // Gamepad center buttons, right one (i.e. PS3: Start)
163  GAMEPAD_LEFT_THUMB, // Gamepad joystick pressed button left
164  GAMEPAD_RIGHT_THUMB, // Gamepad joystick pressed button right
165  //gamepad 0
166  GAMEPAD0_LEFT_FACE_UP, // Gamepad left DPAD up button
167  GAMEPAD0_LEFT_FACE_RIGHT, // Gamepad left DPAD right button
168  GAMEPAD0_LEFT_FACE_DOWN, // Gamepad left DPAD down button
169  GAMEPAD0_LEFT_FACE_LEFT, // Gamepad left DPAD left button
170  GAMEPAD0_RIGHT_FACE_UP, // Gamepad right button up (i.e. PS3: Triangle, Xbox: Y)
171  GAMEPAD0_RIGHT_FACE_RIGHT, // Gamepad right button right (i.e. PS3: Square, Xbox: X)
172  GAMEPAD0_RIGHT_FACE_DOWN, // Gamepad right button down (i.e. PS3: Cross, Xbox: A)
173  GAMEPAD0_RIGHT_FACE_LEFT, // Gamepad right button left (i.e. PS3: Circle, Xbox: B)
174  GAMEPAD0_LEFT_TRIGGER_1, // Gamepad top/back trigger left (first), it could be a trailing button
175  GAMEPAD0_LEFT_TRIGGER_2, // Gamepad top/back trigger left (second), it could be a trailing button
176  GAMEPAD0_RIGHT_TRIGGER_1, // Gamepad top/back trigger right (one), it could be a trailing button
177  GAMEPAD0_RIGHT_TRIGGER_2, // Gamepad top/back trigger right (second), it could be a trailing button
178  GAMEPAD0_MIDDLE_LEFT, // Gamepad center buttons, left one (i.e. PS3: Select)
179  GAMEPAD0_MIDDLE, // Gamepad center buttons, middle one (i.e. PS3: PS, Xbox: XBOX)
180  GAMEPAD0_MIDDLE_RIGHT, // Gamepad center buttons, right one (i.e. PS3: Start)
181  GAMEPAD0_LEFT_THUMB, // Gamepad joystick pressed button left
182  GAMEPAD0_RIGHT_THUMB, // Gamepad joystick pressed button right
183  //gamepad 1
184  GAMEPAD1_LEFT_FACE_UP, // Gamepad left DPAD up button
185  GAMEPAD1_LEFT_FACE_RIGHT, // Gamepad left DPAD right button
186  GAMEPAD1_LEFT_FACE_DOWN, // Gamepad left DPAD down button
187  GAMEPAD1_LEFT_FACE_LEFT, // Gamepad left DPAD left button
188  GAMEPAD1_RIGHT_FACE_UP, // Gamepad right button up (i.e. PS3: Triangle, Xbox: Y)
189  GAMEPAD1_RIGHT_FACE_RIGHT, // Gamepad right button right (i.e. PS3: Square, Xbox: X)
190  GAMEPAD1_RIGHT_FACE_DOWN, // Gamepad right button down (i.e. PS3: Cross, Xbox: A)
191  GAMEPAD1_RIGHT_FACE_LEFT, // Gamepad right button left (i.e. PS3: Circle, Xbox: B)
192  GAMEPAD1_LEFT_TRIGGER_1, // Gamepad top/back trigger left (first), it could be a trailing button
193  GAMEPAD1_LEFT_TRIGGER_2, // Gamepad top/back trigger left (second), it could be a trailing button
194  GAMEPAD1_RIGHT_TRIGGER_1, // Gamepad top/back trigger right (one), it could be a trailing button
195  GAMEPAD1_RIGHT_TRIGGER_2, // Gamepad top/back trigger right (second), it could be a trailing button
196  GAMEPAD1_MIDDLE_LEFT, // Gamepad center buttons, left one (i.e. PS3: Select)
197  GAMEPAD1_MIDDLE, // Gamepad center buttons, middle one (i.e. PS3: PS, Xbox: XBOX)
198  GAMEPAD1_MIDDLE_RIGHT, // Gamepad center buttons, right one (i.e. PS3: Start)
199  GAMEPAD1_LEFT_THUMB, // Gamepad joystick pressed button left
200  GAMEPAD1_RIGHT_THUMB, // Gamepad joystick pressed button right
201  //specials keys
202  TILDE,
203  UNDERSCORE,
204  F13,
205  F14,
206  F15,
207  //equalities
208  QUOTE = APOSTROPHE,
209  SUBSTRACT = MINUS, // '-'
210  DIVIDE = SLASH, // '/'
211  POINT = PERIOD, // '.'
212 
213  };
214 
221  static EKey maxValue() {
222  PROFILER_FUNC_START();
223  PROFILER_FUNC_END();
224  return F15;
225  }
226 
233  static EKey minValue() {
234  PROFILER_FUNC_START();
235  PROFILER_FUNC_END();
236  return UNKNOWN;
237  }
238 
239  Key &operator=(EKey const& _ek) {
240  this->_last = _ek;
241  return *this;
242  }
243  Key &operator=(Key const& _k) {
244  this->_last = _k._last;
245  return *this;
246  }
247  bool operator==(EKey const& _ek) const {
248  PROFILER_FUNC_START();
249  PROFILER_FUNC_END();
250  return (this->_last == _ek);
251  }
252  bool operator==(Key const& _k) const {
253  PROFILER_FUNC_START();
254  PROFILER_FUNC_END();
255  return (this->_last == _k._last);
256  }
257  bool operator!=(EKey const& _ek) const {
258  PROFILER_FUNC_START();
259  PROFILER_FUNC_END();
260  return !(*this == _ek);
261  }
262  bool operator!=(Key const& _ek) const {
263  PROFILER_FUNC_START();
264  PROFILER_FUNC_END();
265  return !(*this == _ek);
266  }
267 
276  static bool intInEnum(int const& id) {
277  PROFILER_FUNC_START();
278  PROFILER_FUNC_END();
279  if (id == -1)
280  return true;
281  if (id == 32)
282  return true;
283  if (id == 96)
284  return true;
285  if (id == 59)
286  return true;
287  if (id >= 44 && id <= 57)
288  return true;
289  //A to Z + 2 keyboard input
290  if (id >= 65 && id <= 93)
291  return true;
292  if (id >= 259 && id <= 269)
293  return true;
294  if (id >= 280 && id <= 284)
295  return true;
296  if (id >= 290 && id <= 301)
297  return true;
298  if (id >= 320 && id <= 336)
299  return true;
300  if (id >= 340)
301  return true;
302  if (id >= EKey::MENU && id <= EKey::F15)
303  return true;
304  return false;
305  }
306 
314  bool isKeyboardKey() const {
315  if (_last >= 1 && _last <= EKey::VOLUME_DOWN)
316  return true;
317  if (_last >= EKey::TILDE && _last <= EKey::F15)
318  return true;
319  return false;
320  }
321 
329  bool isMouseKey() const {
330  if (_last >= EKey::MOUSE_LEFT && _last <= EKey::MOUSE_BACK)
331  return true;
332  return false;
333  }
334 
342  bool isGamepadKey() const {
343  if (!isKeyboardKey() && !isMouseKey())
344  return true;
345  return false;
346  }
347 
348  Key &operator=(int const& _k) {
349  if (_k > maxValue() || _k < minValue()) {
350  _last = UNKNOWN;
351  }else {
352  _last = (EKey) _k;
353  }
354  return *this;
355  }
356 
363  std::string toString() const {
364  PROFILER_FUNC_START();
365  PROFILER_FUNC_END();
366  switch (_last) {
367  case A:
368  return "A";
369  case B:
370  return "B";
371  case C:
372  return "C";
373  case D:
374  return "D";
375  case E:
376  return "E";
377  case F:
378  return "F";
379  case G:
380  return "G";
381  case H:
382  return "H";
383  case I:
384  return "I";
385  case J:
386  return "J";
387  case K:
388  return "K";
389  case L:
390  return "L";
391  case M:
392  return "M";
393  case N:
394  return "N";
395  case O:
396  return "O";
397  case P:
398  return "P";
399  case Q:
400  return "Q";
401  case R:
402  return "R";
403  case S:
404  return "S";
405  case T:
406  return "T";
407  case U:
408  return "U";
409  case V:
410  return "V";
411  case W:
412  return "W";
413  case X:
414  return "X";
415  case Y:
416  return "Y";
417  case Z:
418  return "Z";
419  case NUM0:
420  return "NUM0";
421  case NUM1:
422  return "NUM1";
423  case NUM2:
424  return "NUM2";
425  case NUM3:
426  return "NUM3";
427  case NUM4:
428  return "NUM4";
429  case NUM5:
430  return "NUM5";
431  case NUM6:
432  return "NUM6";
433  case NUM7:
434  return "NUM7";
435  case NUM8:
436  return "NUM8";
437  case NUM9:
438  return "NUM9";
439  case ESCAPE:
440  return "ESCAPE";
441  case LEFT_CONTROL:
442  return "LEFT_CONTROL";
443  case LEFT_SHIFT:
444  return "LEFT_SHIFT";
445  case LEFT_ALT:
446  return "LEFT_ALT";
447  case LEFT_SUPER:
448  return "LEFT_SUPER";
449  case RIGHT_CONTROL:
450  return "RIGHT_CONTROL";
451  case RIGHT_SHIFT:
452  return "RIGHT_SHIFT";
453  case RIGHT_ALT:
454  return "RIGHT_ALT";
455  case RIGHT_SUPER:
456  return "RIGHT_SUPER";
457  case LEFT_BRACKET:
458  return "BACKSLASH";
459  case RIGHT_BRACKET:
460  return "RIGHT_BRACKET";
461  case SEMICOLON:
462  return "SEMICOLON";
463  case COMMA:
464  return "COMMA";
465  case PERIOD:
466  return "PERIOD | POINT";
467  case SLASH:
468  return "SLASH | DIVIDE";
469  case BACKSLASH:
470  return "BACKSLASH";
471  case EQUAL:
472  return "EQUAL";
473  case UNDERSCORE:
474  return "UNDERSCORE";
475  case SPACE:
476  return "SPACE";
477  case ENTER:
478  return "ENTER";
479  case BACKSPACE:
480  return "BACKSPACE";
481  case TAB:
482  return "TAB";
483  case PAGE_UP:
484  return "PAGE_UP";
485  case PAGE_DOWN:
486  return "PAGE_DOWN";
487  case END:
488  return "END";
489  case HOME:
490  return "HOME";
491  case INSERT:
492  return "INSERT";
493  case DELETE:
494  return "DELETE";
495  case KP_ADD:
496  return "KP_ADD";
497  case SUBSTRACT:
498  return "MINUS";
499  case KP_MULTIPLY:
500  return "KP_MULTIPLY";
501  case LEFT:
502  return "LEFT";
503  case RIGHT:
504  return "RIGHT";
505  case UP:
506  return "UP";
507  case DOWN:
508  return "DOWN";
509  case KP_0:
510  return "KP_0";
511  case KP_1:
512  return "KP_1";
513  case KP_2:
514  return "KP_2";
515  case KP_3:
516  return "KP_3";
517  case KP_4:
518  return "KP_4";
519  case KP_5:
520  return "KP_5";
521  case KP_6:
522  return "KP_6";
523  case KP_7:
524  return "KP_7";
525  case KP_8:
526  return "KP_8";
527  case KP_9:
528  return "KP_9";
529  case F1:
530  return "F1";
531  case F2:
532  return "F2";
533  case F3:
534  return "F3";
535  case F4:
536  return "F4";
537  case F5:
538  return "F5";
539  case F6:
540  return "F6";
541  case F7:
542  return "F7";
543  case F8:
544  return "F8";
545  case F9:
546  return "F9";
547  case F10:
548  return "F10";
549  case F11:
550  return "F11";
551  case F12:
552  return "F12";
553  case F13:
554  return "F13";
555  case F14:
556  return "F14";
557  case F15:
558  return "F15";
559  case PAUSE:
560  return "PAUSE";
561  case MOUSE_LEFT:
562  return "MOUSE_LEFT";
563  case MOUSE_RIGHT:
564  return "MOUSE_RIGHT";
565  case MOUSE_MIDDLE:
566  return "MouseMiddle";
567  case MOUSE_EXTRA:
568  return "MOUSE_EXTRA";
569  case MOUSE_SIDE:
570  return "MOUSE_SIDE";
571  case MOUSE_BACK:
572  return "MOUSE_BACK";
573  case MOUSE_FORWARD:
574  return "MOUSE_FORWARD";
575  case MENU:
576  return "MENU (Android)";
577  default:
578  return "Unknown";
579  }
580  }
581 
587  std::string getTextValue() const {
588  PROFILER_FUNC_START();
589  PROFILER_FUNC_END();
590  switch (_last) {
591  case A:
592  return "A";
593  case B:
594  return "B";
595  case C:
596  return "C";
597  case D:
598  return "D";
599  case E:
600  return "E";
601  case F:
602  return "F";
603  case G:
604  return "G";
605  case H:
606  return "H";
607  case I:
608  return "I";
609  case J:
610  return "J";
611  case K:
612  return "K";
613  case L:
614  return "L";
615  case M:
616  return "M";
617  case N:
618  return "N";
619  case O:
620  return "O";
621  case P:
622  return "P";
623  case Q:
624  return "Q";
625  case R:
626  return "R";
627  case S:
628  return "S";
629  case T:
630  return "T";
631  case U:
632  return "U";
633  case V:
634  return "V";
635  case W:
636  return "W";
637  case X:
638  return "X";
639  case Y:
640  return "Y";
641  case Z:
642  return "Z";
643  case NUM0:
644  return "0";
645  case NUM1:
646  return "1";
647  case NUM2:
648  return "2";
649  case NUM3:
650  return "3";
651  case NUM4:
652  return "4";
653  case NUM5:
654  return "5";
655  case NUM6:
656  return "6";
657  case NUM7:
658  return "7";
659  case NUM8:
660  return "8";
661  case NUM9:
662  return "9";
663  case LEFT_BRACKET:
664  return "{";
665  case RIGHT_BRACKET:
666  return "}";
667  case SEMICOLON:
668  return ";";
669  case COMMA:
670  return ",";
671  case PERIOD:
672  return ".";
673  case SLASH:
674  return "/";
675  case BACKSLASH:
676  return "\\";
677  case EQUAL:
678  return "=";
679  case UNDERSCORE:
680  return "_";
681  case SPACE:
682  return " ";
683  case ENTER:
684  return "\n";
685  case TAB:
686  return "\t";
687  case KP_ADD:
688  return "+";
689  case SUBSTRACT:
690  return "-";
691  case KP_MULTIPLY:
692  return "*";
693  case KP_0:
694  return "0";
695  case KP_1:
696  return "1";
697  case KP_2:
698  return "2";
699  case KP_3:
700  return "3";
701  case KP_4:
702  return "4";
703  case KP_5:
704  return "5";
705  case KP_6:
706  return "6";
707  case KP_7:
708  return "7";
709  case KP_8:
710  return "8";
711  case KP_9:
712  return "9";
713  default:
714  return "";
715  }
716  }
717 
723  EKey get() const {
724  return _last;
725  }
726 
727  protected:
728  private:
729  EKey _last = UNKNOWN;
730  };
731 
732  }
733 }
734 
735 #endif /* !KEY_HPP_ */
KapEngine::Events::Key::intInEnum
static bool intInEnum(int const &id)
Check id in enum.
Definition: Key.hpp:276
KapEngine::Events::Key
Definition: Key.hpp:17
KapEngine::Events::Key::EKey
EKey
all keys
Definition: Key.hpp:29
KapEngine::Events::Key::maxValue
static EKey maxValue()
Max value EKey.
Definition: Key.hpp:221
KapEngine::Events::Key::isKeyboardKey
bool isKeyboardKey() const
Check is a keyboard key.
Definition: Key.hpp:314
KapEngine::Events::Key::get
EKey get() const
get current value Key
Definition: Key.hpp:723
KapEngine::Events::Key::toString
std::string toString() const
Get key name.
Definition: Key.hpp:363
KapEngine::Events::Key::isMouseKey
bool isMouseKey() const
vérifie si la touche provient de la souris
Definition: Key.hpp:329
KapEngine::Events::Key::getTextValue
std::string getTextValue() const
Get displayable value of your key.
Definition: Key.hpp:587
KapEngine
main namespace
Definition: Component.hpp:17
KapEngine::Events::Key::isGamepadKey
bool isGamepadKey() const
Check is a gamepad key.
Definition: Key.hpp:342
KapEngine::Events::Key::minValue
static EKey minValue()
Minimum value EKey.
Definition: Key.hpp:233