emulate touch event from adb

::

it is for android, of course!

if you'd prefer to emulate user behavior directly but not write test cases like MonkeyRunner, it is best to just repeat all user touch/click motions directly.

android just provide tools to send/recv event directly for input systems.

by typing below cmd you are able to get all input devices and its functions, and dump all the events received.

adb shell getevent
add device 1: /dev/input/event5
name: "msm8974-taiko-mtp-snd-card Headset Jack"
add device 2: /dev/input/event4
name: "msm8974-taiko-mtp-snd-card Button Jack"
add device 3: /dev/input/event3
name: "hs_detect"
add device 4: /dev/input/event1
name: "touch_dev"
add device 5: /dev/input/event0
name: "qpnp_pon"
add device 6: /dev/input/event2
name: "gpio-keys"

you can find that "/dev/input/event1" stands for the touch device.

if click on the screen once, it would dump like

/dev/input/event1: 0003 0035 000003cd
/dev/input/event1: 0003 0036 00000377
/dev/input/event1: 0003 0039 000000fa
/dev/input/event1: 0001 014a 00000001
/dev/input/event1: 0000 0000 00000000
/dev/input/event1: 0003 0039 ffffffff
/dev/input/event1: 0001 014a 00000000
/dev/input/event1: 0000 0000 00000000
add "-l" param would show the results more readable

/dev/input/event1: EV_ABS       ABS_MT_TOUCH_MAJOR   00000018            
/dev/input/event1: EV_ABS       ABS_MT_WIDTH_MAJOR   00000018            
/dev/input/event1: EV_ABS       ABS_MT_PRESSURE      00000018            
/dev/input/event1: EV_ABS       ABS_MT_POSITION_X    00000207            
/dev/input/event1: EV_ABS       ABS_MT_POSITION_Y    0000057d            
/dev/input/event1: EV_ABS       ABS_MT_TRACKING_ID   0000010a            
/dev/input/event1: EV_KEY       BTN_TOUCH            DOWN                
/dev/input/event1: EV_SYN       SYN_REPORT           00000000            
/dev/input/event1: EV_ABS       ABS_MT_TRACKING_ID   ffffffff            
/dev/input/event1: EV_KEY       BTN_TOUCH            UP                  
/dev/input/event1: EV_SYN       SYN_REPORT           00000000  
different HW and android version may contains more or less events, and the order of the may also differ, while position/touch/syn events would be all kept.

and to send, just type like below:

adb shell sendevent /dev/input/event1 0x0003 0x0035 0x000003cd
the three digital params meanings are the same as the one that you dumpped by "getevent".

one problem is that "getevent" dump the digitals as hex format while "sendevent" only accept decimal format. so apply the cmd above directly would not work at all.

to make it easy to input, you can patch the sendevent utils:

//system/core/toolbox/sendevent.c

diff --git a/toolbox/sendevent.c b/toolbox/sendevent.c
index 4d0ca17..6e4b0fa 100644
--- a/toolbox/sendevent.c
+++ b/toolbox/sendevent.c
@@ -30,9 +30,14 @@ int sendevent_main(int argc, char *argv[])
         return 1;
     }
     memset(&event, 0, sizeof(event));
+    /* //[NET] make it has ability to parse hex string
     event.type = atoi(argv[2]);
     event.code = atoi(argv[3]);
     event.value = atoi(argv[4]);
+    // */
+    event.type = (int)strtol(argv[2], NULL, 0);
+    event.code = (int)strtol(argv[3], NULL, 0);
+    event.value = (int)strtol(argv[4], NULL, 0);
     ret = write(fd, &event, sizeof(event));
     if(ret < (ssize_t) sizeof(event)) {
         fprintf(stderr, "write event failed, %s\n", strerror(errno));
Notice: you have to have a copy of android source code to modify and build the modules. and have right to replace that into your device. btw, the target module is "toolbox" but not "sendevent".

you can also emulate some key input like BACK/HOME and others by input cmd too:

adb shell input keyevent 4
above is for click back button, others are all the same.

to input text, just type:

adb shell "input text here" 
you can check "input" cmd for more details if interested, most input behavior is able to be emulated by this cmd.

::

by above all preparations, nearly all user behaviors is able to be emulated now.

just try one test case from the beginning and dump all the inputs, then translate these dumped events into cmd for send.

remember to sleep for a while and check status while there are UI change and there are different UI behavior depending on some other parts' executing result. and the result maybe out of control by simply rotating your device.

and, it sounds simple, but an perfect impl may be far more than this.


REF:

http://kenji-chao.logdown.com/posts/2013/11/12/android-use-adb-commands-to-perform-the-app-and-send-keyevent
http://azard.me/blog/2015/06/13/android-cross-app-touch-event-injection/
http://c.biancheng.net/cpp/html/129.html


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值