-
Notifications
You must be signed in to change notification settings - Fork 2
/
KeyboardControl.h
77 lines (60 loc) · 1.84 KB
/
KeyboardControl.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
#ifndef EVESTANDALONE_KEYBOARDCONTROL_H
#define EVESTANDALONE_KEYBOARDCONTROL_H
#include <iostream>
#include <memory>
#include <string>
#include <future>
#include <thread>
#include <mutex>
#include <condition_variable>
#include <deque>
class KeyboardControl
{
public:
KeyboardControl():
_isQuitCommand(false),
_isNextCommand(false),
_isAutoplayCommand(false),
_mustCancel(false)
{
}
void PrintMenu() {
std::cout<<"a - autoplay, n - next, q - quit"<<std::endl;
}
void StartListening() {
io_thread.reset(new std::thread([&]{
char nextChar;
bool error = false;
while(!_mustCancel && (nextChar = std::getchar())){
std::cout<<"getchar code: "<<(int)nextChar<<" char: '"<<nextChar<<"'"<<endl;
switch (nextChar){
case 'n':
_isNextCommand = true;
break;
case 'a':
_isAutoplayCommand = true;
break;
case 'q':
_isQuitCommand = true;
break;
}
}
}));
}
void StopListening() {
_mustCancel = true;
}
bool IsQuitCommand() {return _isQuitCommand;}
void ClearQuitCommand() {_isQuitCommand = false; }
bool IsNextCommand() {return _isNextCommand;}
void ClearNextCommand() {_isNextCommand = false; }
bool IsAutoplayCommand() {return _isAutoplayCommand;}
void ClearAutoplayCommand() {_isAutoplayCommand = false; }
private:
std::atomic<bool> _isQuitCommand;
std::atomic<bool> _isNextCommand;
std::atomic<bool> _isAutoplayCommand;
std::atomic<bool> _mustCancel;
std::unique_ptr<std::thread> io_thread;
};
#endif //EVESTANDALONE_KEYBOARDCONTROL_H