代码非常简单:
1 #include <stdio.h> 2 #include <termios.h> 3 #include <unistd.h> 4 #include <sys/types.h> 5 #include <sys/time.h> 6 7 int kbhit(void){ 8 struct timeval tv; 9 fd_set rdfs; 10 11 tv.tv_sec = 0; 12 tv.tv_usec = 0; 13 14 FD_ZERO(&rdfs); 15 FD_SET(STDIN_FILENO, &rdfs); 16 17 select(STDIN_FILENO + 1, &rdfs, NULL, NULL, &tv); 18 19 return FD_ISSET(STDIN_FILENO, &rdfs); 20 } 21 22 int main(int argc, char **argv) 23 { 24 int ch; 25 while(1){ 26 if(kbhit() != 0){ //如果键盘被敲击 27 ch = getchar(); //获取键值 28 printf("num: %d\n", ch); //打印键值 29 printf("char: %c\n", ch); //打印键符 30 } 31 } 32 33 return 0; 34 }
使用也很简单,不多赘述。
标签:ch,rdfs,tv,C语言,int,键值,linux,include From: https://www.cnblogs.com/guochaoxxl/p/16840509.html