-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKeyboard.h
79 lines (77 loc) · 1.45 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
#pragma once
#include <queue>
#include <bitset>
class Keyboard
{
friend class MainWindow;
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 )
:
type( type ),
code( code )
{}
bool IsPress() const
{
return type == Type::Press;
}
bool IsRelease() const
{
return type == Type::Release;
}
bool IsValid() const
{
return type != Type::Invalid;
}
unsigned char GetCode() const
{
return code;
}
};
public:
Keyboard() = default;
Keyboard( const Keyboard& ) = delete;
Keyboard& operator=( const Keyboard& ) = delete;
bool KeyIsPressed( unsigned char keycode ) const;
Event ReadKey();
bool KeyIsEmpty() const;
char ReadChar();
bool CharIsEmpty() const;
void FlushKey();
void FlushChar();
void Flush();
void EnableAutorepeat();
void DisableAutorepeat();
bool AutorepeatIsEnabled() const;
private:
void OnKeyPressed( unsigned char keycode );
void OnKeyReleased( unsigned char keycode );
void OnChar( char character );
void ClearState();
template<typename T>
void TrimBuffer( std::queue<T>& buffer );
private:
static constexpr unsigned int nKeys = 256u;
static constexpr unsigned int bufferSize = 4u;
bool autorepeatEnabled = false;
std::bitset<nKeys> keystates;
std::queue<Event> keybuffer;
std::queue<char> charbuffer;
};