//头文件
#include <unistd.h> #include <fcntl.h>
#include <sys/select.h>
#include <stdlib.h>
void SetTerminal(int nFlag);
int KbHit(void);
//源文件
#include "KbHit.h" void SetTerminal(int nFlag) { if(nFlag) { system("stty cbreak -echo"); } else { system("stty cooked echo"); } } int KbHit(void) { struct timeval tv; fd_set fdSet; tv.tv_sec = 0; tv.tv_usec = 0; FD_ZERO(&fdSet); FD_SET(STDIN_FILENO, &fdSet); select(STDIN_FILENO+1, &fdSet, NULL, NULL, &tv); return FD_ISSET(STDIN_FILENO, &fdSet); }
//main
#include "KbHit.h"
int main()
{
SetTerminal(1);
while(!KbHit())
{
}
SetTerminal(0);
return 0;
} |