Android adb sendevent无法正确执行事件

2024-05-16 08:34:31 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试录制屏幕事件并在回放后执行它们

我编写了一个小python脚本,它侦听事件,将它们从十六进制转换为十进制,等待5秒钟,并用adb sendevent执行记录的事件

但由于某些原因,sendevent永远不会正确执行,有时它接触到错误的坐标,有时接触时间过长,而且接触之间存在延迟问题

我不明白为什么会这样?我所期望的是它应该只是重放,因为getevent捕获了所有需要的数据(?)

import subprocess
import threading
import os
from time import sleep

eventsToSend = []
def eventSender():
    while(True):
        if(len(eventsToSend) > 200):
            print("starting to execute in 5 seconds...")
            sleep(5)
            for command in eventsToSend:
                #with open('output.txt', 'a') as f1:
                    #f1.write(command+os.linesep)
                subprocess.Popen(command.split(), stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
            print("done")
            break




        else:
            None

eventSenderStarter = threading.Thread(target = eventSender)
eventSenderStarter.start()

def runProcess(exe):    
    p = subprocess.Popen(exe, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
    while(True):
        # returns None while subprocess is running
        retcode = p.poll() 
        line = p.stdout.readline()
        yield line
        if retcode is not None or len(eventsToSend)>200:
            print("Executing events...")
            break

print("Listening for events...")

for line in runProcess('adb shell -- getevent  /dev/input/event1'.split()):
    myLine = line.decode().strip()
    splittedLine = myLine.split(" ")
    decimalString = ""
    for index,hexadecimal in enumerate(splittedLine):
        decimal = int(hexadecimal, 16)
        if(index==0):
            decimalString = decimalString+str(decimal)
        if(index>0):
            decimalString = decimalString+" "+str(decimal)

    eventsToSend.append("adb shell sendevent /dev/input/event1 "+decimalString)

只需将手机连接到pc,然后运行此脚本在200个事件后用屏幕播放它将开始重播(小心,因为它可能按错坐标:p)

/dev/input/event1 

所以您可能需要编辑event1进行测试


Tags: inimportforifline事件commandsubprocess