Android, adb command to send “touch” screen events to ICS devices (and now Jelly Bean)

Thanks to SoftTeco. I found how to send touch event directly to Android devices. However, his code works for 2.3.3, but not ICS and Jelly Bean. (I did not test 3.x).

However, his great blog not only gives you the code, but also tells you how to get it. After searching valid code for ICS without any result, I just catch the codes using
adb shell getevent | grep event2
Then convert Hex to decimal.
Here is the code to touch the center of my GS2 (240, 400) running AOKP ICS 4.0.X ( I am lazy, this is NOT the newest version of AOKP)
adb shell sendevent /dev/input/event2 3 57 29
adb shell sendevent /dev/input/event2 3 53 240
adb shell sendevent /dev/input/event2 3 54 400
adb shell sendevent /dev/input/event2 3 48 29
adb shell sendevent /dev/input/event2 3 58 2
adb shell sendevent /dev/input/event2 0 0 0
adb shell sendevent /dev/input/event2 3 57 4294967295
adb shell sendevent /dev/input/event2 0 0 0

As you can see, only 8 lines of code is needed now, versus 16 lines for 2.x.

Also, if you run this on DOS cmd, copy all lines and add one more line of something, and paste it all to execute all 8 lines. If you don’t add one more line, you only run 7 lines and result is unpredictable.

You can also put all of them in one line with no new line break:
adb shell "sendevent /dev/input/event2 3 57 29;sendevent /dev/input/event2 3 53 240;sendevent /dev/input/event2 3 54 400;sendevent /dev/input/event2 3 48 29;sendevent /dev/input/event2 3 58 2;sendevent /dev/input/event2 0 0 0;sendevent /dev/input/event2 3 57 4294967295;sendevent /dev/input/event2 0 0 0;"

I did not test it on Jelly Bean, but I think it should be same.
Again, the credit goes to SOFTTE, I am merely using his method to get the code for a different version of Android.

Revision: Tested on Jelly Bean. Different code. So your best shot is getevent.

Advertisements

6 thoughts on “Android, adb command to send “touch” screen events to ICS devices (and now Jelly Bean)

  1. Pingback: A simple low level test automation | Fang Mobile

  2. Here’s a small piece of python code that works for me on a Samsung GT-I9000 with CM10 (JB):

    import os
    import time

    DN=1
    UP=2
    DNUP=UP|DN

    def adbshell(cmd):
    return os.system(“adb shell ‘%s'” % cmd)

    def adbtouch(x,y,mode=DNUP):
    s = ”
    if (mode & DN) == DN:
    s += ‘adb shell sendevent /dev/input/event0 3 53 %d;’ % x
    s += ‘adb shell sendevent /dev/input/event0 3 54 %d;’ % y
    s += ‘adb shell sendevent /dev/input/event0 3 58 71;’
    s += ‘adb shell sendevent /dev/input/event0 3 48 4;’
    s += ‘adb shell sendevent /dev/input/event0 3 57 0;’
    s += ‘adb shell sendevent /dev/input/event0 0 2 0;’
    s += ‘adb shell sendevent /dev/input/event0 0 0 0;’
    if (mode & UP) == UP:
    s += ‘adb shell sendevent /dev/input/event0 0 2 0;’
    s += ‘adb shell sendevent /dev/input/event0 0 0 0;’
    if s != ”:
    adbshell(s)

      • Thanks for the info. I already got the code for JB and am running it. I just did not paste it here and want to leave it as exercise for readers. Don’t worry about the indentation.
        If you are using Python script already then you can simply use Monkeyrunner. use device.touch(,, ‘DOWN_AND_UP’) to touch the on screen. It is OS version and screen resolution independent.

    • You are most likely right Tim! I only know line2 and line 3 are related to coordinates. So to touch another spot on screen , just change those two numbers. I had no idea what the other lines do. Your observation makes total sense.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s