115

I am trying to send touch events to a device using AndroidDebugBridge, so that I can do some basic automation for UI tests. I have followed the discussion in LINK. I am able to use sendevent to simulate touch on emulators, but unable to do the same on a device.

Like in above link the emulator seems to send out 6 events for each touch (xcoord, ycoord, 2 for press, 2 for release) and it was easy to use this information to sendevents, but a getevent for the touchscreen for a device seems to generate far too many events.

Has somebody managed to send touch from ADB to a device? Could you please share the solution.

1
  • On the device, did you check if you are passing the events to the proper "input-device"? ie. the input-device which is registered as the touch-driver? Commented Mar 17, 2011 at 11:26

6 Answers 6

174

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
4
  • 2
    It works perfectly on Nox App using nox_adb.exe thank you!
    – Smeterlink
    Commented Nov 14, 2015 at 11:57
  • 2
    For Testing double tap i wrote a loop click like this i=0; while [ $(($i)) -le 2 ]; do i=$(($i + 1)); input tap 500 800; done; But it is clicking the place every one second. can i do it faster using terminal?
    – Vignesh KM
    Commented Sep 21, 2017 at 5:01
  • 1
    Any way to long press too? Commented Feb 13, 2023 at 19:01
  • It's working as required. Thanks for this help Commented Sep 11, 2023 at 9:22
93

In order to do a particular action (for example to open the web browser), you need to first figure out where to tap. To do that, you can first run:

adb shell getevent -l

Once you press on the device, at the location that you want, you will see this output:

<...>
/dev/input/event3: EV_KEY       BTN_TOUCH            DOWN
/dev/input/event3: EV_ABS       ABS_MT_POSITION_X    000002f5
/dev/input/event3: EV_ABS       ABS_MT_POSITION_Y    0000069e

adb is telling you that a key was pressed (button down) at position 2f5, 69e in hex which is 757 and 1694 in decimal.

If you now want to generate the same event, you can use the input tap command at the same position:

adb shell input tap 757 1694

More info can be found at:

https://source.android.com/devices/input/touch-devices.html http://source.android.com/devices/input/getevent.html

4
  • 18
    To find the touch co-ordinates, I would recommend using Developer Options --> Show Touch Location. Also, it gives the values in decimals.
    – Praveen
    Commented May 30, 2016 at 2:15
  • 5
    If you don't want to have to convert the hex to decimal, you can let your shell do it: adb shell input tap $((16#2f5)) $((16#69e)). Also, just to be pedantic, 0x2F5 and 0x69E are 757 and 1694 respectively... What did you use to convert between bases?
    – ghoti
    Commented Oct 17, 2016 at 20:53
  • Thanks @ghoti not sure I must have copied it off another point or something like that...
    – Tomas
    Commented Nov 10, 2016 at 17:17
  • Building on top of this I built the command adb shell getevent -l | grep ABS_MT_POSITION --line-buffered | awk '{a = substr($0,54,8); sub(/^0+/, "", a); b = sprintf("0x%s",a); printf("%d\n",strtonum(b))}' that gets the tap position as an integer
    – kyczawon
    Commented Jun 13, 2020 at 15:51
14

2.3.5 did not have input tap, just input keyevent and input text You can use the monkeyrunner for it: (this is a copy of the answer at https://stackoverflow.com/a/18959385/1587329):

You might want to use monkeyrunner like this:

$ monkeyrunner
>>> from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice
>>> device = MonkeyRunner.waitForConnection()
>>> device.touch(200, 400, MonkeyDevice.DOWN_AND_UP)

You can also do a drag, start activies etc. Have a look at the api for MonkeyDevice.

14

Building on top of Tomas's answer, this is the best approach of finding the location tap position as an integer I found:

adb shell getevent -l | grep ABS_MT_POSITION --line-buffered | awk '{a = substr($0,54,8); sub(/^0+/, "", a); b = sprintf("0x%s",a); printf("%d\n",strtonum(b))}'

Use adb shell getevent -l to get a list of events, the using grep for ABS_MT_POSITION (gets the line with touch events in hex) and finally use awk to get the relevant hex values, strip them of zeros and convert hex to integer. This continuously prints the x and y coordinates in the terminal only when you press on the device.

You can then use this adb shell command to send the command:

adb shell input tap x y
5
  • Your answer is great, can we use this command to further send those integers to another device like reading touch events from a physical keyboard and at the same time using sendevents we can forward those to another virtual keyboard Commented Nov 23, 2021 at 8:33
  • But one touch prints 6 numbers! 7150 6984 7147 6974 7143 6964 and if you press harder, it prints more! Can we pass a sequence of x y in one tap command?
    – Dr.jacky
    Commented Jan 21, 2023 at 22:30
  • ``` awk: calling undefined function strtonum input record number 1, file source line number 1 ``` Commented Nov 22, 2023 at 3:02
  • @LêVũHuy Pay attention to use adb shell prefix to leverage your computer awk, not the Android one. Commented Jun 22 at 16:39
  • substr($0,49,1) allows knowing the coordinate (X or Y). Commented Jun 22 at 16:43
11

You don't need to use

adb shell getevent -l

command, you just need to enable in Developer Options on the device [Show Touch data] to get X and Y.

Some more information can be found in my article here: https://mobileqablog.wordpress.com/2016/08/20/android-automatic-touchscreen-taps-adb-shell-input-touchscreen-tap/

5

Consider using Android's uiautomator, with adb shell uiautomator [...] or directly using the .jar that comes with the SDK.

1
  • BTW, there are a few tools on the market that can make Android's Automation testing quite easier for you.
    – Elist
    Commented Jun 16, 2013 at 10:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.