10

So far I'm able to tap, swipe, unlock, install/uninstall and launch app using adb commands but not able to find how to perform zoom in/out using adb command.

I have got coordinates of pinch in/out but not understanding how to fire them using adb command from terminal.

For example , we have command --> adb shell input tap x y So in this we can replace x and y with values/coordinates similarly I want to know the way to simulate pinch in/out.

Coordinates that I get for pinch-in are below.

{"id":0,"dumb":[{"events":[{"id":0,"pressure":1,"x":392.94117647058823,"y":607.4264705882354}],"action":0},{"events":[{"id":0,"pressure":1,"x":392.94117647058823,"y":607.4264705882354},{"id":1,"pressure":1,"x":327.05882352941177,"y":672.5735294117648}],"action":261},{"events":[{"id":0,"pressure":1,"x":392.94117647058823,"y":609.7794117647059},{"id":1,"pressure":1,"x":327.05882352941177,"y":670.2205882352941}],"action":2},{"events":[{"id":0,"pressure":1,"x":392.94117647058823,"y":612.1323529411765},{"id":1,"pressure":1,"x":327.05882352941177,"y":667.8676470588235}],"action":2},{"events":[{"id":0,"pressure":1,"x":392.94117647058823,"y":616.8382352941177},{"id":1,"pressure":1,"x":327.05882352941177,"y":663.1617647058823}],"action":2},{"events":[{"id":0,"pressure":1,"x":392.94117647058823,"y":626.25},{"id":1,"pressure":1,"x":327.05882352941177,"y":653.75}],"action":2},{"events":[{"id":0,"pressure":1,"x":392.94117647058823,"y":630.9558823529412},{"id":1,"pressure":1,"x":327.05882352941177,"y":649.0441176470589}],"action":2},{"events":[{"id":0,"pressure":1,"x":395.29411764705884,"y":635.6617647058823},{"id":1,"pressure":1,"x":324.70588235294116,"y":644.3382352941177}],"action":2},{"events":[{"id":0,"pressure":1,"x":397.64705882352945,"y":642.7205882352941},{"id":1,"pressure":1,"x":322.3529411764706,"y":637.2794117647059}],"action":2},{"events":[{"id":0,"pressure":1,"x":400,"y":649.7794117647059},{"id":1,"pressure":1,"x":320,"y":630.2205882352941}],"action":2},{"events":[{"id":0,"pressure":1,"x":402.3529411764706,"y":656.8382352941177},{"id":1,"pressure":1,"x":317.64705882352945,"y":623.1617647058823}],"action":2},{"events":[{"id":0,"pressure":1,"x":402.3529411764706,"y":661.5441176470589},{"id":1,"pressure":1,"x":317.64705882352945,"y":618.4558823529412}],"action":2},{"events":[{"id":0,"pressure":1,"x":404.7058823529412,"y":668.6029411764706},{"id":1,"pressure":1,"x":315.29411764705884,"y":611.3970588235294}],"action":2},{"events":[{"id":0,"pressure":1,"x":404.7058823529412,"y":673.3088235294118},{"id":1,"pressure":1,"x":315.29411764705884,"y":606.6911764705883}],"action":2},{"events":[{"id":0,"pressure":1,"x":404.7058823529412,"y":680.3676470588235},{"id":1,"pressure":1,"x":315.29411764705884,"y":599.6323529411765}],"action":2},{"events":[{"id":0,"pressure":1,"x":407.05882352941177,"y":685.0735294117648},{"id":1,"pressure":1,"x":312.94117647058823,"y":594.9264705882354}],"action":2},{"events":[{"id":0,"pressure":1,"x":407.05882352941177,"y":687.4264705882354},{"id":1,"pressure":1,"x":312.94117647058823,"y":592.5735294117648}],"action":2},{"events":[{"id":0,"pressure":1,"x":409.4117647058824,"y":689.7794117647059},{"id":1,"pressure":1,"x":310.5882352941177,"y":590.2205882352941}],"action":2},{"events":[{"id":0,"pressure":1,"x":411.7647058823529,"y":694.4852941176471},{"id":1,"pressure":1,"x":308.2352941176471,"y":585.5147058823529}],"action":2},{"events":[{"id":0,"pressure":1,"x":411.7647058823529,"y":699.1911764705883},{"id":1,"pressure":1,"x":308.2352941176471,"y":580.8088235294118}],"action":2},{"events":[{"id":0,"pressure":1,"x":414.11764705882354,"y":706.25},{"id":1,"pressure":1,"x":305.88235294117646,"y":573.75}],"action":2},{"events":[{"id":0,"pressure":1,"x":416.47058823529414,"y":713.3088235294118},{"id":1,"pressure":1,"x":303.5294117647059,"y":566.6911764705883}],"action":2},{"events":[{"id":0,"pressure":1,"x":418.8235294117647,"y":715.6617647058824},{"id":1,"pressure":1,"x":301.1764705882353,"y":564.3382352941177}],"action":2},{"events":[{"id":0,"pressure":1,"x":418.8235294117647,"y":718.0147058823529},{"id":1,"pressure":1,"x":301.1764705882353,"y":561.9852941176471}],"action":2},{"events":[{"id":0,"pressure":1,"x":418.8235294117647,"y":720.3676470588235},{"id":1,"pressure":1,"x":301.1764705882353,"y":559.6323529411765}],"action":2},{"events":[{"id":0,"pressure":1,"x":421.1764705882353,"y":727.4264705882354},{"id":1,"pressure":1,"x":298.8235294117647,"y":552.5735294117648}],"action":2},{"events":[{"id":0,"pressure":1,"x":421.1764705882353,"y":732.1323529411765},{"id":1,"pressure":1,"x":298.8235294117647,"y":547.8676470588235}],"action":2},{"events":[{"id":0,"pressure":1,"x":423.5294117647059,"y":739.1911764705883},{"id":1,"pressure":1,"x":296.47058823529414,"y":540.8088235294118}],"action":2},{"events":[{"id":0,"pressure":1,"x":425.88235294117646,"y":746.25},{"id":1,"pressure":1,"x":294.11764705882354,"y":533.75}],"action":2},{"events":[{"id":0,"pressure":1,"x":425.88235294117646,"y":753.3088235294118},{"id":1,"pressure":1,"x":294.11764705882354,"y":526.6911764705883}],"action":2},{"events":[{"id":0,"pressure":1,"x":428.2352941176471,"y":758.0147058823529},{"id":1,"pressure":1,"x":291.7647058823529,"y":521.9852941176471}],"action":2},{"events":[{"id":0,"pressure":1,"x":435.29411764705884,"y":765.0735294117648},{"id":1,"pressure":1,"x":284.70588235294116,"y":514.9264705882354}],"action":2},{"events":[{"id":0,"pressure":1,"x":440,"y":769.7794117647059},{"id":1,"pressure":1,"x":280,"y":510.22058823529414}],"action":2},{"events":[{"id":0,"pressure":1,"x":440,"y":779.1911764705883},{"id":1,"pressure":1,"x":280,"y":500.80882352941177}],"action":2},{"events":[{"id":0,"pressure":1,"x":444.7058823529412,"y":788.6029411764706},{"id":1,"pressure":1,"x":275.29411764705884,"y":491.39705882352945}],"action":2},{"events":[{"id":0,"pressure":1,"x":451.7647058823529,"y":800.3676470588235},{"id":1,"pressure":1,"x":268.2352941176471,"y":479.63235294117646}],"action":2},{"events":[{"id":0,"pressure":1,"x":456.47058823529414,"y":812.1323529411765},{"id":1,"pressure":1,"x":263.5294117647059,"y":467.86764705882354}],"action":2},{"events":[{"id":0,"pressure":1,"x":461.1764705882353,"y":821.5441176470589},{"id":1,"pressure":1,"x":258.8235294117647,"y":458.4558823529412}],"action":2},{"events":[{"id":0,"pressure":1,"x":465.88235294117646,"y":833.3088235294118},{"id":1,"pressure":1,"x":254.11764705882354,"y":446.69117647058823}],"action":2},{"events":[{"id":0,"pressure":1,"x":470.5882352941177,"y":840.3676470588235},{"id":1,"pressure":1,"x":249.41176470588235,"y":439.63235294117646}],"action":2},{"events":[{"id":0,"pressure":1,"x":475.29411764705884,"y":847.4264705882354},{"id":1,"pressure":1,"x":244.7058823529412,"y":432.5735294117647}],"action":2},{"events":[{"id":0,"pressure":1,"x":477.64705882352945,"y":854.4852941176471},{"id":1,"pressure":1,"x":242.3529411764706,"y":425.5147058823529}],"action":2},{"events":[{"id":0,"pressure":1,"x":482.3529411764706,"y":863.8970588235294},{"id":1,"pressure":1,"x":237.64705882352942,"y":416.1029411764706}],"action":2},{"events":[{"id":0,"pressure":1,"x":484.7058823529412,"y":870.9558823529412},{"id":1,"pressure":1,"x":235.29411764705884,"y":409.04411764705884}],"action":2},{"events":[{"id":0,"pressure":1,"x":489.4117647058824,"y":875.6617647058824},{"id":1,"pressure":1,"x":230.58823529411765,"y":404.3382352941177}],"action":2},{"events":[{"id":0,"pressure":1,"x":489.4117647058824,"y":878.0147058823529},{"id":1,"pressure":1,"x":230.58823529411765,"y":401.9852941176471}],"action":2},{"events":[{"id":0,"pressure":1,"x":491.764705882353,"y":882.7205882352941},{"id":1,"pressure":1,"x":228.23529411764707,"y":397.2794117647059}],"action":2},{"events":[{"id":0,"pressure":1,"x":494.11764705882354,"y":885.0735294117648},{"id":1,"pressure":1,"x":225.88235294117646,"y":394.9264705882353}],"action":2},{"events":[{"id":0,"pressure":1,"x":496.47058823529414,"y":892.1323529411765},{"id":1,"pressure":1,"x":223.52941176470588,"y":387.86764705882354}],"action":2},{"events":[{"id":0,"pressure":1,"x":501.1764705882353,"y":899.1911764705883},{"id":1,"pressure":1,"x":218.82352941176472,"y":380.80882352941177}],"action":2},{"events":[{"id":0,"pressure":1,"x":503.5294117647059,"y":908.6029411764706},{"id":1,"pressure":1,"x":216.47058823529412,"y":371.39705882352945}],"action":2},{"events":[{"id":0,"pressure":1,"x":505.88235294117646,"y":908.6029411764706},{"id":1,"pressure":1,"x":214.11764705882354,"y":371.39705882352945}],"action":2},{"events":[{"id":0,"pressure":1,"x":505.88235294117646,"y":910.9558823529412},{"id":1,"pressure":1,"x":214.11764705882354,"y":369.04411764705884}],"action":2},{"events":[{"id":0,"pressure":1,"x":505.88235294117646,"y":913.3088235294118},{"id":1,"pressure":1,"x":214.11764705882354,"y":366.69117647058823}],"action":2},{"events":[{"id":0,"pressure":1,"x":505.88235294117646,"y":913.3088235294118}],"action":1},{"events":[{"id":0,"pressure":1,"x":505.88235294117646,"y":913.3088235294118},{"id":1,"pressure":1,"x":214.11764705882354,"y":366.69117647058823}],"action":262}],"eventType":"MOTION"}
Murtaza Khursheed Hussain
  • 15,176
  • 7
  • 58
  • 83
nikhil84
  • 3,235
  • 4
  • 22
  • 43
  • This looks like an `Instrumentation`. Why not using something like an `ActivityInstrumentationTestCase2`, then send `MotionEvent` to your `Activity` using `Instrumentation` API ? – Antoine Marques Aug 18 '14 at 13:13
  • 1
    What is the purpose of this? Just testing? If so, you can use Robotium and here is an example [link](https://groups.google.com/d/msg/robotium-developers/CmjQKrPL6uE/PEnvr7bPWGgJ). You can also use batching. take a look [link](http://stackoverflow.com/a/25045657/896038) – Saeid Farivar Aug 18 '14 at 13:16
  • I want to be able to fire adb command from terminal itself. Instrumentation and testcase2 relates to scripting if me not wrong, so don't want to go into that. Still if @AntoineMarques if u have example of it pls provide me link. – nikhil84 Aug 19 '14 at 04:07
  • See here the part about `Instrumentation` (http://developer.android.com/training/activity-testing/activity-basic-testing.html). The method i refer to is http://developer.android.com/reference/android/app/Instrumentation.html#sendPointerSync(android.view.MotionEvent). Then, this guide might help you if haven't already made an instrumentation : http://developer.android.com/tools/testing/activity_test.html – Antoine Marques Aug 19 '14 at 07:27
  • @AntoineMarques for these i need script to run but what i'm looking is way to fire pinch in/out with coordinates like input tap x y. Still appreciated for your ans. – nikhil84 Aug 19 '14 at 08:18
  • I don't think there is a way to do multitouch actions using ADB. You might be able to acheive what you want using uiautomator, there is support for multitouch since api level 18. http://developer.android.com/tools/testing/testing_ui.html http://developer.android.com/tools/help/uiautomator/UiObject.html#performMultiPointerGesture(android.view.MotionEvent.PointerCoords[]...) – Raanan Sep 01 '14 at 17:04
  • @Raanan I have looked around and came across multitouch/touchscreen in ADB ,and also sendevent command is there which have a pattern of multitouch. At end not getting exact way of implementation. – nikhil84 Sep 02 '14 at 04:24
  • 1
    I stand corrected, I found an interesting link here: http://ktnr74.blogspot.de/2013/06/emulating-touchscreen-interaction-with.html – Raanan Sep 02 '14 at 12:04
  • For simple touch down and swipe adb have commands provided so need to go for such box and coding. For touch adb shell input tap x y and adb shell input swipe x1 y1 x2 y2. – nikhil84 Sep 02 '14 at 13:10
  • Any one who vote down question please do provide reason for same. – nikhil84 Sep 04 '14 at 09:54

2 Answers2

27

You can do it using adb getevent and sendevent.
Connect you device using adb. Follow the steps below.

  1. Identify your Input device:
    Open any image on your device. To list input devices, run

    $ adb shell getevent   
    add device 1: /dev/input/event7   
    name:     "msm8226-tapan9302-snd-card Headset Jack"
    add device 2: /dev/input/event6
      name:     "msm8226-tapan9302-snd-card Button Jack"
    add device 3: /dev/input/event2
      name:     "synaptics_dsx_i2c"
    add device 4: /dev/input/event4
      name:     "qpnp_pon"
    

    Pinch in/out on the image, you should see some continuous logs like

    /dev/input/event2: 0003 0030 00000005
    /dev/input/event2: 0000 0000 00000000
    /dev/input/event2: 0003 002f 00000000
    /dev/input/event2: 0003 0036 00000144
    /dev/input/event2: 0003 003a 00000079
    /dev/input/event2: 0000 0000 00000000
    /dev/input/event2: 0003 0036 00000142
    

    Confirms /dev/input/event2 is the input device name for my target device.

  2. Get exact getevent and convert getevent to sendevent:

    Make sure your screen switched on and is open with some image,
    Run the below command on the prompt.

    $ adb shell getevent | grep dev/input/event2 > getevent_input.txt
    

    While the above is running, Pinch in/out on the image on your phone.

    Once completed, Kill the above command Ctrl + C
    Open file getevent_input.txt and delete first line "add device X: /dev/input/eventX" from it.

    Since getevent returns values in decimal, and sendevent takes value in hexadecimal.
    We have to do the above conversion.
    This script hex_to_dec.py here does the Job. Thanks to this guy!

    $./hex_to_dec.py  getevent_input.txt 
    

    Generates a file getevent_input.scr, Now rename this file to .sh

    $ mv getevent_input.scr  sendevent_input.sh
    

    Open file sendevent_input.sh and delete second line "echoing – drawing function" and save it.

  3. Run on device.
    Transfer sendevent_input.sh to device.

    $ adb push sendevent_input.sh /sdcard/
    1615 KB/s (64379 bytes in 0.038s)
    

    Make sure you device has an image open, and screen is not off.

    $ adb shell sh /sdcard/sendevent_input.sh
    

Worked perfectly fine for me, If this what you looking for.
For visible results, enable Show touches in Developer Options.

Environment: Motorola Moto G (Android 4.4.4) with ADB (v 1.0.31) on Ubuntu 12.04.

Saurabh Meshram
  • 8,106
  • 3
  • 23
  • 32
  • Your answer is outstanding, however on some devices sendevent is really slow and it doesn't work. – Smeterlink Dec 24 '17 at 16:58
  • 1
    @Saurabh Meshram, I followed your answer step by step but can't seem to avoid getting sendevent: /dev/input/event2: Permission denied when executing. Does this require root access? – S. Van den Wyngaert Sep 20 '20 at 12:25
  • In current Android versions access is blocked for adb shell by SELinux policy, even though /dev/input/eventn devices seem to be writable. Therefore sendevent will not work (can't write the device). So yes, you probably need root for that. – Zoltan Peller Jan 03 '23 at 00:57
3

A (kind of hacky) solution with adb shell input looks like

adb shell input tap 200 200 & PIDTAP=$!
sleep 0.1
adb shell input swipe 200 200 200 100 1000 & PIDSWIPE=$!
wait $PIDTAP
wait $PIDSWIPE

The idea is that we launch TAP command, then short sleep and then perform SWIPE. Such commands sequence is interpreted as tap + swipe hence zoom is performed. The example above would be a zoom out on a map.

yunik
  • 53
  • 1
  • 3
Anton Rutkevich
  • 592
  • 1
  • 7
  • 11