-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdemo_keybhandle.cpp
More file actions
59 lines (56 loc) · 1.82 KB
/
demo_keybhandle.cpp
File metadata and controls
59 lines (56 loc) · 1.82 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
#include <iostream>
#include <string>
#include <thread>
#include <chrono>
#include "keybhandle.hpp"
using namespace std;
int main()
{
#ifndef _WIN32
cout << R"(
As you can see, there is a echo when you type any key. The problem is the
keyboard is not ignored during sleep in C++. Delete/comment the line
this_thread::sleep_for(std::chrono::milliseconds(200));
and all will be fine. Just remember that if you want to avoid echo:
Do NOT use sleep thread or the sleep()/usleep() functions to handle delays.
This issue is Linux/UNIX only.
)";
#endif // ! _WIN32
cout << "Press keys. ESC to exit.\n";
string msg = "";
while (true) {
if (kh::ispressed()) {
auto myKey = kh::getkey();
if (myKey >= key::f1 && myKey <= key::f10) {
msg = "[Fn Key]";
} else {
switch (myKey) {
case key::esc:
return 0;
case key::up:
msg = "[UP]";
break;
case key::down:
msg = "[DOWN]";
break;
case key::left:
msg = "[LEFT]";
break;
case key::right:
msg = "[RIGHT]";
break;
case key::enter:
msg = "[ENTER]\n";
break;
default:
msg = "(" + to_string(myKey) + ")";
//keyAscii = static_cast<char>(myKey);
}
}
cout << msg;
}
//To show that program is not paused waiting for a key...
this_thread::sleep_for(std::chrono::milliseconds(200));
cout << "." << flush;
}
}