linux下获取按键响应事件

上传人:自*** 文档编号:79097913 上传时间:2019-02-16 格式:DOCX 页数:5 大小:48.18KB
返回 下载 相关 举报
linux下获取按键响应事件_第1页
第1页 / 共5页
linux下获取按键响应事件_第2页
第2页 / 共5页
linux下获取按键响应事件_第3页
第3页 / 共5页
linux下获取按键响应事件_第4页
第4页 / 共5页
linux下获取按键响应事件_第5页
第5页 / 共5页
亲,该文档总共5页,全部预览完了,如果喜欢就下载吧!
资源描述

《linux下获取按键响应事件》由会员分享,可在线阅读,更多相关《linux下获取按键响应事件(5页珍藏版)》请在金锄头文库上搜索。

1、linux下获取按键响应事件1、问题通过一个死循环将读取键盘对应的设备文件将触发键盘事件在屏幕上打印出来,按esc退出程序代码是在unbuntu10.04编译执行通过的2、input_event描述在Linux内核中,input设备用input_dev结构体描述,使用input子系统实现输入设备驱动的时候,驱动的核心工作就是向系统报告按键、触摸屏、键盘、鼠标等输入事件(event,通过input_event结构体描述),不再需要关心文件操作接口,因为Input子系统已经完成了文件操作接口linux/input.h 这个文件定义了event事件的结构体,API和标准按键的编码等;struct i

2、nput_event struct timeval time; /按键时间 _u16 type; /事件类型 _u16 code; /要模拟成什么按键 _s32 value;/是按下还是释放;type,指事件类型,常见的事件类型有:EV_KEY, 按键事件,如键盘的按键(按下哪个键),鼠标的左键右键(是非击下)等;EV_REL, 相对坐标,主要是指鼠标的移动事件(相对位移);EV_ABS, 绝对坐标,主要指触摸屏的移动事件code:事件的代码.如果事件的类型代码是EV_KEY,该代码code为设备键盘代码.0127为键盘上的按键代码关于按键代码的定义,部分如下所示:#define KEY_RE

3、SERVED 0#define KEY_ESC 1#define KEY_1 2#define KEY_2 3#define KEY_3 4#define KEY_4 5#define KEY_5 6#define KEY_6 7#define KEY_7 8#define KEY_8 9#define KEY_9 10#define KEY_0 11#define KEY_MINUS 12#define KEY_EQUAL 13#define KEY_BACKSPACE 14#define KEY_TAB 15#define KEY_Q 16#define KEY_W 17#define K

4、EY_E 18#define KEY_R 19#define KEY_T 20value:事件的值.如果事件的类型代码是EV_KEY,当按键按下时值为1,松开时值为0;如果事件的类型代码是EV_REL,value的正数值和负数值分别代表两个不同方向的值.3、相关代码key_simulator.cJava代码1. #include2. #include3. #include4. #include5. #include6. int main ()7. int keys_fd;8. char ret2;9. struct input_event t;10. keys_fd = open (/dev/

5、input/event2, O_RDONLY);11. if (keys_fd = 0)12. printf (open /dev/input/event2 device error!n);13. return 0;14. while (1)15. if (read (keys_fd, &t, sizeof (t) = sizeof (t)16. if (t.type = EV_KEY)17. if (t.value = 0 | t.value = 1)18. printf (key %d %sn, t.code,19. (t.value)?Pressed:Released);20. if(t

6、.code=KEY_ESC)21. break;22. close (keys_fd);23. return 0;#include#include#include#include#includeint main () int keys_fd; char ret2; struct input_event t; keys_fd = open (/dev/input/event2, O_RDONLY); if (keys_fd = 0) printf (open /dev/input/event2 device error!n); return 0; while (1) if (read (keys

7、_fd, &t, sizeof (t) = sizeof (t) if (t.type = EV_KEY) if (t.value = 0 | t.value = 1) printf (key %d %sn, t.code, (t.value)?Pressed : Released); if(t.code=KEY_ESC) break; close (keys_fd); return 0;4、注意问题1)不同的类型计算机,设备对应event信息会有所不同可以用cat获得的设备对应event信息:# cat /proc/bus/input/devicesI: Bus=0019 Vendor=00

8、00 Product=0001 Version=0000N: Name=Power ButtonP: Phys=LNXPWRBN/button/input0S: Sysfs=/devices/LNXSYSTM:00/LNXPWRBN:00/input/input0U: Uniq=H: Handlers=kbd event0B: EV=3B: KEY=100000 0 0 0I: Bus=0017 Vendor=0001 Product=0001 Version=0100N: Name=Macintosh mouse button emulationP: Phys=S: Sysfs=/devic

9、es/virtual/input/input1U: Uniq=H: Handlers=mouse0 event1B: EV=7B: KEY=70000 0 0 0 0 0 0 0 0B: REL=3I: Bus=0011 Vendor=0001 Product=0001 Version=ab41N: Name=AT Translated Set 2 keyboardP: Phys=isa0060/serio0/input0S: Sysfs=/devices/platform/i8042/serio0/input/input2U: Uniq=H: Handlers=kbd event2B: EV

10、=120013B: KEY=4 2000000 3803078 f800d001 feffffdf ffefffff ffffffff fffffffeB: MSC=10B: LED=7I: Bus=0011 Vendor=0002 Product=0005 Version=0000N: Name=ImPS/2 Generic Wheel MouseP: Phys=isa0060/serio1/input0S: Sysfs=/devices/platform/i8042/serio1/input/input3U: Uniq=H: Handlers=mouse1 event3B: EV=7B:

11、KEY=70000 0 0 0 0 0 0 0 0B: REL=103I line:这行包含身份信息,显示了 bus type是 3 (usb), vendor, product, version等信息。N line:这行包含了名字信息。P line:这行包含了物理设备信息。H line: 这行包含了与设备关联的 handler drivers。B line: 这些行包含了显示设备能力的一些位域 (bitfield)。我的键盘对应的事件类型是event22)open /dev/input/event2 device error需要使用sudo或者改变设备的属性如下:sudo./key_simulator或者chmod 777 /dev/input/event2

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 办公文档 > 其它办公文档

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号