Ubuntu键盘响应程序

touch test.cpp
gedit test.cpp

test.cpp内容:

#include 
#include   
#include   
#include   
#include   
int kbhit(void)  
{  
      struct termios oldt, newt;  
      int ch;  
      int oldf;  
      tcgetattr(STDIN_FILENO, &oldt);  
      newt = oldt;  
      newt.c_lflag &= ~(ICANON | ECHO);  
      tcsetattr(STDIN_FILENO, TCSANOW, &newt);  
      oldf = fcntl(STDIN_FILENO, F_GETFL, 0);  
      fcntl(STDIN_FILENO, F_SETFL, oldf | O_NONBLOCK);  
      ch = getchar();  
      tcsetattr(STDIN_FILENO, TCSANOW, &oldt);  
      fcntl(STDIN_FILENO, F_SETFL, oldf);  
      if(ch != EOF)  
      {  
        ungetc(ch, stdin);  
        return 1;  
      }  
      return 0; 
}  
int main(void)
{  
      while(!kbhit())
      {
            while(1)
            { 
              if('r' == getchar()){printf("r\n");}
              else if('t' == getchar()){printf("t\n");}
              else if('u' == getchar()){printf("u\n");}
            }
     } 
     return 0;  
}  

编译:

gcc test.cpp

你可能感兴趣的:(linux)