-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconsolekbd.cc
47 lines (34 loc) · 872 Bytes
/
consolekbd.cc
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
#include <stdio.h>
#include <termios.h>
#include <unistd.h>
class ConsoleKeyboard {
public:
static ConsoleKeyboard & instantiate() {
static ConsoleKeyboard theKbd;
return(theKbd);
}
int getchar(); // unsigned char if any keypress, otherwise -1
~ConsoleKeyboard();
private:
struct termios oldt, newt;
ConsoleKeyboard();
};
ConsoleKeyboard::ConsoleKeyboard() {
// Set non-canonical mode, so we can read keypresses directly.
tcgetattr(STDIN_FILENO, &oldt);
newt = oldt;
newt.c_lflag &= ~( ICANON | ECHO | ECHONL |IEXTEN);
newt.c_cc[VTIME] = 0;
newt.c_cc[VMIN] = 0;
tcsetattr( STDIN_FILENO, TCSANOW, &newt);
}
ConsoleKeyboard::~ConsoleKeyboard() {
// Return to old mode.
tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
}
int ConsoleKeyboard::getchar() {
static char line[2];
if (read (0, line, 1))
return(line[0]);
else return(-1);
}