脚本引擎 API
触控操作 API
runtime.touch.* 坐标触控与语义级文字查找触控。支持点击、滑动、长按、多点触控、文字查找与输入。
概述
执行点击、滑动、长按等触控动作。所有触控操作通过无障碍服务或媒体投影注入到设备。带 ? 的参数为可选,省略时使用默认值。
坐标触控方法
基于像素坐标的触控方法。
runtime.touch.tap(x, y, durationMs?: 50): void— 在 (x,y) 处点击。runtime.touch.multiTap(x, y, count, intervalMs?: 100): void— 连点 count 次。runtime.touch.longPress(x, y, durationMs?: 1000): void— 长按指定时长。runtime.touch.swipe(x1, y1, x2, y2, durationMs?: 200): void— 从起点滑动到终点。runtime.touch.dragToTarget(x1, y1, x2, y2, durationMs?: 300): void— 拖拽操作。runtime.touch.touchDown(pointerId, x, y, holdMs?: 0): void— 按下触点。pointerId 为触点编号。runtime.touch.touchUp(pointerId): void— 抬起指定编号的触点。runtime.touch.movePointer(pointerId, endX, endY, durationMs?: 200, pathType?: 'LINE'): void— 移动触点。pathType 可选 LINE/POINT/POLYLINE/CURVE/RECORD。
语义触控方法
基于文字识别的触控方法。不需要精确坐标,系统自动查找匹配的文字。
runtime.touch.findAndClick(text): boolean— 查找屏幕中的文字并点击。找到返回 true。runtime.touch.findAndInput(text, input): boolean— 查找到文字标签后点击聚焦,然后输入内容。runtime.touch.scrollToFind(text, maxScrolls?: 10): boolean— 在可滚动区域逐页向下滑动直到找到文字。runtime.touch.sleep(ms?: 500): void— 阻塞等待指定毫秒数(替代 setTimeout)。
调用示例
// 坐标点击 + 滑动
runtime.touch.tap(540, 1100);
runtime.touch.sleep(800);
runtime.touch.swipe(100, 800, 100, 300, 400);
// 带时长的长按
runtime.touch.longPress(540, 1100, 2000);// 文字查找点击
if (runtime.touch.findAndClick("确认")) {
runtime.process.log("已点击确认按钮");
} else {
runtime.process.warn("未找到确认按钮");
}
// 查找输入框并输入
runtime.touch.findAndInput("搜索", "app");
runtime.touch.sleep(500);
runtime.touch.findAndClick("搜索");// 滚动查找
if (runtime.touch.scrollToFind("条款", 5)) {
runtime.process.log("已找到条款");
runtime.touch.findAndClick("同意");
} else {
runtime.process.error("未找到条款");
}