-
Notifications
You must be signed in to change notification settings - Fork 0
/
Keyboard.cpp
106 lines (89 loc) · 1.69 KB
/
Keyboard.cpp
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#include "Keyboard.h"
bool Keyboard::KeyIsPressed( unsigned char keycode ) const
{
return keystates[keycode];
}
Keyboard::Event Keyboard::ReadKey()
{
if( keybuffer.size() > 0u )
{
Keyboard::Event e = keybuffer.front();
keybuffer.pop();
return e;
}
else
{
return Keyboard::Event();
}
}
bool Keyboard::KeyIsEmpty() const
{
return keybuffer.empty();
}
char Keyboard::ReadChar()
{
if( charbuffer.size() > 0u )
{
unsigned char charcode = charbuffer.front();
charbuffer.pop();
return charcode;
}
else
{
return 0;
}
}
bool Keyboard::CharIsEmpty() const
{
return charbuffer.empty();
}
void Keyboard::FlushKey()
{
keybuffer = std::queue<Event>();
}
void Keyboard::FlushChar()
{
charbuffer = std::queue<char>();
}
void Keyboard::Flush()
{
FlushKey();
FlushChar();
}
void Keyboard::EnableAutorepeat()
{
autorepeatEnabled = true;
}
void Keyboard::DisableAutorepeat()
{
autorepeatEnabled = false;
}
bool Keyboard::AutorepeatIsEnabled() const
{
return autorepeatEnabled;
}
void Keyboard::OnKeyPressed( unsigned char keycode )
{
keystates[ keycode ] = true;
keybuffer.push( Keyboard::Event( Keyboard::Event::Press,keycode ) );
TrimBuffer( keybuffer );
}
void Keyboard::OnKeyReleased( unsigned char keycode )
{
keystates[ keycode ] = false;
keybuffer.push( Keyboard::Event( Keyboard::Event::Release,keycode ) );
TrimBuffer( keybuffer );
}
void Keyboard::OnChar( char character )
{
charbuffer.push( character );
TrimBuffer( charbuffer );
}
template<typename T>
void Keyboard::TrimBuffer( std::queue<T>& buffer )
{
while( buffer.size() > bufferSize )
{
buffer.pop();
}
}