-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathgetchInLinux.h
More file actions
executable file
·45 lines (40 loc) · 947 Bytes
/
getchInLinux.h
File metadata and controls
executable file
·45 lines (40 loc) · 947 Bytes
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
#include <termios.h>
#include <stdio.h>
static struct termios old, new1;
/* Initialize new1 terminal i/o settings */
void initTermios(int echo)
{
tcgetattr(0, &old); /* grab old terminal i/o settings */
new1 = old; /* make new1 settings same as old settings */
new1.c_lflag &= ~ICANON; /* disable buffered i/o */
if (echo) {
new1.c_lflag |= ECHO; /* set echo mode */
} else {
new1.c_lflag &= ~ECHO; /* set no echo mode */
}
tcsetattr(0, TCSANOW, &new1); /* use these new1 terminal i/o settings now */
}
/* Restore old terminal i/o settings */
void resetTermios(void)
{
tcsetattr(0, TCSANOW, &old);
}
/* Read 1 character - echo defines echo mode */
char getch_(int echo)
{
char ch;
initTermios(echo);
ch = getchar();
resetTermios();
return ch;
}
/* Read 1 character without echo */
char getchInLinux()
{
return getch_(0);
}
/* Read 1 character with echo */
char getche(void)
{
return getch_(1);
}