全部博文(60)
分类: Android平台
2015-08-17 11:03:49
有的时候我们并不希望起个View,从画面上得到各种输入事件。
怎么从底层得到各种input事件呢?
首先android下有个工具getevent,输出结果如下:
$ adb shell getevent add device 1: /dev/input/event7 name: "proximity" add device 2: /dev/input/event6 name: "lightsensor-level" add device 3: /dev/input/event5 name: "compass" add device 4: /dev/input/event4 name: "saga-keypad" add device 5: /dev/input/event3 name: "atmel-touchscreen" add device 6: /dev/input/event2 name: "projector-Keypad" add device 7: /dev/input/event1 name: "projector_input" add device 8: /dev/input/event0 name: "h2w headset" /dev/input/event4: 0001 0074 00000001 /dev/input/event6: 0003 0028 ffffffff /dev/input/event6: 0000 0000 00000000 /dev/input/event6: 0003 0028 00000005 /dev/input/event6: 0000 0000 00000000 /dev/input/event3: 0003 0030 00000000 /dev/input/event3: 0000 0000 00000000 /dev/input/event3: 0003 0030 00000000 /dev/input/event3: 0000 0000 00000000 /dev/input/event4: 0001 0074 00000000
/dev/input/event?都是一些输入设备,
比如event3是触摸屏的,event5是罗盘的,event4是物理键盘的,等等。
既然getevent能取到这些信息,那我们能不能自己写程序获得这些事件呢,
当然可以,以event3(触摸屏事件)为例:
#include <fcntl.h> #include <linux/input.h> #include <errno.h> #include <poll.h> static char TOUCH_DEVICE[256] = "/dev/input/event3"; int touchfd = -1; struct pollfd fds[1]; static void init_touch() { if((touchfd = open(TOUCH_DEVICE, O_RDWR)) == -1) { printf("cannot open touch device %s\n", TOUCH_DEVICE); // exit(-1); } fds[0].fd = touchfd; fds[0].events = POLLIN; } int getevent() { struct input_event event; int res = 0; res = poll(fds, 1, -1); while(true) { if(fds[0].revents) { if(fds[0].revents & POLLIN) { res = read(touchfd, &event, sizeof(event)); if(res < (int)sizeof(event)) { fprintf(stderr, "could not get event\n"); return 1; } printf("getevent: %04x %04x %08x\n", event.type, event.code, event.value); } } } return 0; }
能取到了,怎么用就不说了