参考博客
#include二、#include #include #define TTY_PATH "/dev/tty" #define STTY_US "stty raw -echo -F " #define STTY_DEF "stty -raw echo -F " int get_char() { fd_set rfds; struct timeval tv; int ch = 0; FD_ZERO(&rfds); FD_SET(0, &rfds); tv.tv_sec = 0; tv.tv_usec = 10; //设置等待超时时间 if (select(1, &rfds, NULL, NULL, &tv) > 0) //检测键盘是否有输入 { ch = getchar(); } return ch; } int main() { int ch = 0; system(STTY_US TTY_PATH); while(1){ ch = get_char(); if(ch != 0) { printf("%dnr",ch); } if(ch == 3){ system(STTY_DEF TTY_PATH); return 0; } } }
参考博客
#include三、#include //键盘检测函数 int scanKeyboard() { int in; struct termios new_settings; struct termios stored_settings; tcgetattr(0,&stored_settings); new_settings = stored_settings; new_settings.c_lflag &= (~ICANON); new_settings.c_cc[VTIME] = 0; tcgetattr(0,&stored_settings); new_settings.c_cc[VMIN] = 1; tcsetattr(0,TCSANOW,&new_settings); in = getchar(); tcsetattr(0,TCSANOW,&stored_settings); return in; } int main(int argc, char **argv) { while(1) { int ascii = scanKeyboard(); printf(":%dn",ascii); if(ascii==27) break; } return 0; }
#include#include #include bool kbhit() { termios term; tcgetattr(0, &term); termios term2 = term; term2.c_lflag &= ~ICANON; tcsetattr(0, TCSANOW, &term2); int byteswaiting; ioctl(0, FIONREAD, &byteswaiting); tcsetattr(0, TCSANOW, &term); // std::cout< 0; } int main(int argc, char **argv) { int i; int c; // Check for keyboard input while (true) { if (kbhit()) { c = fgetc(stdin); std::cout < case 'h': std::cout << "you input h"<



