1#include <stdio.h>2#include <stdlib.h>3#include <string.h>4#include <termios.h>5#include <term.h>6#include <curses.h>7#include <unistd.h>8 Static structTermios initial_settings,new_settings;9 Static intpeek_character=-1;Ten voidInit_keyboard (); One voidClose_keyboard (); A intCheck_keyboard (); - intreadch (); - intMain () the { - intCh=0; - Init_keyboard (); - while(ch!='Q') + { -printf"looping\n"); +Sleep1); A if(Check_keyboard ()) at { -Ch=readch (); - ///scanf ("%c", &ch); -printf"You hit %c\n", ch); - } - } in Close_keyboard (); -Exit0); to } + - intreadch () the { * Charch; $ if(peek_character!=-1)Panax Notoginseng { -Ch=Peek_character; thepeek_character=-1; + returnch; A } theRead0, &ch,1); + returnch; - } $ $ voidInit_keyboard () - { -Tcgetattr (0,&initial_settings); thenew_settings=initial_settings; -New_settings.c_lflag &=~Icanon;WuyiNew_settings.c_lflag &=~ECHO; theNew_settings.c_lflag &=~Isig; -new_settings.c_cc[vtime]=0; Wunew_settings.c_cc[vmin]=1; -Tcsetattr (0,tcsanow,&new_settings); About } $ - voidClose_keyboard () - { -Tcsetattr (0,tcsanow,&initial_settings); A } + the intCheck_keyboard () - { $ Charch; the intnread; the if(peek_character!=-1) the { the return 1; - } innew_settings.c_cc[vmin]=0; theTcsetattr (0,tcsanow,&new_settings); theNread=read (0, &ch,1); Aboutnew_settings.c_cc[vmin]=1; theTcsetattr (0,tcsanow,&new_settings); the if(nread==1) the { +Peek_character=ch; - return 1; the }Bayi return 0; the}
, press Q to exit
Linux record keyboard