Linux C编程连载【2】-鼠标

本文详细介绍了如何使用C语言通过打开'/dev/input/mice'设备文件来读取鼠标输入,并利用select函数实现定时选择,以及如何在不同情况下处理读取结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <sys/types.h> #include <fcntl.h> #include <time.h> #include <errno.h> int main(void){ int fd,retval; char buf[6]; fd_set readfds,tmp_readfds; struct timeval tv; if((fd=open("/dev/input/mice",O_RDONLY|O_NONBLOCK))<0){ printf("Fail to open /dev/input/mice!\n"); return -1; } printf("Open /dev/input/mice success!\n"); tv.tv_sec=5; tv.tv_usec=0; FD_ZERO(&readfds); FD_SET(fd,&readfds); FD_SET(0,&readfds); while(FD_ISSET(fd,&readfds)){ tmp_readfds=readfds; retval=select(fd+1,&readfds,NULL,NULL,&tv); printf("retval is %d.\n",&retval); switch(retval){ case -1: { printf("Select error\n"); return -1; } break; case 0: { printf("Time out\n"); return -1; } break; default:{ printf("Select OK!\n"); if(read(fd,buf,sizeof(buf))<=0) { printf("Fail to read!\n"); continue; } printf("Button type is %d, x=%d ,y=%d\n",buf[0]&0x07,buf[1],buf[2]); } break; }//end of switch }//end of while close(fd); return 0; }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值