Tutorial Example

Python Get Android Phone Touch Event Coordinate using ADB – ADB Tutorial

When you use your finger to touch the screen of an android phone. What is the coordinate of the position you have touched? In this tutorial, we will use python and adb to get the touch coordinate.

We will use adb command adb shell getevent to get useful information.

adb shell getevent

Run this command, you will get:

From above, we can find:

0035 is the coordinate x of touch event, and 0036 is coordinate y.

We will use python subprocess.Popen() to get the output of adb shell getevent and get coordinate (x, y) of touch event on android phone.

You can refer this tutorial to get output.

Implement Python subprocess.Popen(): Execute an External Command and Get Output

Wew will write an example to show this coordinate. Here is an example code.

import subprocess

def get_xy():
    cmd = r'adb shell getevent'
    w = 0
    h = 0
    try:
        p1=subprocess.Popen(cmd,shell=True,stdout=subprocess.PIPE)
        for line in p1.stdout:
            line = line.decode(encoding="utf-8", errors="ignore")
            line = line.strip()

            if ' 0035 ' in line:
                e = line.split(" ")
                w = e[3]
                w = int(w, 16)
                
            if  ' 0036 ' in line:
                e = line.split(" ")
                h = e[3]
                h = int(h, 16)
                if h >0:
                    p = (w, h)
                    print(p) 
        p1.wait()
        
    except Exception as e:
        print(e)

size = get_xy()
print(size)

Run this code, you will get:

After having got touch event coordinate, we can run these touch events by adb easily.