收藏 分销(赏)

linux 设备输入子系统---源代码示例自动捕获键盘鼠标等外设消息.docx

上传人:s4****5z 文档编号:8756114 上传时间:2025-03-01 格式:DOCX 页数:10 大小:128.65KB 下载积分:10 金币
下载 相关 举报
linux 设备输入子系统---源代码示例自动捕获键盘鼠标等外设消息.docx_第1页
第1页 / 共10页
linux 设备输入子系统---源代码示例自动捕获键盘鼠标等外设消息.docx_第2页
第2页 / 共10页


点击查看更多>>
资源描述
Linux input 子系统详解与代码示例 李邦柱于杭州2014/01/09 Email:helpylee@ 由于linux 的驱动模型增加了input层,导致几乎所有的底层驱动都把数据封装在event里上报给input子系统。由此看来,这种改变让kernel 更具有模块化,各个模块的耦合度更低了。 下面我们一起来研究input 层^_^ 1. 从用户层的角度看input(event 事件) 了解linux的人一定会对/dev,/ sys, /proc这几个目录有所印象,这是从内核导出到用户层的接口(从这里几乎可以观览内核)。kernel为我们导出了input在用户态的接口,就是/dev/input/下的接口,所以我们只关注这个目录下的event*(event0/event1/……)字符设备。 那么这些event*是干什么用的?简单来说就是我们对计算机的输入(包括敲击键盘,移动鼠标等等操作)经过内核(底层驱动,input)处理最后就上报到这些event*里面了。  而这里event0,event1,..就是用来区分各个外设的,可以通过命令来查看外设具体和哪个event相关联: 这个命令是:cat /proc/bus/input/devices  所以我们用此命令在linux系统查看外设信息。 2. 在linux/input.h中有这些数据的结构: struct input_event {          struct timeval time; //事件发生的时间          __u16 type; //事件类类型:按键和移动鼠标就是不同类型          __u16 code;          __s32 value; //事件值:按键a和按键b就对应不同值 }; code: 事件的代码.如果事件的类型代码是EV_KEY,该代码code为设备键盘代码.代码植0~127为键盘上的按键代码,0x110~0x116 为鼠标上按键代码,其中0x110(BTN_ LEFT)为鼠标左键,0x111(BTN_RIGHT)为鼠标右键,0x112(BTN_ MIDDLE)为鼠标中键.其它代码含义请参看include/linux/input.h文件. 如果事件的类型代码是EV_REL,code值表示轨迹的类型.如指示鼠标的X轴方向REL_X(代码为0x00),指示鼠标的Y轴方向REL_Y(代码 为0x01),指示鼠标中轮子方向REL_WHEEL(代码为0x08). type:  EV_KEY,键盘 EV_REL,相对坐标 EV_ABS,绝对坐标 value: 事件的值.如果事件的类型代码是EV_KEY,当按键按下时值为1,松开时值为0;如果事件的类型代码是EV_ REL,value的正数值和负数值分别代表两个不同方向的值. /* * Event types */ #define EV_SYN 0x00 #define EV_KEY 0x01 //按键 #define EV_REL 0x02 //相对坐标(轨迹球) #define EV_ABS 0x03 //绝对坐标 #define EV_MSC 0x04 //其他 #define EV_SW 0x05 #define EV_LED 0x11 //LED #define EV_SND 0x12//声音 #define EV_REP 0x14//repeat #define EV_FF 0x15  #define EV_PWR 0x16 #define EV_FF_STATUS 0x17 #define EV_MAX 0x1f #define EV_CNT (EV_MAX+1) 这里事件指的是我们对外设的操作,比如按键一次a可能就产生数个input_event数据 3.代码示例: 此代码可以完全正确编译运行。把此代码拷贝到xx.c 文件中,gcc xx.c –o xx 后要用sudo 去执行。 Sudo ./xx 此程序的功能是可以及时捕获键盘消息,鼠标消息和其他外设出发的消息。比如可以记录键盘事件。下图就是此程序运行后 成功捕获到键盘和鼠标消息的截图。 本程序采用I/O多路复用技术。 #include<stdlib.h> #include <stdio.h> #include <sys/time.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <unistd.h> #include <linux/input.h> #include<string.h> int b_cur_x = 0, b_cur_y = 0; typedef enum { BlcTermKeyDevStat_Default = 1, // 所有键值的默认状态或鼠标的坐标移动状态 BlcTermKeyDevStat_Down = 2, // 所有键值的按下状态 BlcTermKeyDevStat_Up = 3, // 所有键值的弹起状态 BlcTermKeyDevStat_MouseWheel = 4, // 鼠标滚轮转动 BlcTermKeyDevStat_ConsoleLeftAXIS = 5, // 游戏手柄左摇杆坐标操作 BlcTermKeyDevStat_ConsoleRightAXIS = 6, // 游戏手柄右摇杆坐标操作 BlcTermKeyDevStat_MouseDoubleClick = 7, // 鼠标左键双击 }BlcTermKeyDevStat; typedef enum { BlcPlayCtl_EventID_Login = 3001, BlcPlayCtl_EventID_Logout = 3002, BlcDmxFilter_EventID_TSData = 3003, BlcKey_EventID_KeyValue = 3004, BlcPlayCtl_EventID_MwReturn = 3005, BlcRes_EventID_ResoureRequest = 3006, BlcRes_EventID_ServiceNotice = 3007, BlcPlayCtl_EventID_ConnectServer = 3008, BlcKey_EventID_KeyDevPlug = 3101, } Blc_EventID; typedef enum { BlcKeyDevPlugType_KeyboardPlugin = 1, BlcKeyDevPlugType_KeyboardPlugout = 2, BlcKeyDevPlugType_MousePlugin = 3, BlcKeyDevPlugType_MousePlugout = 4, BlcKeyDevPlugType_ConsolePlugin = 5, BlcKeyDevPlugType_ConsolePlugout = 6, } BlcKeyDevPlugType; typedef enum { BlcTermKeyDevType_Irr = 1, BlcTermKeyDevType_Keyboard = 2, BlcTermKeyDevType_Mouse = 3, BlcTermKeyDevType_Console = 4, } BlcTermKeyDevType; typedef enum { BlcMousePropertyValue_DEFAULT = 0, BlcMousePropertyValue_BUTTONLEFT = 1, BlcMousePropertyValue_BUTTONMIDDLE = 2, BlcMousePropertyValue_BUTTONRIGHT = 3, } BlcMouseKeyValue; typedef struct { int keydev; //键值设备类型 int keystate; // 键值状态; int keyvalue; // 键值 short x; //鼠标的x坐标 short y; //鼠标的y坐标 } BlcKeyValueData; struct input_event ev_temp; int func(int fd) { BlcKeyValueData key_info; int count; int plug_flag = BlcKeyDevPlugType_MousePlugout; // struct input_event ev_temp; if(fd != -1) { plug_flag = BlcKeyDevPlugType_MousePlugin; } char up[] = "抬起"; char down[] = "按下"; //while(1) count = read(fd, &ev_temp, sizeof(struct input_event)); if(count > 0) { if(ev_temp.type == EV_SYN) return 1; printf("$$$$$$$$$$$$$$$: %d, %d, %d\n", ev_temp.type, ev_temp.code, ev_temp.value); printf("key:%d ", ev_temp.code); printf("%s\n", ev_temp.value?down:up); memset(&key_info, 0 , sizeof(BlcKeyValueData)); if (ev_temp.type == EV_KEY) { if (ev_temp.code >= 0 && ev_temp.code <= 127) { key_info.keydev = BlcTermKeyDevType_Keyboard; key_info.keyvalue = ev_temp.value; } else if (ev_temp.code >= 0x110 && ev_temp.code <= 0x116) { key_info.keydev = BlcTermKeyDevType_Mouse; if (ev_temp.code == 0x110) key_info.keyvalue = BlcMousePropertyValue_BUTTONLEFT; else if (ev_temp.code == 0x111) key_info.keyvalue = BlcMousePropertyValue_BUTTONRIGHT; else if (ev_temp.code == 0x112) key_info.keyvalue = BlcMousePropertyValue_BUTTONMIDDLE; } if (ev_temp.value == 1) key_info.keystate = BlcTermKeyDevStat_Down; else if (ev_temp.value == 0) key_info.keystate = BlcTermKeyDevStat_Up; key_info.x = -1; key_info.y = -1; } else if (ev_temp.type == EV_REL) { key_info.keydev = BlcTermKeyDevType_Mouse; key_info.keystate = BlcTermKeyDevStat_Default; key_info.keyvalue = 0; if (ev_temp.code == REL_X) b_cur_x += ev_temp.value; else if (ev_temp.code == REL_Y) b_cur_y += ev_temp.value; else if (ev_temp.code == REL_WHEEL) ;//???//key_info.keyvalue = ev_temp.code; if (b_cur_x < 0) b_cur_x = 0; if (b_cur_x > 1280) b_cur_x = 1280; if (b_cur_y < 0) b_cur_y = 0; if (b_cur_y > 720) b_cur_y = 720; key_info.x = b_cur_x; key_info.y = 0 - b_cur_y; } printf("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@: %d, %d, %d\n", key_info.keydev, key_info.keyvalue, key_info.keyvalue); } else if (count == -1) { if (plug_flag == BlcKeyDevPlugType_MousePlugin) { close(fd); plug_flag = BlcKeyDevPlugType_MousePlugout; printf("plug out !\n"); } if (fd != -1) { plug_flag = BlcKeyDevPlugType_MousePlugin; printf("plug int ok !\n"); } else { printf("plug int fail !\n"); usleep(500); } } //} } int main(void) { int fd_key,fd_ts, fd_led, fd_max, fd_0, fd_2, fd_3; struct input_event event_key,event_ts; int dac_value; struct timeval select_timeout; fd_set readfds; select_timeout.tv_sec = 20; select_timeout.tv_usec = 0; fd_0 = open("/dev/input/event0", O_RDWR); fd_key = open("/dev/input/event1", O_RDWR); fd_2 = open("/dev/input/event2", O_RDWR); fd_3 = open("/dev/input/event3", O_RDWR); fd_ts = open("/dev/input/event4", O_RDWR); if (fd_key>fd_ts) fd_max = fd_key; else fd_max = fd_ts; while(1) { FD_ZERO(&readfds); FD_SET(fd_key, &readfds); FD_SET(fd_ts, &readfds); FD_SET(fd_0, &readfds); FD_SET(fd_2, &readfds); FD_SET(fd_3, &readfds); select_timeout.tv_sec= 2; select_timeout.tv_usec = 0; select(fd_max + 1, &readfds, NULL, NULL, &select_timeout); if(FD_ISSET(fd_0, &readfds)) { printf("come from fd_0\n"); func(fd_0); } if(FD_ISSET(fd_2, &readfds)) { printf("come from fd_2\n"); func(fd_2); } if(FD_ISSET(fd_3, & readfds)) { printf("come from fd_3\n"); func(fd_3); } if ( FD_ISSET(fd_key, &readfds)) { printf("come from keyborad\n"); func(fd_key); } else if(FD_ISSET(fd_ts, &readfds)) { printf("come from mouse\n"); func(fd_ts); } printf("|\n|\n"); } }
展开阅读全文

开通  VIP会员、SVIP会员  优惠大
下载10份以上建议开通VIP会员
下载20份以上建议开通SVIP会员


开通VIP      成为共赢上传

当前位置:首页 > 百科休闲 > 其他

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        抽奖活动

©2010-2026 宁波自信网络信息技术有限公司  版权所有

客服电话:0574-28810668  投诉电话:18658249818

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :微信公众号    抖音    微博    LOFTER 

客服