//------------------------------------------------------------------- // rawtty.cpp // // This program sets the terminal into 'raw' mode -- until the // user hits the -key. (The numerical value for a key // that is pressed is shown in hexadecimal and ascii formats.) // // compile using: $ make rawtty // execute using: $ rawtty // // programmer: ALLAN CRUSE // written on: 22 SEP 2003 // revised on: 20 SEP 2005 //------------------------------------------------------------------- #include // for printf(), perror() #include // for STDIN_FILENO #include // for tcgetattr(), tcsetattr() int main( int argc, char **argv ) { // save a copy of the current terminal's default settings struct termios orig_termios; tcgetattr( STDIN_FILENO, &orig_termios ); // install desired changes using a work-copy of settings struct termios work_termios = orig_termios; work_termios.c_lflag &= ~ISIG; work_termios.c_lflag &= ~ECHO; work_termios.c_lflag &= ~ICANON; work_termios.c_cc[ VTIME ] = 0; work_termios.c_cc[ VMIN ] = 1; tcsetattr( STDIN_FILENO, TCSAFLUSH, &work_termios ); // perform some keyboard input using 'raw' terminal mode printf( "\nPress any keys. Hit to quit.\n\n" ); int done = 0; while ( !done ) { int inch = 0; read( STDIN_FILENO, &inch, sizeof( inch ) ); if ( inch == '\e' ) done = 1; printf( "inch=%08X ", inch ); if (( inch >= 0x20 )&&( inch <= 0x7E )) printf( "\'%c\' ", inch ); printf( "\n" ); } printf( "\n" ); // restore the terminal's original default settings tcsetattr( STDIN_FILENO, TCSAFLUSH, &orig_termios ); }