-
Notifications
You must be signed in to change notification settings - Fork 0
/
Keyboard.h
82 lines (80 loc) · 1.68 KB
/
Keyboard.h
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
#pragma once
#include <queue>
#include <bitset>
class Keyboard
{
friend class Window;
public:
class Event
{
public:
enum class Type
{
Press,
Release,
Invalid
};
private:
Type type;
unsigned char code;
public:
Event()
:
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;
}
};
public:
Keyboard() = default;
Keyboard(const Keyboard&) = delete;
Keyboard& operator=(const Keyboard&) = delete;
// key event stuff
bool KeyIsPressed(unsigned char keycode) const noexcept;
Event ReadKey() noexcept;
bool KeyIsEmpty() const noexcept;
void FlushKey() noexcept;
// char event stuff
char ReadChar() noexcept;
bool CharIsEmpty() const noexcept;
void FlushChar() noexcept;
void Flush() noexcept;
// auto repeat control
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;
private:
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;
};