您的位置:首页 > 运维架构 > Linux

linux c/c++监听鼠标或键盘事件

2015-12-18 11:54 811 查看
linux c/c++监听鼠标或键盘事件
1、输入设备在文件/proc/bus/input/devices中,如:



其中Handlers=kbd event2,说明其值可以在/dev/input文件夹的event2文件中读到,文件夹内容:



2、可能是受虚拟机影响,鼠标事件并不能从mousex系列的文件中读到,而是event3,键盘是event2,完整程序如下:

#include <stdio.h>
#include <iostream>
#include <linux/input.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

int main(int argc,char** argv)
{
int keys_fd;
char ret[2];
struct input_event t;
keys_fd=open(argv[1],O_RDONLY);
if(keys_fd<=0)
{
printf("error\n");
return -1;
}
while(1)
{
read(keys_fd,&t,sizeof(struct input_event));
if(t.type==1)
printf("key %i state %i \n",t.code,t.value);
}
close(keys_fd);
return 0;
}


3、键盘事件效果:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: