-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKeyboard.cpp
More file actions
91 lines (74 loc) · 1.78 KB
/
Keyboard.cpp
File metadata and controls
91 lines (74 loc) · 1.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include "Keyboard.h"
//Public Methods
bool Keyboard::KeyIsPressed(unsigned char keycode) const noexcept {
return keyStates[keycode];
}
Keyboard::Event Keyboard::ReadKey() noexcept {
if (keybuffer.size() > 0u) {
Keyboard::Event e = keybuffer.front();
keybuffer.pop();
return e;
}
else {
return Keyboard::Event();
}
}
bool Keyboard::KeyIsEmpty() const noexcept {
return keybuffer.empty();
}
char Keyboard::ReadChar() noexcept {
if (charbuffer.size() > 0u) {
unsigned char charCode = charbuffer.front();
charbuffer.pop();
return charCode;
}
else {
return 0;
}
}
bool Keyboard::CharIsEmpty() const noexcept {
return charbuffer.empty();
}
void Keyboard::FlushKey() noexcept {
keybuffer = std::queue<Event>();
}
void Keyboard::FlushChar() noexcept{
charbuffer = std::queue<char>();
}
void Keyboard::Flush() noexcept {
FlushKey();
FlushChar();
}
void Keyboard::EnableAutoRepeat() noexcept {
autoRepeatEnabled = true;
}
void Keyboard::DisableAutoRepeat() noexcept {
autoRepeatEnabled = false;
}
bool Keyboard::AutoRepeatIsEnabled() const noexcept {
return autoRepeatEnabled;
}
//Private Methods
void Keyboard::OnKeyPressed(unsigned char keycode) noexcept {
keyStates[keycode] = true;
keybuffer.push(Keyboard::Event(Keyboard::Event::Type::Press, keycode));
TrimBuffer(keybuffer);
}
void Keyboard::OnKeyReleased(unsigned char keycode) noexcept {
keyStates[keycode] = false;
keybuffer.push(Keyboard::Event(Keyboard::Event::Type::Release, keycode));
TrimBuffer(keybuffer);
}
void Keyboard::OnChar(char character) noexcept {
charbuffer.push(character);
TrimBuffer(charbuffer);
}
void Keyboard::ClearState() noexcept {
keyStates.reset();
}
template<typename T>
void Keyboard::TrimBuffer(std::queue<T>& buffer) noexcept {
while (buffer.size() > bufferSize) {
buffer.pop();
}
}