I am trying to send touch events to a device using adb shell command, so that I can do some basic automation for UI tests. I have followed the discussion in some previous threads regarding this.
I confirmed about getting the events and using sendevent, to send out 6 events for each touch ( xcoord, ycoord, 2 for press, 2 for release) and it was easy to use this information with sendevent, but the getevent command for the touchscreen device seems to generate far too many events.
Has somebody managed to send touch event from adb to a device? Could you please share the solution.
I am working on recording the touch events on phone. After that I wish to send the same event back to the device as part of UI testing.
Please help
Android comes with an input command-line tool that can simulate miscellaneous input events.
To simulate a tap, use:
input tap x y
Run the input command remotely using adb shell:
adb shell input tap x y
Other options are:
shell@m0:/ $ input
input
usage: input ...
input text <string>
input keyevent <key code number or name>
input [touchscreen|touchpad|touchnavigation] tap <x> <y>
input [touchscreen|touchpad|touchnavigation] swipe <x1> <y1> <x2> <y2> [duration(ms)]
input trackball press
input trackball roll <dx> <dy>
To send touch event you need to do:
Set coordinates:
adb shell sendevent /dev/input/event2 3 0 x
adb shell sendevent /dev/input/event2 3 1 y
Send touch event (must have 0 0 0 pair):
adb shell sendevent /dev/input/event2 1 330 1
adb shell sendevent /dev/input/event2 0 0 0
Send release finger event (must have 0 0 0 pair):
adb shell sendevent /dev/input/event2 1 330 0
adb shell sendevent /dev/input/event2 0 0 0
Please note:
You can record events:
adb shell getevent
If you use getevent all event values are in hex.
I managed to record a session and replay it with only bash and adb.
This what I did, I hope it helps someone.
Set up the pc to record data in a file (/tmp/android-touch-events.log)
$ adb shell getevent | grep --line-buffered ^/ | tee /tmp/android-touch-events.log
ctrl-c
This command will do the hex conversion in awk
$ awk '{printf "%s %d %d %d\n", substr($1, 1, length($1) -1), strtonum("0x"$2), strtonum("0x"$3), strtonum("0x"$4)}' /tmp/android-touch-events.log | xargs -l adb shell sendevent
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With