Diff for libplaton/platon/misc/kbhit.c between version 1.2 and 1.3
| version 1.2, 2001/10/05 18:14:54 |
version 1.3, 2002/02/04 18:38:11 |
|
|
| #ifdef __MSDOS__ |
#ifdef __MSDOS__ |
| |
|
| #include <conio.h> |
#include <conio.h> |
| |
|
| #else |
#else |
| |
|
| #include<stdio.h> |
#include<stdio.h> |
| #include<fcntl.h> |
#include<fcntl.h> |
| #include<termios.h> |
#include<termios.h> |
|
|
| |
|
| #include "kbhit.h" |
#include "kbhit.h" |
| |
|
| int kbhit(void){ |
int kbhit(void) |
| |
{ |
| char c; |
char c; |
| static struct termios new_term; |
static struct termios new_term; |
| static struct termios old_term; |
static struct termios old_term; |
| static int handle; |
static int handle; |
| static int init=0; |
static int init = 0; |
| |
|
| fflush(stdout); |
fflush(stdout); |
| |
|
| if(!init){ |
if (! init) { |
| init=1; |
init = 1; |
| if((handle=open("/dev/tty",O_RDWR|O_SYNC))==-1){ |
if ((handle = open("/dev/tty", O_RDWR|O_SYNC)) == -1) { |
| #if 0 |
#if 0 |
| fputs("kbhit() ERROR: Cannot read from terminal!\n",stderr); |
fputs("kbhit() ERROR: Cannot read from terminal!\n",stderr); |
| #endif |
#endif |
| return -1; |
return -1; |
| } |
} |
| else { |
else { |
| tcgetattr(handle,&old_term); |
tcgetattr(handle, &old_term); |
| new_term=old_term; |
new_term = old_term; |
| new_term.c_lflag&=~ICANON; |
new_term.c_lflag &= ~ICANON; |
| new_term.c_lflag&=~ECHO; |
new_term.c_lflag &= ~ECHO; |
| new_term.c_cc[VMIN]=0; |
new_term.c_cc[VMIN] = 0; |
| new_term.c_cc[VTIME]=1; |
new_term.c_cc[VTIME] = 1; |
| } |
} |
| } |
} |
| |
|
| tcsetattr(handle,TCSANOW,&new_term); |
tcsetattr(handle, TCSANOW, &new_term); |
| if(read(handle,&c,1)<=0)c=0; |
|
| else ungetc(c,stdin); |
if (read(handle, &c, 1) <= 0) |
| tcsetattr(handle,TCSANOW,&old_term); |
c = 0; |
| |
else |
| |
ungetc(c, stdin); |
| |
|
| return(c); |
tcsetattr(handle, TCSANOW, &old_term); |
| |
|
| |
return c; |
| } |
} |
| |
|
| #endif |
#endif |
| |
|
| |
#if defined(SELF) || defined(SELFTEST) || defined(SELF_KBHIT) |
| |
|
| |
int main(void) |
| |
{ |
| |
int c; |
| |
|
| |
while (1) { |
| |
putchar('<'); |
| |
|
| |
if (kbhit() != 0) { |
| |
c = getchar(); |
| |
printf("\nPressed key ('z' to quit): %d [%c]\n", c, c); |
| |
|
| |
if (c == 'z') |
| |
break; |
| |
} |
| |
} |
| |
|
| |
putchar('>'); |
| |
|
| |
return 0; |
| |
} |
| |
|
| |
#endif |
| |
|
| |
|
Platon Group <platon@platon.org> http://platon.org/
|
|