在做SDL至Android的移植时,键盘事件是能正常捕获到,看了SLD的源码,发现用的device是 /dev/tty0,但是鼠标叫是不能成功捕获,总是得到 0,运行命令查看devices时,显示如下:


# cat /proc/bus/input/devices
cat /proc/bus/input/devices
I: Bus=0000 Vendor=0000 Product=0000 Version=0000
N: Name="qwerty"
P: Phys=
S: Sysfs=/class/input/input0
U: Uniq=
H: Handlers=kbd mouse0 event0
B: EV=2f
B: KEY=ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff f
fffffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff fffffffe
B: REL=3
B: ABS=7
B: SW=1


进入 /dev/input 目录,发现在3个device文件:mice,mouse0,event0,分别 cat这3个文件,发现只有 event0 有反应,如下图:




而且不管是点击鼠标还是按键,都有反应,但显示的是一堆乱码,而且点击鼠标出来的东西要多一点,难道这就是传说是的 touchscreen ?!

为了分析 event0 的返回值,写了一段代码 testmice.c,如下:


#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <linux/input.h>

static int event0_fd = -1;
struct input_event ev0[64];

//for handling event0, mouse/key/ts
static int handle_event0() {
int button = 0, realx = 0, realy = 0, i, rd;

rd = read(event0_fd, ev0, sizeof(struct input_event) * 64);
if ( rd < sizeof(struct input_event) ) return 0;

for (i = 0; i < rd / sizeof(struct input_event); i++) {
printf("", ev0[i].type, ev0[i].code, ev0[i].value);
if (ev0[i].type == 3 && ev0[i].code == 0)
realx = ev0[i].value;
else if (ev0[i].type == 3 && ev0[i].code == 1)
realy = ev0[i].value;
else if (ev0[i].type == 1) {
if (ev0[i].code == 158) {
//if key esc then exit
return 0;
}
} else if (ev0[i].type == 0 && ev0[i].code == 0 && ev0[i].value == 0) {
realx = 0, realy = 0;
}
printf("event(%d): type: %d; code: %3d; value: %3d; realx: %3d; realy: %3d\n", i,
ev0[i].type, ev0[i].code, ev0[i].value, realx, realy);
}

return 1;
}

int main(void) {
int done = 1;
printf("sizeof(struct input_event) = %d\n", sizeof(struct input_event));

event0_fd = open("/dev/input/event0", O_RDWR);

if ( event0_fd < 0 )
return -1;

while ( done ) {
printf("begin handel_event0...\n");
done = handle_event0();
printf("end handel_event0...\n");
}

if ( event0_fd > 0 ) {
close(event0_fd);
event0_fd = -1;
}

return 0;

}



用交叉编译器编译好后,push至 emulator后执行后,切换到android 模拟器,在模拟器上点几下mouse,程序就会打出你点击的信息,效果如下,果然能正确得到点击的 mouse pos,如下图:



分析上面的返回值,发现当按下 mouse left button 时,会得到4个事件,2个 type = 3 的事件返回了 pos x, pos y 的值,即mouse click pos, 另外1个 type = 1 的事件是按键事件(keydown),value就是按下的键的key,为0的应该就是 key的release事件,当松开 mouse时,也会得到两个 type = 1, 0 的事件,没有仔细去看它们的返回值,反正已经正确得到了 mosue的事件,下一步就是改SDL的事件驱动源码了...

更多相关文章

  1. 用Kotlin写响应式编程RxAndroid
  2. 记录我发现的第一个关于 Google 的 Bug
  3. [Android进阶笔记]Android触摸事件的拦截机制
  4. Android中的按键监听
  5. ANDROID关于亮屏和暗屏还有解锁的监听事件http://blog.csdn.net/
  6. INSTALL_FAILED_MISSING_SHARED_LIBRARY错误的解决
  7. Android连续点击两次退出程序
  8. Android怎么实现网易新闻的图文混排?
  9. android可视化视图无法显示layout控件

随机推荐

  1. Android中Activity启动模式launchermode
  2. 三个博客和一张关系图,掌握Android(安卓)W
  3. Android(安卓)studio 挂断电话 使用ITele
  4. 查看Android内存的8中方法
  5. Android(安卓)配置Material Design库及使
  6. Android中文API(136) —— GeoCoder
  7. Android中重复执行动画bug
  8. Tab添加左右按钮
  9. Android程序版本控制工具类
  10. 编程之路-客户端学习知识点纲要(Web/iOS/A