How to Use Adb to Send Touch Events to Device Using Sendevent Command

How to use ADB to send touch events to device using sendevent command?

Android comes with an input command-line tool that can simulate miscellaneous input events. To simulate tapping, it's:

input tap x y

You can use the adb shell ( > 2.3.5) to run the command remotely:

adb shell input tap x y

ADB: How to programmatically determine which input device is used for sending touch event with sendevent

Thanks to @AlexP. for the relevant link to the command getevent -pl. The SO answer he linked did not fully work for me though, so, I had to modify it a bit. This is the one that appears to work for me.

getevent -pl | awk 'BEGIN { RS="add device "; } /^[0-9]/ { print RS $0; }' | grep -B 100 ABS_MT_POSITION_X | awk '/add device/ {print $NF}'

simulating touch using ADB

Since it seems to change depending on the Android version, I suggest you to follow these instructions :

  1. Start dump motion event you need to reproduce:

    ~$ adb shell getevent | grep event2

    grep is very useful to filter output.

  2. Do motion event you want to reproduce;

  3. Then just convert all values from hex in dump to decimal values! :)


To find what eventX is working for you do following:

  1. Start terminal and type:

    ~$ adb shell getevent

You will see quickly moving traces with for example /dev/input/event4 ......


  1. Touch screen once

You must see between event4 few eventX and these eventX right in the moment of the touch

will be yours input interface for reproducing motion events! :)

Source.

can anyone explain this command fully adb shell sendevent [device] [type] [code] [value]?

First you need to find out the name of the touchscreen device on your phone or tablet. You can use this command in adb shell session:

getevent -pl 2>&1 | sed -n '/^add/{h}/ABS_MT_TOUCH/{x;s/[^/]*//p}'

Let's say the input device name is /dev/input/event0 and you want to emulate a quick tap at coordinates x=300, y=400:

sendevent /dev/input/event0 3 53 300
sendevent /dev/input/event0 3 54 400
sendevent /dev/input/event0 3 48 5
sendevent /dev/input/event0 3 58 50
sendevent /dev/input/event0 0 2 0
sendevent /dev/input/event0 0 0 0
sendevent /dev/input/event0 0 2 0
sendevent /dev/input/event0 0 0 0

The long touch (let's say 1sec long) at the same coordinates would be:

sendevent /dev/input/event0 3 53 300
sendevent /dev/input/event0 3 54 400
sendevent /dev/input/event0 3 48 5
sendevent /dev/input/event0 3 58 50
sendevent /dev/input/event0 0 2 0
sendevent /dev/input/event0 0 0 0
sleep 1
sendevent /dev/input/event0 0 2 0
sendevent /dev/input/event0 0 0 0

For the explanation what those commands mean and do exactly please read Emulating touchscreen interaction with sendevent in Android.



Related Topics



Leave a reply



Submit