In other words, it's just a keyboard test that's good trouble. I still know too little ...
#include <stdio.h> #include <stdlib.h> #include <unistd.h> #include < errno.h> #include <fcntl.h> #include <stdio.h> #include <termios.h> #include <unistd.h> #include <string.h> #define key_esc 27void init_keyboard (); void close_keyboard (); Int kbhit (); Int readch (); /* correlation function Declaration */static struct termios initial_settings, new_settings;static int peek_character = -1; /* a function to test if a key is pressed *//* detects keyboard keys */int kbhit () { char ch; int nread; if ( peek_character != -1 ) return (1); new_settings.c_cc[vmin] = 0; tcsetattr ( 0, tcsanow, &new_settings ) nread = read ( 0, &ch, 1 ); new_settings.c_cc[vmin] = 1; tcsetattr ( 0, tcsanow, &new_settings ); if ( nread == 1 ) { peek_character = ch; return (1); } return (0) ;} The/* is used to receive the pressed key and peek_character = -1 the recovery status */int readch () { char ch; if ( peek_character != -1 ) { ch = peek_character; peek_character = -1; return (CH); } read ( 0, &ch, 1 ); return (ch);} /* Configuration terminal function */void init_keyboard () { tcgetattr ( 0, &initial_ settings ); new_settings = initial_settings; new_ settings.c_lflag &= ~icanon; new_settings.c_lflag &= ~echo; new_settings.c_lflag &= ~isig; new_settings.c_cc[ vmin] = 1; new_settings.c_cc[vtime] = 0; Tcsetattr ( 0, TCSANOW, &new_settings );} Void close_keyboard () { tcsetattr ( 0, tcsanow, &initial_settings );} Int main (int argc, char const *argv[]) { int ch = 0; init_keyboard (); printf ( "you can put esc to quit!\n" ); while ( ch != 27 ) { if ( kbhit () ) { &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;CH&NBSP;=&NBSP;READCH (); if ( ch != 27 ) printf ( "you put %c ! only put esc can quit! \n ", ch ); } } close_keyboard (); return 0;}
Reference:
http://blog.163.com/liang_w_yan/blog/static/210657088201332444016981/
Keyboard detection under Linux