-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKeyboard.h
More file actions
73 lines (70 loc) · 1.63 KB
/
Keyboard.h
File metadata and controls
73 lines (70 loc) · 1.63 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
#pragma once
#include <queue>
#include <bitset>
class Keyboard
{
friend class Window;
public:
class Event {
public:
enum class Type {
Press,
Release,
Invalid
};
Event() noexcept :
type(Type::Invalid),
code(0u)
{};
Event(Type type, unsigned char code) noexcept :
type(type),
code(code)
{};
bool isPress() const noexcept {
return type == Type::Press;
}
bool isRelease() const noexcept {
return type == Type::Release;
}
bool isValid() const noexcept {
return type != Type::Invalid;
}
unsigned char GetCode() const noexcept {
return code;
}
private:
Type type;
unsigned char code;
};
public:
Keyboard() = default;
Keyboard(const Keyboard&) = delete;
Keyboard& operator = (const Keyboard&) = delete;
//Key Event
bool KeyIsPressed(unsigned char keycode) const noexcept;
Event ReadKey() noexcept;
bool KeyIsEmpty() const noexcept;
void FlushKey() noexcept;
//char event
char ReadChar() noexcept;
bool CharIsEmpty() const noexcept;
void FlushChar() noexcept;
void Flush() noexcept;
//autorepeat
void EnableAutoRepeat() noexcept;
void DisableAutoRepeat() noexcept;
bool AutoRepeatIsEnabled() const noexcept;
private:
void OnKeyPressed(unsigned char keycode) noexcept;
void OnKeyReleased(unsigned char keycode) noexcept;
void OnChar(char character) noexcept;
void ClearState() noexcept;
template<typename T>
static void TrimBuffer(std::queue<T>& buffer) noexcept;
static constexpr unsigned int nKeys = 256u;
static constexpr unsigned int bufferSize = 16u;
bool autoRepeatEnabled = false;
std::bitset<nKeys> keyStates;
std::queue<Event> keybuffer;
std::queue<char> charbuffer;
};