ECSTASY
All in the name
Loading...
Searching...
No Matches
Keyboard.hpp
Go to the documentation of this file.
1
11
12#ifndef ECSTASY_INTEGRATIONS_EVENT_INPUTS_KEYBOARD_HPP_
13#define ECSTASY_INTEGRATIONS_EVENT_INPUTS_KEYBOARD_HPP_
14
15#include <array>
16#include <iostream>
17#include <unordered_map>
18
21
23{
30 class Keyboard : public IResource {
31 public:
32#ifndef _DOXYGEN_
33
34 // LCOV_EXCL_START
35
36 SERIALIZABLE_ENUM(Key, -1, Unknown, A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y,
42 F15, Pause, Count)
43
44 // LCOV_EXCL_STOP
45#else
47 enum class Key {
48 Unknown = -1,
49 A,
50 B,
51 C,
52 D,
53 E,
54 F,
55 G,
56 H,
57 I,
58 J,
59 K,
60 L,
61 M,
62 N,
63 O,
64 P,
65 Q,
66 R,
67 S,
68 T,
69 U,
70 V,
71 W,
72 X,
73 Y,
74 Z,
75 Num0,
76 Num1,
77 Num2,
78 Num3,
79 Num4,
80 Num5,
81 Num6,
82 Num7,
83 Num8,
84 Num9,
85 Escape,
86 LControl,
87 LShift,
88 LAlt,
89 LSystem,
90 RControl,
91 RShift,
92 RAlt,
93 RSystem,
94 Menu,
95 LBracket,
96 RBracket,
97 Semicolon,
98 Comma,
99 Period,
100 Quote,
101 Slash,
102 Backslash,
103 Tilde,
104 Equal,
105 Hyphen,
106 Space,
107 Enter,
108 Backspace,
109 Tab,
110 PageUp,
111 PageDown,
112 End,
113 Home,
114 Insert,
115 Delete,
116 Add,
117 Subtract,
118 Multiply,
119 Divide,
120 Left,
121 Right,
122 Up,
123 Down,
124 Numpad0,
125 Numpad1,
126 Numpad2,
127 Numpad3,
128 Numpad4,
129 Numpad5,
130 Numpad6,
131 Numpad7,
132 Numpad8,
133 Numpad9,
134 F1,
135 F2,
136 F3,
137 F4,
138 F5,
139 F6,
140 F7,
141 F8,
142 F9,
143 F10,
144 F11,
145 F12,
146 F13,
147 F14,
148 F15,
149 Pause,
150
151 Count,
152 };
153#endif
154
161 Keyboard() noexcept : _keys({false}){};
162
169 virtual ~Keyboard() noexcept = default;
170
183 [[nodiscard]] constexpr bool isKeyDown(Key key) const
184 {
185 assertKeyValid(key);
186 return _keys[static_cast<size_t>(key)];
187 }
188
201 [[nodiscard]] constexpr bool isKeyUp(Key key) const
202 {
203 return !isKeyDown(key);
204 }
205
219 constexpr void setKeyState(Key key, bool down)
220 {
221 assertKeyValid(key);
222 _keys[static_cast<size_t>(key)] = down;
223 }
224
236 constexpr static void assertKeyValid(Key key, bool allowUnknown = false)
237 {
238 if (static_cast<size_t>(key) >= static_cast<size_t>(Key::Count) || (!allowUnknown && key == Key::Unknown))
239 [[unlikely]]
240 throw std::invalid_argument("Invalid key");
241 }
242
243 private:
246 };
247} // namespace ecstasy::integration::event
248
249#endif /* !ECSTASY_INTEGRATIONS_EVENT_INPUTS_KEYBOARD_HPP_ */
Base class of all registry resources.
Create a serializable enumeration type.
#define SERIALIZABLE_ENUM(NAME, STARTING_AT,...)
Create a serializable enumeration type.
Base class of all registry resources.
Definition IResource.hpp:33
virtual ~Keyboard() noexcept=default
Destroy the Keyboard.
Keyboard() noexcept
Construct a new Keyboard resource.
Definition Keyboard.hpp:161
constexpr bool isKeyUp(Key key) const
Check whether a key is up.
Definition Keyboard.hpp:201
constexpr void setKeyState(Key key, bool down)
Update a given key state.
Definition Keyboard.hpp:219
static constexpr void assertKeyValid(Key key, bool allowUnknown=false)
Check whether a key is valid.
Definition Keyboard.hpp:236
std::array< bool, static_cast< int >(Key::Count)> _keys
Keyboard keys state.
Definition Keyboard.hpp:245
constexpr bool isKeyDown(Key key) const
Check whether a key is down.
Definition Keyboard.hpp:183
@ Subtract
The - key (minus, usually from numpad)
@ RSystem
The right OS specific key: window (Windows and Linux), apple (MacOS X), ...
@ Count
Keep last – the total number of keyboard keys.
@ LSystem
The left OS specific key: window (Windows and Linux), apple (MacOS X), ...
Event integration.
Definition Event.hpp:25