/*---------------------------------------------------------------- *\ file: consolekeyloop.c author: mathias gumz start: 031204 16:45:33 about: shows how to read keypress from stdin without waiting til the final enter comes... \*---------------------------------------------------------------- */ #include #include #include #include #include #include #include #include struct timeval tv; int retval; fd_set rfds; char buf; static struct termios new_termios; static struct termios old_termios; void quietterm() { tcgetattr(fileno(stdin), &old_termios); new_termios = old_termios; new_termios.c_iflag &= ~(ISTRIP|IXON|IXOFF); new_termios.c_lflag &= ~(ICANON|ISIG|IEXTEN|ECHO); new_termios.c_cc[VMIN] = 1; new_termios.c_cc[VTIME] = 0; tcsetattr(0, TCSANOW, &new_termios); return; } void resetterm() { tcsetattr(0, TCSANOW, &old_termios); return; } int main() { FD_ZERO(&rfds); FD_SET(fileno(stdin), &rfds); quietterm(); tv.tv_sec= 1; tv.tv_usec= 0; while(1) { FD_SET(fileno(stdin), &rfds); retval= pselect(1, &rfds, NULL, NULL, &tv, NULL); if ( retval > 0) { read(0, &buf, 1); switch(buf) { case 'q': resetterm(); printf("exit\n"); exit(0); break; default: printf("pressed: %d\r", (int)buf); fflush(stdout); break; } } else if ( retval == 0 ) printf("nothing pressed %d\n", retval); else printf("error\n"); fflush(stdin); }; resetterm(); return 0; }