windows非阻塞编程
#include <conio.h>
#include <windows.h>
#include <iostream>
using namespace std;
int main()
{
while (!_kbhit()) //在没有接收到键盘事件的时候这个循环会一直存在
{
cout << "等待键盘点击!!" << endl;
Sleep(50);
}
char ch = _getch();
printf("\nKey struck was '%c'\n",ch);
system("pause");
}
linux非阻塞编程
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#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("%d\n\r",ch);
unsleep(1000);//进行休眠
}
if(ch == 3){
system(STTY_DEF TTY_PATH);
return 0;
}
}
}