Hid 方法
Hid 把触摸和按键交给已连接的蓝牙设备发出。系统看成普通键鼠。能用节点时优先 UiObject;Hid 适合游戏或对无障碍检测严的场景。
先开蓝牙权限并连上硬件,见 权限 和 蓝牙硬件。模式对比见 模式介绍。
if (!Access.hasBluetoothConnectionPermission()) {
Access.requestBluetoothConnectionPermission();
System.exit();
}
Hid.initBluetooth(context);
if (Hid.getConnectState()) {
Hid.tap(500, 800);
}| 分组 | 方法 |
|---|---|
| 连接 | initBluetooth / connect / connectAndWait / ensureConnected / isConnected / getConnectState / getConnectedDevices / getName / disconnect |
| 点击滑动 | tap / press / longClick / doubleTap / tapRandom / swipe / swipex / touchDown / touchMove / touchUp |
| 图色联动 | tapImage / pressImage / tapColor / waitImage / waitColor / waitImageGone / clickText / waitText / waitTextGone / swipeUntilImage |
| 文本输入 | type / paste / clear |
| 系统键 | home / back / back1 / recents / power / volUp / volDown |
| 编辑键 | key_copy / key_paste / key_cat / key_select / key_del / key_enter |
| 字符 | key_num / key_abc / keyDown / keyUp / keyPress |
| 设备 | setXY / setRnd / setBattery / reg / reboot / ver |
| 原始数据 | sendData / getData / waitFor |
initBluetooth(context)
参数:
context {object}当前上下文
初始化蓝牙 Hid。Android 12+ 先确认 Access.hasBluetoothConnectionPermission()。
connect(autoconnect, index)
参数:
autoconnect {boolean}是否自动连接index {number}设备索引
返回: {boolean} 是否发出连接
getConnectState()
返回: {boolean} 当前是否已连接
getConnectedDevices()
返回: {BluetoothDevice|null} 已连接设备,没有则 null
getName()
返回: {string} 已连接设备名称
disconnect()
返回: {boolean} 是否断开
tap(x, y)
参数:
x {number}横坐标y {number}纵坐标
返回: {boolean} 是否发出点击
Hid.tap(500, 800);swipe(x1, y1, x2, y2, step, down_timeout, up_timeout, timeout, up_down_times)
参数:
x1 {number}起点横坐标y1 {number}起点纵坐标x2 {number}终点横坐标y2 {number}终点纵坐标step {number}单步距离,建议 5–60,越大越快;默认 20–51 随机down_timeout {number}按下后等多久再滑,默认 100–180 随机up_timeout {number}滑完等多久抬起,默认 100–180 随机timeout {number}每步间隔,默认 8–15 随机up_down_times {number}抬起次数,一般不用,默认 1
返回: {boolean} 是否发出直线滑动
后面几个参数都可以省略。
Hid.swipe(10, 1000, 1000, 1050);swipex(x1, y1, x2, y2, radian, step, down_timeout, up_timeout, timeout, up_down_times)
参数:
x1 {number}起点横坐标y1 {number}起点纵坐标x2 {number}终点横坐标y2 {number}终点纵坐标radian {number}弧度,建议 1–500,常用 10–100;默认 10–100 随机step/down_timeout/up_timeout/timeout/up_down_times含义同swipe
返回: {boolean} 是否发出曲线滑动
Hid.swipex(10, 1000, 1000, 1050);touchDown(x, y)
参数:
x {number}横坐标y {number}纵坐标
返回: {boolean} 是否按下
自己拼手势时:先 touchDown,再 touchMove,最后 touchUp。普通点击用 tap。
touchMove(x, y)
参数:
x {number}横坐标y {number}纵坐标
返回: {boolean} 是否移动
touchUp(x, y)
参数:
x {number}横坐标y {number}纵坐标
返回: {boolean} 是否在该点抬起
touchUp()
返回: {boolean} 在最后一次触摸位置抬起
touchUp2()
返回: {boolean} 多次尝试抬起,手势没收干净时用
home()
返回: {boolean} 是否发出 Home
recents()
返回: {boolean} 是否发出最近任务键
back()
返回: {boolean} 是否发出返回
back1()
返回: {boolean} 是否发出返回(另一种实现,back 无效时再试)
power()
返回: {boolean} 是否短按电源键
power(time)
参数:
time {number}按住毫秒数
返回: {boolean} 是否按住电源键指定时长
volUp()
返回: {boolean} 是否发出音量加
volDown()
返回: {boolean} 是否发出音量减
keyDown(code)
参数:
code {number}键码
返回: {boolean} 是否按下该键
keyUp(code)
参数:
code {number}键码
返回: {boolean} 是否抬起该键
keyPress(code)
参数:
code {number}键码
返回: {boolean} 是否完成一次按下再抬起
keyPress_code(code)
参数:
code {number}键码
返回: {boolean} 是否发出特定键的按下再抬起,和 keyPress 不是同一路实现
keyDown_code(code)
参数:
code {number}键码
返回: {boolean} 是否按下特定键,和 keyDown 不是同一路实现
keyUp_code(code)
参数:
code {number}键码
返回: {boolean} 是否抬起特定键,和 keyUp 不是同一路实现
keyUpAll()
返回: {boolean} 是否松开当前按下的全部键
key_select()
返回: {boolean} 是否发出全选
key_copy()
返回: {boolean} 是否发出复制
key_paste()
返回: {boolean} 是否发出粘贴
key_cat()
返回: {boolean} 是否发出剪切
key_del()
返回: {boolean} 是否发出退格
key_delete()
返回: {boolean} 是否发出删除
key_enter()
返回: {boolean} 是否发出回车
key_num(n)
参数:
n {number}数字 0–9
返回: {boolean} 是否发出该数字键
key_abc(n)
参数:
n {string}单个字母
返回: {boolean} 是否发出该字母键
setXY(x, y)
参数:
x {number}宽度y {number}高度
返回: {boolean} 是否设置分辨率
坐标对不上时先对一下屏幕宽高。
setRnd(x, y)
参数:
x {number}随机下限y {number}随机上限
返回: {boolean} 是否设置点击延时随机范围
setBattery(lv)
参数:
lv {number}电量百分比
返回: {boolean} 是否设置硬件上报电量
reg(key)
参数:
key {string}注册密钥
返回: {boolean} 是否注册成功
reboot()
返回: {boolean} 是否重启蓝牙主板
ver()
返回: {number} 插件版本号
sendData(str)
参数:
str {string}发给硬件的数据
返回: {boolean} 是否发出
sendDataAwait(str, time)
参数:
str {string}发给硬件的数据time {number}等待响应的毫秒数
返回: {boolean} 是否在时限内收到响应
getData(time)
参数:
time {number}等待毫秒数
返回: {string} 读到的数据
getData()
返回: {string} 最新一次收到的数据
waitFor(time, sleep)
参数:
time {number}最长等待毫秒数sleep {number}轮询间隔毫秒数
返回: {string} 数据;超时则返回超时提示
waitFor(time)
参数:
time {number}最长等待毫秒数
返回: {string} 数据或超时提示,间隔用默认值
waitFor()
返回: {string} 一直等到有数据或连接断开
高层封装(图色 / 输入 / 连接)
以下方法由引擎封装,不依赖无障碍。找图找色需先开启 图色权限;识别细节见 图片 / 取色 / OCR。
press(x, y, durationMs)
参数:
x {number}y {number}坐标durationMs {number}按住毫秒,默认约800
返回: {boolean}
Hid.press(500, 800, 1000);longClick(x, y)
长按(内部调用 press)。
Hid.longClick(500, 800);doubleTap(x, y)
双击。
Hid.doubleTap(540, 960);tapRandom(x, y, rx, ry)
在 (x±rx, y±ry) 范围内随机点击;rx/ry 默认 5。
Hid.tapRandom(500, 800, 10, 10);type(text)
整串输入。纯 ASCII 尽量逐键;含中文等走剪贴板 + key_paste。
Hid.type("hello");
Hid.type("你好世界");paste(text)
写入剪贴板并粘贴。
Hid.paste("粘贴这段文字");clear()
全选后删除(清空输入框)。
Hid.clear();tapImage(templateFile, threshold)
截屏找图,点击模板中心。
参数:
templateFile {string}模板图路径threshold {number}匹配阈值,默认0.8
返回: {ImageMatch|null} 含 x y centerX centerY score width height
Hid.ensureConnected();
let m = Hid.tapImage("/sdcard/btn.png", 0.85);
if (m) {
console.log(m.centerX, m.centerY, m.score);
}pressImage(templateFile, durationMs, threshold)
找图后长按。
let m = Hid.pressImage("/sdcard/icon.png", 1000, 0.8);
console.log(m);tapColor(color, tolerance)
截屏找色(色块中心)后点击。color 支持 rgba / rgb / #RRGGBB;tolerance 为通道容差,默认 0。
let p = Hid.tapColor("#3480F8", 10);
if (p) {
console.log(p.x, p.y);
}waitImage(templateFile, timeoutMs, intervalMs, threshold)
等到图出现,返回 ImageMatch;超时返回 null。默认超时 10000,间隔 500。
let m = Hid.waitImage("/sdcard/ok.png", 10000);
if (m) {
console.log("出现", m.centerX, m.centerY);
}waitColor(color, timeoutMs, intervalMs, tolerance)
等到颜色出现。
let p = Hid.waitColor("#FF0000", 8000, 500, 5);
console.log(p);waitImageGone(templateFile, timeoutMs, intervalMs, threshold)
等到图消失,返回 {boolean}。
if (Hid.waitImageGone("/sdcard/loading.png", 10000)) {
console.log("加载图已消失");
}clickText(keyword, timeoutMs, mode)
OCR 找字后点击中心。mode:contains(默认)/ exact / regex。
Hid.clickText("登录", 5000);
Hid.clickText("确定", 5000, "exact");
Hid.clickText("\\d{4}", 5000, "regex");waitText(keyword, timeoutMs, intervalMs, mode)
等到文字出现,返回 {text, rect, confidence}。
let hit = Hid.waitText("加载完成", 10000);
if (hit) {
console.log(hit.text, hit.confidence);
}waitTextGone(keyword, timeoutMs, intervalMs, mode)
等到文字消失。
if (Hid.waitTextGone("加载中", 8000)) {
console.log("加载文案消失");
}connectAndWait(autoconnect, index, timeoutMs)
发起连接并等到就绪。默认 autoconnect=true,index=0,超时 10000。
if (Hid.connectAndWait(true, 0, 10000)) {
console.log("已连接");
}ensureConnected()
未连接则自动 connectAndWait;已连接直接返回 true。
if (!Hid.ensureConnected()) {
console.log("连接失败");
}isConnected()
是否已连接(真实布尔,与 getConnectState 相同实现)。
console.log(Hid.isConnected());swipeUntilImage(x1, y1, x2, y2, templateFile, maxTimes, threshold)
按给定方向滑动,直到出现目标图或达到 maxTimes(默认 10)。
let m = Hid.swipeUntilImage(500, 1500, 500, 500, "/sdcard/item.png", 8, 0.8);
if (m) {
Hid.tap(m.centerX, m.centerY);
}