Skip to content

USB HID 接口

说明

本页为 自动化接口 / USB HID 模块,与其它开放接口模块并列;对应脚本 usbHidEvent

  • USB HID 开放接口供第三方语言(Python、易语言、C++ 等)通过 HTTP 控制 iPhone,无需蓝牙板 / OTG 板
  • 路径前缀:中控地址,例如 http://127.0.0.1:8019
  • 全部为 POSTContent-Type: application/json
  • 与脚本模块 USB HID 函数(usbHidEvent) 一一对应
  • 适配 EC iOS USB 10.7.0+;手机建议 iOS 17+

通用返回

{
"code": 0,
"msg": "",
"data": ""
}
字段说明
code0 成功,其它失败
msg失败时的错误信息
data部分接口返回业务数据(如剪贴板文本);多数动作接口可为空

会话

POST 开启 USB HID 会话

POST /openapi/usbhidSessionStart

对应 usbHidEvent.sessionStart(gate)。会话已存在时会复用。

{
"deviceId": "90e2f3834c0977205e441aa664916a9bdde81e8d",
"gate": true
}
参数类型必选说明
deviceIdstring设备 ID
gateboolean是否增强兼容模式,默认 true

请求示例

import requests
body = {
"deviceId": "90e2f3834c0977205e441aa664916a9bdde81e8d",
"gate": True
}
r = requests.post("http://127.0.0.1:8019/openapi/usbhidSessionStart", json=body, timeout=30)
print(r.json())

POST 关闭 USB HID 会话

POST /openapi/usbhidSessionStop

{
"deviceId": "90e2f3834c0977205e441aa664916a9bdde81e8d"
}

请求示例

import requests
body = {
"deviceId": "90e2f3834c0977205e441aa664916a9bdde81e8d"
}
r = requests.post("http://127.0.0.1:8019/openapi/usbhidSessionStop", json=body, timeout=30)
print(r.json())

POST 重建 USB HID 会话

POST /openapi/usbhidSessionRestart

点击无响应时可重建会话(对应投屏右键「重建 USBHID 会话」)。

{
"deviceId": "90e2f3834c0977205e441aa664916a9bdde81e8d",
"gate": true
}

请求示例

import requests
body = {
"deviceId": "90e2f3834c0977205e441aa664916a9bdde81e8d",
"gate": True
}
r = requests.post("http://127.0.0.1:8019/openapi/usbhidSessionRestart", json=body, timeout=30)
print(r.json())

屏幕与坐标

POST 设置屏幕尺寸

POST /openapi/usbhidSetScreenSize

按截图/投屏实际分辨率填写。也可用 width / height 代替 w / h

{
"deviceId": "90e2f3834c0977205e441aa664916a9bdde81e8d",
"w": 1170,
"h": 2532
}
参数类型必选说明
deviceIdstring设备 ID
w / widthnumber
h / heightnumber

请求示例

import requests
body = {
"deviceId": "90e2f3834c0977205e441aa664916a9bdde81e8d",
"w": 1170,
"h": 2532
}
r = requests.post("http://127.0.0.1:8019/openapi/usbhidSetScreenSize", json=body, timeout=30)
print(r.json())

触控

坐标与投屏画面、截图像素坐标一致。

POST 点击

POST /openapi/usbhidClickPoint

{
"deviceId": "90e2f3834c0977205e441aa664916a9bdde81e8d",
"x": 200,
"y": 400
}

请求示例

import requests
body = {
"deviceId": "90e2f3834c0977205e441aa664916a9bdde81e8d",
"x": 200,
"y": 400
}
r = requests.post("http://127.0.0.1:8019/openapi/usbhidClickPoint", json=body, timeout=30)
print(r.json())

POST 双击

POST /openapi/usbhidDoubleClickPoint

参数同点击:deviceIdxy

请求示例

import requests
body = {
"deviceId": "90e2f3834c0977205e441aa664916a9bdde81e8d",
"x": 200,
"y": 400
}
r = requests.post("http://127.0.0.1:8019/openapi/usbhidDoubleClickPoint", json=body, timeout=30)
print(r.json())

POST 长按

POST /openapi/usbhidPress

{
"deviceId": "90e2f3834c0977205e441aa664916a9bdde81e8d",
"x": 200,
"y": 400,
"delay": 500
}
参数类型必选说明
delaynumber长按毫秒,默认 500

请求示例

import requests
body = {
"deviceId": "90e2f3834c0977205e441aa664916a9bdde81e8d",
"x": 200,
"y": 400,
"delay": 500
}
r = requests.post("http://127.0.0.1:8019/openapi/usbhidPress", json=body, timeout=30)
print(r.json())

POST 滑动

POST /openapi/usbhidSwipeToPoint

{
"deviceId": "90e2f3834c0977205e441aa664916a9bdde81e8d",
"x": 100,
"y": 800,
"ex": 100,
"ey": 200,
"duration": 500
}
参数类型必选说明
x, ynumber起点
ex, eynumber终点
duration / delaynumber滑动时长毫秒,默认 500

请求示例

import requests
body = {
"deviceId": "90e2f3834c0977205e441aa664916a9bdde81e8d",
"x": 100,
"y": 800,
"ex": 100,
"ey": 200,
"duration": 500
}
r = requests.post("http://127.0.0.1:8019/openapi/usbhidSwipeToPoint", json=body, timeout=30)
print(r.json())

POST 按下 / 移动 / 抬起

  • POST /openapi/usbhidTouchDown
  • POST /openapi/usbhidTouchMove
  • POST /openapi/usbhidTouchUp

参数均为:deviceIdxy

POST /openapi/usbhidTouchDown

请求示例

import requests
body = {
"deviceId": "90e2f3834c0977205e441aa664916a9bdde81e8d",
"x": 200,
"y": 400
}
r = requests.post("http://127.0.0.1:8019/openapi/usbhidTouchDown", json=body, timeout=30)
print(r.json())

POST /openapi/usbhidTouchMove

请求示例

import requests
body = {
"deviceId": "90e2f3834c0977205e441aa664916a9bdde81e8d",
"x": 220,
"y": 420
}
r = requests.post("http://127.0.0.1:8019/openapi/usbhidTouchMove", json=body, timeout=30)
print(r.json())

POST /openapi/usbhidTouchUp

请求示例

import requests
body = {
"deviceId": "90e2f3834c0977205e441aa664916a9bdde81e8d",
"x": 220,
"y": 420
}
r = requests.post("http://127.0.0.1:8019/openapi/usbhidTouchUp", json=body, timeout=30)
print(r.json())

POST 多点触控轨迹

POST /openapi/usbhidMultiTouch

{
"deviceId": "90e2f3834c0977205e441aa664916a9bdde81e8d",
"finger1": [
{ "action": 0, "x": 100, "y": 500, "delay": 20 },
{ "action": 2, "x": 100, "y": 300, "delay": 30 },
{ "action": 1, "x": 100, "y": 300, "delay": 20 }
],
"timeout": 10000
}
参数类型必选说明
finger1 / dataarray/string触点轨迹;action0 按下、2 移动、1 抬起
timeout / delaynumber总超时毫秒,默认 10000

请求示例

import requests
body = {
"deviceId": "90e2f3834c0977205e441aa664916a9bdde81e8d",
"finger1": [
{
"action": 0,
"x": 100,
"y": 500,
"delay": 20
},
{
"action": 2,
"x": 100,
"y": 300,
"delay": 30
},
{
"action": 1,
"x": 100,
"y": 300,
"delay": 20
}
],
"timeout": 10000
}
r = requests.post("http://127.0.0.1:8019/openapi/usbhidMultiTouch", json=body, timeout=30)
print(r.json())

输入与按键

POST 粘贴输入

POST /openapi/usbhidInputText

统一剪贴板粘贴,适合中英文。也可用字段 content

{
"deviceId": "90e2f3834c0977205e441aa664916a9bdde81e8d",
"text": "你好"
}

请求示例

import requests
body = {
"deviceId": "90e2f3834c0977205e441aa664916a9bdde81e8d",
"text": "你好"
}
r = requests.post("http://127.0.0.1:8019/openapi/usbhidInputText", json=body, timeout=30)
print(r.json())

POST 打字输入

POST /openapi/usbhidTypeText

可打印英文走键盘;含中文、emoji 等自动改粘贴。

{
"deviceId": "90e2f3834c0977205e441aa664916a9bdde81e8d",
"text": "hello"
}

请求示例

import requests
body = {
"deviceId": "90e2f3834c0977205e441aa664916a9bdde81e8d",
"text": "hello"
}
r = requests.post("http://127.0.0.1:8019/openapi/usbhidTypeText", json=body, timeout=30)
print(r.json())

POST 系统按键

POST /openapi/usbhidSystemKey

{
"deviceId": "90e2f3834c0977205e441aa664916a9bdde81e8d",
"key": "home"
}
key说明
home主屏
recents多任务
lock锁屏

请求示例

import requests
body = {
"deviceId": "90e2f3834c0977205e441aa664916a9bdde81e8d",
"key": "home"
}
r = requests.post("http://127.0.0.1:8019/openapi/usbhidSystemKey", json=body, timeout=30)
print(r.json())

POST 字符按键 / 组合键

POST /openapi/usbhidKeyPressChar

{
"deviceId": "90e2f3834c0977205e441aa664916a9bdde81e8d",
"prefix": "gui",
"code": "v"
}
参数说明
prefix可空:alt / ctrl / gui / shift / r_ctrl / r_shift
code字符或键名,如 a / enter / BS

请求示例

import requests
body = {
"deviceId": "90e2f3834c0977205e441aa664916a9bdde81e8d",
"prefix": "gui",
"code": "v"
}
r = requests.post("http://127.0.0.1:8019/openapi/usbhidKeyPressChar", json=body, timeout=30)
print(r.json())

POST 按下单个键 / 抬起

  • POST /openapi/usbhidKeyPress — body:deviceIdkey(或 code
  • POST /openapi/usbhidKeyUp — body:deviceId

POST /openapi/usbhidKeyPress

请求示例

import requests
body = {
"deviceId": "90e2f3834c0977205e441aa664916a9bdde81e8d",
"key": "enter"
}
r = requests.post("http://127.0.0.1:8019/openapi/usbhidKeyPress", json=body, timeout=30)
print(r.json())

POST /openapi/usbhidKeyUp

请求示例

import requests
body = {
"deviceId": "90e2f3834c0977205e441aa664916a9bdde81e8d"
}
r = requests.post("http://127.0.0.1:8019/openapi/usbhidKeyUp", json=body, timeout=30)
print(r.json())

POST 音量 / 静音

仅需 deviceId

  • POST /openapi/usbhidVolumeUp
  • POST /openapi/usbhidVolumeDown
  • POST /openapi/usbhidMute

POST /openapi/usbhidVolumeUp

请求示例

import requests
body = {
"deviceId": "90e2f3834c0977205e441aa664916a9bdde81e8d"
}
r = requests.post("http://127.0.0.1:8019/openapi/usbhidVolumeUp", json=body, timeout=30)
print(r.json())

POST /openapi/usbhidVolumeDown

请求示例

import requests
body = {
"deviceId": "90e2f3834c0977205e441aa664916a9bdde81e8d"
}
r = requests.post("http://127.0.0.1:8019/openapi/usbhidVolumeDown", json=body, timeout=30)
print(r.json())

POST /openapi/usbhidMute

请求示例

import requests
body = {
"deviceId": "90e2f3834c0977205e441aa664916a9bdde81e8d"
}
r = requests.post("http://127.0.0.1:8019/openapi/usbhidMute", json=body, timeout=30)
print(r.json())

剪贴板

POST 设置剪贴板

POST /openapi/usbhidSetClipboard

{
"deviceId": "90e2f3834c0977205e441aa664916a9bdde81e8d",
"text": "剪贴板内容"
}

请求示例

import requests
body = {
"deviceId": "90e2f3834c0977205e441aa664916a9bdde81e8d",
"text": "剪贴板内容"
}
r = requests.post("http://127.0.0.1:8019/openapi/usbhidSetClipboard", json=body, timeout=30)
print(r.json())

POST 读取剪贴板

POST /openapi/usbhidGetClipboard

{
"deviceId": "90e2f3834c0977205e441aa664916a9bdde81e8d"
}

成功时文本在返回的 data 中。


与脚本 / BLE 对照

脚本 usbHidEventOpenAPI 接口名说明
sessionStartusbhidSessionStart开启会话
sessionStopusbhidSessionStop关闭会话
sessionRestartusbhidSessionRestart重建会话
setScreenSizeusbhidSetScreenSize屏幕尺寸
clickPointusbhidClickPoint点击
pressusbhidPress长按
swipeToPointusbhidSwipeToPoint滑动
typeText / inputTextusbhidTypeText / usbhidInputText打字 / 粘贴
见本页各接口其余同名对应

请求示例

import requests
body = {
"deviceId": "90e2f3834c0977205e441aa664916a9bdde81e8d"
}
r = requests.post("http://127.0.0.1:8019/openapi/usbhidGetClipboard", json=body, timeout=30)
print(r.json())

蓝牙板方案见 自动化 · 蓝牙 BLE;USB HID 不需要 打开串口、配对开发板等步骤。

更多脚本说明:USB HID 函数 · 投屏场景指引