39

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

Alex P.
  • 30,437
  • 17
  • 118
  • 169
Sunilcnair
  • 401
  • 1
  • 5
  • 4
  • Checkout http://marian.schedenig.name/2014/07/03/remote-control-your-android-phone-through-adb/ – Gelldur Dec 31 '15 at 14:29

4 Answers4

57

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>
Pedro Lobito
  • 94,083
  • 31
  • 258
  • 268
35

To send touch event you need to do:

  1. Set coordinates:

    adb shell sendevent /dev/input/event2 3 0 x
    adb shell sendevent /dev/input/event2 3 1 y
    
  2. 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
    
  3. 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:

  1. You can record events:

    adb shell getevent
    
  2. If you use getevent all event values are in hex.

Anton Menshov
  • 2,266
  • 14
  • 34
  • 55
user643154
  • 2,361
  • 1
  • 19
  • 11
27

I managed to record a session and replay it with only bash and adb.

This what I did, I hope it helps someone.

Record a series of events

  1. 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
    
  2. do some stuff on the phone
  3. stop the adb command on the pc with ctrl-c

Replay the recorded events

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
MaxChinni
  • 1,206
  • 14
  • 21
  • 1
    "awk: bailing out at source line 1" this error is coming. Do u have any idea ? – nikhil84 Aug 19 '14 at 06:54
  • 1
    replay command doesn't work on OSX. xargs: illegal option -- l; awk: calling undefined function strtonum – retromuz Feb 20 '15 at 12:03
  • 1
    @geeth it seems [someone has found it, too](http://stackoverflow.com/questions/8199934/strtonum-in-os-x-not-found). Sorry by I'm on GNU/Linux. – MaxChinni Feb 20 '15 at 16:14
  • 1
    This worked great for me, however I need to have a delay between each line. I tried this, but it didn't work: awk '{printf "%s %d %d %d\n", substr($1, 1, length($1) -1), strtonum("0x"$2), strtonum("0x"$3), strtonum("0x"$4);sleep 5}' android-touch-events.log | xargs -l adb shell sendevent – Rich Elswick Aug 18 '15 at 13:50
  • this answered it for me some, however, the delay seems to cause issues and the input may timeout: http://stackoverflow.com/questions/15153240/how-to-sleep-for-1-second-between-each-xargs-command – Rich Elswick Aug 18 '15 at 14:42
  • @MaxChinni awesome.! but am getting a delay between each touch which i didn't make when i recorded the input.So, how do i remove the delay and make it as it recorded? how can i repeat the task like 2-3 times ?. thank you – theapache64 Oct 26 '16 at 14:47
  • 1
    I still get the xargs: illegal option -- l. Which option is used in the command ? – Chris Maverick Feb 23 '20 at 16:24
  • 1
    On Mac, I fixed the xargs issue above with "-L 1". So the whole command: gawk '{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 1 adb shell sendevent – Eradicatore Nov 18 '22 at 14:32
  • I also needed to use the command "adb root" before doing this. Otherwise, I got "sendevent: /dev/input/event5: Permission denied" – Eradicatore Nov 18 '22 at 14:33
6

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.

serv-inc
  • 35,772
  • 9
  • 166
  • 188