-
Notifications
You must be signed in to change notification settings - Fork 0
/
kbhit.c
executable file
·66 lines (55 loc) · 1.43 KB
/
kbhit.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
55
56
57
58
59
60
61
62
63
64
65
66
#include "kbhit.h"
#include <sys/select.h>
#include <stdlib.h> // para poder usar NULL
#include <stdio.h> // para poder usar getc, printf...
//#define DEBUG
#include "dprintf.h" // para poder usar DPRINTF
static char ch2=0;
int kbread(void)
{
char ch=ch2;
DPRINTF("kbread '%c'\n",ch2);
ch2=0;
return ch;
}
int kbhit(void)
{
struct timeval tv;
fd_set read_fd;
if (ch2)
{
DPRINTF("kbhit buffer '%c'\n",ch2);
return ch2;
}
system ("/bin/stty raw");
/* Do not wait at all, not even a microsecond */
tv.tv_sec=0;
tv.tv_usec=0;
/* Must be done first to initialize read_fd */
FD_ZERO(&read_fd);
/* Makes select() ask if input is ready:
* 0 is the file descriptor for stdin */
FD_SET(0,&read_fd);
/* The first parameter is the number of the
* largest file descriptor to check + 1. */
if(select(1, &read_fd,NULL, /*No writes*/NULL, /*No exceptions*/&tv) == -1)
{
system ("/bin/stty cooked");
return 0; /* An error occured */
}
/* read_fd now holds a bit map of files that are
* readable. We test the entry for the standard
* input (file 0). */
if(FD_ISSET(0,&read_fd))
/* Character pending on stdin */
{
ch2=getc(stdin);
system ("/bin/stty cooked");
DPRINTF("kbhit '%c'\n",ch2);
// ungetc(ch2,stdin);
return ch2;
}
/* no characters were pending */
system ("/bin/stty cooked");
return 0;
}