Skip to content

HID

hid 是 Aimaxbot 注入到脚本运行环境中的全局对象,用于通过蓝牙 HID 设备执行点击、滑动、输入、导航和常用按键操作。

脚本中直接使用全局 hid 对象,无需 import

使用前请先完成 HID 设备烧录HID 使用设置


连接状态

方法返回值说明
hid.enable()void开启 HID 模式,并开始扫描/连接 HID 设备
hid.disable()void关闭 HID 模式,并断开 HID 设备
hid.connect()void手动触发连接
hid.disconnect()void手动断开连接
hid.ensureConnected()void等待连接成功,默认最多等待 15 秒
hid.ensureConnected(timeoutMs)void等待连接成功,最多等待 timeoutMs 毫秒
hid.isEnabled()booleanHID 模式开关是否已开启
hid.isConnected()booleanHID 设备当前是否已连接
hid.getState()string当前 HID 连接状态

状态值:

状态说明
DISABLEDHID 模式已关闭
IDLE已初始化,等待连接
SCANNING正在扫描 HID 设备
CONNECTING正在建立连接
WAITING_HOST已连接 GATT,等待系统 HID Host 接管
CONNECTED已连接,可以发送操作
RECONNECTING连接断开,正在重连
ERROR连接异常

发送操作前建议先调用:

javascript
hid.ensureConnected(15000);

Android 导航

方法说明
hid.home()返回桌面
hid.back()返回上一页
hid.recents()打开最近任务,适用于原生 Android / 小米 / 华为 / vivo
hid.recentsV2()打开最近任务,适用于 ColorOS:OPPO / OnePlus / realme
hid.recentsGesture(screenWidth, screenHeight)通过底部上划手势打开最近任务,适用于手势导航兜底
javascript
hid.home();
sleep(1000);
hid.back();

媒体控制

方法说明
hid.volumeUp()音量加
hid.volumeDown()音量减
hid.playPause()播放 / 暂停
hid.prevTrack()上一曲
hid.nextTrack()下一曲
hid.stop()停止播放

常用键盘按键

方法说明
hid.enter()回车
hid.tab()Tab
hid.esc()Esc
hid.space()空格
hid.backspace()退格
hid.del()Delete
hid.arrowUp()方向上
hid.arrowDown()方向下
hid.arrowLeft()方向左
hid.arrowRight()方向右
hid.pageUp()Page Up
hid.pageDown()Page Down
hid.homeKey()键盘 Home
hid.endKey()End

剪贴板快捷键

这些方法通过常见键盘快捷键实现,适合文本框、WebView、输入区域等场景。

方法说明
hid.selectAll()全选
hid.cut()剪切
hid.copy()复制
hid.paste()粘贴
hid.undo()撤销
javascript
hid.selectAll();
hid.copy();
hid.paste();

触摸操作

触摸方法需要传入目标手机屏幕尺寸:

参数说明
screenWidth目标手机屏幕宽度,单位像素
screenHeight目标手机屏幕高度,单位像素
x / y目标手机屏幕坐标

可以直接使用 device.widthdevice.height

javascript
var sw = device.width;
var sh = device.height;
hid.tap(sw / 2, sh / 2, sw, sh);

点击

方法说明
hid.tap(x, y, screenWidth, screenHeight)点击
hid.click(x, y, screenWidth, screenHeight)点击,等同于 tap
hid.press(x, y, durationMs, screenWidth, screenHeight)按压指定时长
hid.longClick(x, y, screenWidth, screenHeight)长按,默认 700 毫秒
hid.longClick(x, y, durationMs, screenWidth, screenHeight)长按指定时长
hid.doubleClick(x, y, screenWidth, screenHeight)双击
hid.clicks(x, y, count, screenWidth, screenHeight)连续点击,默认间隔 100 毫秒
hid.clicks(x, y, count, intervalMs, screenWidth, screenHeight)连续点击,并指定点击间隔

滑动

方法说明
hid.swipe(x1, y1, x2, y2, durationMs, screenWidth, screenHeight)从起点滑动到终点
hid.swipeFast(x1, y1, x2, y2, screenWidth, screenHeight)快速滑动,适合翻页或列表快速滚动
hid.swipeMultiple(segments, durationMs, screenWidth, screenHeight)连续多段滑动

swipeMultiplesegments 是一维数组,每 4 个数字表示一段滑动:[x1, y1, x2, y2]

javascript
var sw = device.width;
var sh = device.height;

hid.swipe(sw / 2, sh * 0.8, sw / 2, sh * 0.3, 400, sw, sh);

hid.swipeMultiple([
  sw / 2, sh * 0.7, sw / 2, sh * 0.4,
  sw / 2, sh * 0.4, sw / 2, sh * 0.6
], 300, sw, sh);

文本输入

javascript
hid.text("Hello Aimaxbot 123");

hid.text() 会逐字符发送键盘输入,适合已经聚焦的输入框。支持常见英文、数字、空格、换行、Tab 和常用符号。

javascript
var sw = device.width;
var sh = device.height;

hid.tap(sw / 2, 300, sw, sh);
sleep(300);
hid.text("hello");
hid.enter();

完整示例

javascript
var sw = device.width;
var sh = device.height;
var cx = sw / 2;
var cy = sh / 2;

hid.ensureConnected(15000);
log("HID state: " + hid.getState());

hid.home();
sleep(1000);

hid.tap(cx, cy, sw, sh);
sleep(500);

hid.swipe(cx, sh * 0.75, cx, sh * 0.25, 400, sw, sh);
sleep(800);

hid.tap(cx, 300, sw, sh);
sleep(300);
hid.text("Hello Aimaxbot");
hid.enter();

hid.selectAll();
sleep(300);
hid.copy();

hid.back();