-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlearn_keyboard.c~
executable file
·54 lines (44 loc) · 1.02 KB
/
learn_keyboard.c~
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
#include <termios.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int scan_keyboard(void)
{
struct termios oldstuff;
struct termios newstuff;
int inch;
tcgetattr(STDIN_FILENO, &oldstuff);
newstuff = oldstuff; /* save old attributes */
newstuff.c_lflag &= ~(ICANON | ECHO); /* reset "canonical" and "echo" flags*/
tcsetattr(STDIN_FILENO, TCSANOW, &newstuff); /* set new attributes */
inch = getchar();
tcsetattr(STDIN_FILENO, TCSANOW, &oldstuff); /* restore old attributes */
return inch;
}
int key_stroke()
{
int i;
ioctl(0, FIONREAD, &i);
return i; /* return a count of chars available to read */
}
void wait()
{
clock_t now = clock();
while((clock() - now) < (1 * CLOCKS_PER_SEC));
return;
}
int main()
{
int i;
char d;
for (; d != 97; ) {
printf("Looping ... \n");
wait();
if (key_stroke()) /* if any key is pressed */{
d = scan_keyboard(); /* scan keyboard for input */
}
}
return 0;
}