1.fcntl
#include <unistd.h>
#include <fcntl.h>
int fcntl(int fd, int cmd, ... /* arg */ );
The file status flags and their semantics are described in open(2).
F_GETFL (void)
Get the file access mode and the file status flags; arg is
ignored.
F_SETFL (int)
Set the file status flags to the value specified by arg. File
access mode (O_RDONLY, O_WRONLY, O_RDWR) and file creation flags
(i.e., O_CREAT, O_EXCL, O_NOCTTY, O_TRUNC) in arg are ignored.
On Linux this command can change only the O_APPEND, O_ASYNC,
O_DIRECT, O_NOATIME, and O_NONBLOCK flags.
//O_NONBLOCK,fcntl 非阻塞io读取键盘鼠标
#include<stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
int main(void)
{
int fd=-1,ret=-1;
char buf[200]={0};
int flag=-1;
fd=open("/dev/input/mouse0",O_NONBLOCK | O_RDONLY);//将鼠标设置为非阻塞
if(fd<0)
{
perror("open");
return -1;
}
//将键盘设置为非阻塞
flag=fcntl(0,F_GETFL); //获取当前flag
flag |=O_NONBLOCK; //设置新falg
fcntl(0,F_SETFL,flag); //更新flag
while(1)
{
//读鼠标
memset(buf,0,sizeof(buf));
ret=read(fd,buf,sizeof(buf));
if(ret>0)
{
printf("鼠标读取为:[%s].\n",buf);
}
//读键盘
memset(buf,0,sizeof(buf));
ret=read(0,buf,sizeof(buf));
if(ret>0)
{
printf("键盘读取为:[%s].\n",buf);
}
}
return 0;
}
转载自原文链接, 如需删除请联系管理员。
原文链接:O_NONBLOCK,fcntl 非阻塞io读取键盘鼠标,转载请注明来源!