Pol
Revision:cb584c9
Main Page
Related Pages
Modules
Namespaces
Classes
Files
File List
File Members
kbhit.cpp
Go to the documentation of this file.
1
// History
2
// 2006/10/07 Shinigami: FreeBSD fix - changed __linux__ to __unix__
3
4
// this file only used in Linux build
5
#ifdef LINUX
6
7
#include "
kbhit.h
"
8
#include <unistd.h>
// read()
9
namespace
Pol
10
{
11
namespace
Clib
12
{
13
KeyboardHook::KeyboardHook
()
14
{
15
tcgetattr( 0, &initial_settings );
16
new_settings
= initial_settings;
17
new_settings
.c_lflag &= ~ICANON;
18
new_settings
.c_lflag &= ~ECHO;
19
new_settings
.c_lflag |= ISIG;
// enable signals, like ctrl-c
20
new_settings
.c_cc[VMIN] = 1;
21
new_settings
.c_cc[VTIME] = 0;
22
tcsetattr( 0, TCSANOW, &
new_settings
);
23
peek_character
= -1;
24
}
25
26
KeyboardHook::~KeyboardHook
()
27
{
28
tcsetattr( 0, TCSANOW, &initial_settings );
29
}
30
31
int
KeyboardHook::kbhit
()
32
{
33
if
(
peek_character
!= -1 )
34
return
1;
35
36
new_settings
.c_cc[VMIN] = 0;
37
tcsetattr( 0, TCSANOW, &
new_settings
);
38
unsigned
char
ch;
39
int
nread = read( 0, &ch, 1 );
40
new_settings
.c_cc[VMIN] = 1;
41
tcsetattr( 0, TCSANOW, &
new_settings
);
42
43
if
( nread == 1 )
44
{
45
peek_character
= ch;
46
return
1;
47
}
48
return
0;
49
}
50
51
int
KeyboardHook::getch
()
52
{
53
char
ch;
54
55
if
(
peek_character
!= -1 )
56
{
57
ch =
peek_character
;
58
peek_character
= -1;
59
}
60
else
61
{
62
int
numread = read( 0, &ch, 1 );
63
if
( numread != 1 )
64
throw
;
65
}
66
67
return
ch;
68
}
69
}
70
}
71
72
#endif
Pol::Clib::KeyboardHook::new_settings
struct termios initial_settings new_settings
Definition:
kbhit.h:21
Pol::Clib::KeyboardHook::~KeyboardHook
~KeyboardHook()
Pol::Clib::KeyboardHook::KeyboardHook
KeyboardHook()
Pol::Clib::KeyboardHook::getch
int getch()
kbhit.h
Pol::Clib::KeyboardHook::peek_character
int peek_character
Definition:
kbhit.h:22
Pol::Clib::KeyboardHook::kbhit
int kbhit()
Pol
Definition:
berror.cpp:12
clib
kbhit.cpp
Generated on Wed Oct 10 2018 02:41:48 for Pol by
1.8.11