【发布时间】:2022-02-18 09:59:22
【问题描述】:
我使用 sendevent 来模拟复杂的手势(点击、滑动、长按并在最后滑动)。问题是sendevent很慢,cmd中每个sendevent命令0.1秒,点击需要7个sendevent命令。我非常了解 sendevent 会打开文件、写入、关闭等 7 次。如何优化?
我知道输入点击,但输入比发送事件慢
我在 python 上的代码:
import time
from ppadb.client import Client
adb = Client(host='127.0.0.1', port=5037)
devices = adb.devices()
if len(devices) == 0:
print('Devices not found')
quit()
device = devices[0]
def sendevent(_type, _code, _value, _deviceName='/dev/input/event4'):
last_time = time.time()
device.shell(f"su -c 'sendevent {_deviceName} {_type} {_code} {_value}'")
print(time.time()-last_time)
def TapScreen(x, y):
sendevent(EV_ABS, ABS_MT_ID, 0)
sendevent(EV_ABS, ABS_MT_TRACKING_ID, 0)
sendevent(1, 330, 1)
sendevent(1, 325, 1)
sendevent(EV_ABS, ABS_MT_PRESSURE, 5)
sendevent(EV_ABS, ABS_MT_POSITION_X, x)
sendevent(EV_ABS, ABS_MT_POSITION_Y, y)
sendevent(EV_SYN, SYN_REPORT, 0)
sendevent(EV_ABS, ABS_MT_TRACKING_ID, -1)
sendevent(1, 330, 0)
sendevent(1, 325, 0)
sendevent(EV_SYN, SYN_REPORT, 0)
EV_ABS = 3
EV_SYN = 0
SYN_REPORT = 0
ABS_MT_ID = 47
ABS_MT_TOUCH_MAJOR = 48
ABS_MT_POSITION_X = 53
ABS_MT_POSITION_Y = 54
ABS_MT_TRACKING_ID = 57
ABS_MT_PRESSURE = 58
TapScreen(1000, 500)
是否可以将所有操作写入一个文件,然后将它们作为一个发送事件一起发送?
我找到了问题的答案,现在的问题是在哪里运行代码? :D
回答:
https://stackoverflow.com/questions/18848540/rewrite-sendevent-c-from-c-to-java\
https://gist.github.com/Yhzhtk/6595875
请原谅我的英语,我用谷歌翻译:)
【问题讨论】:
-
您可以尝试绕过 sendevent 可执行文件并自己发送必要的数据:android.googlesource.com/platform/system/core/+/… 这应该允许您发送命令比每次必须执行 su、sendevent 和打开事件目标时快得多并发送代码。