O_NONBLOCK,fcntl 非阻塞io读取键盘鼠标

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;
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值