#ifdef __MSDOS__ #include #else #include #include #include #include #include int kbhit(void) { char c; static struct termios new_term; static struct termios old_term; static int handle; static int init = 0; fflush(stdout); if (! init) { init = 1; if ((handle = open("/dev/tty", O_RDWR|O_SYNC)) == -1) { #if 0 fputs("kbhit() ERROR: Cannot read from terminal!\n",stderr); #endif return -1; } else { tcgetattr(handle, &old_term); new_term = old_term; new_term.c_lflag &= ~ICANON; new_term.c_lflag &= ~ECHO; new_term.c_cc[VMIN] = 0; new_term.c_cc[VTIME] = 1; } } tcsetattr(handle, TCSANOW, &new_term); if (read(handle, &c, 1) <= 0) c = 0; else ungetc(c, stdin); tcsetattr(handle, TCSANOW, &old_term); return c; } #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