Main Page
Related Pages
Modules
Namespaces
Classes
Files
File List
File Members
KNI_4.3.0
demo
common
demo/common/keyboard.cpp
Go to the documentation of this file.
1
#include "
keyboard.h
"
2
#include <termios.h>
3
#include <unistd.h>
// for read()
4
5
static
struct
termios initial_settings,
new_settings
;
6
static
int
peek_character
= -1;
7
8
void
init_keyboard
()
9
{
10
tcgetattr(0,&initial_settings);
11
new_settings
= initial_settings;
12
new_settings
.c_lflag &= ~ICANON;
13
new_settings
.c_lflag &= ~ECHO;
14
new_settings
.c_lflag &= ~ISIG;
15
new_settings
.c_cc[VMIN] = 1;
16
new_settings
.c_cc[VTIME] = 0;
17
tcsetattr(0, TCSANOW, &
new_settings
);
18
}
19
20
void
close_keyboard
()
21
{
22
tcsetattr(0, TCSANOW, &initial_settings);
23
}
24
25
int
kbhit
()
26
{
27
unsigned
char
ch;
28
int
nread;
29
30
if
(
peek_character
!= -1)
return
1;
31
new_settings
.c_cc[VMIN]=0;
32
tcsetattr(0, TCSANOW, &
new_settings
);
33
nread = read(0,&ch,1);
34
new_settings
.c_cc[VMIN]=1;
35
tcsetattr(0, TCSANOW, &
new_settings
);
36
if
(nread == 1)
37
{
38
peek_character
= ch;
39
return
1;
40
}
41
return
0;
42
}
43
44
int
readch
()
45
{
46
char
ch;
47
48
if
(
peek_character
!= -1)
49
{
50
ch =
peek_character
;
51
peek_character
= -1;
52
return
ch;
53
}
54
read(0,&ch,1);
55
return
ch;
56
}
57
58
int
_getch
() {
59
init_keyboard
();
60
kbhit
();
61
char
c =
readch
();
62
close_keyboard
();
63
return
c;
64
}
readch
int readch()
Definition:
demo/common/keyboard.cpp:44
new_settings
static struct termios initial_settings new_settings
Definition:
demo/common/keyboard.cpp:5
close_keyboard
void close_keyboard()
Definition:
demo/common/keyboard.cpp:20
kbhit
int kbhit()
Definition:
demo/common/keyboard.cpp:25
keyboard.h
init_keyboard
void init_keyboard()
Definition:
demo/common/keyboard.cpp:8
_getch
int _getch()
Definition:
demo/common/keyboard.cpp:58
peek_character
static int peek_character
Definition:
demo/common/keyboard.cpp:6
kni
Author(s): Martin Günther
autogenerated on Fri Jun 7 2019 22:06:44