Commit e69d7be
authored
feat(win32): 添加后台托管按键守护,支持后台长按移动 (#1231)
## 概述
为 Win32 控制器添加**后台托管按键守护**(Background Managed Key
Guardian)能力,支持在后台持续维持指定按键的按下/释放状态。这是"实现终末地后台移动"计划的一部分,解决后台长按 WASD 移动的需求。
## 动机
《明日方舟:终末地》等游戏使用 WASD 控制角色移动,需要按键在后台被持续按住。传统的 `PostMessage`
方式无法可靠地维持按键状态,因为系统层面的热键拦截、焦点切换等因素会导致按键"丢失"。本 PR 引入专用守护线程,以 5ms 间隔轮询 +
差量校正的方式确保按键状态一致性。
## 实现方案
### 核心组件:`BackgroundManagedKeyInput`
- 独立守护线程维护"期望按下键集合"与"受管键域"
- 每 5ms 通过 `GetAsyncKeyState` 检测实际按键状态,与期望状态做差量校正
- 使用 `RegisterHotKey` → `SendInput` → `UnregisterHotKey`
的三步法确保按键注入不被系统吃掉
- 每次按下受管键后发送 F13 nudge(终末地特定需求)
- generation 计数 + condition_variable 实现同步等待(500ms 超时)
### 透明路由机制
- `ControllerAgent` 的 `handle_click_key` / `handle_key_down` /
`handle_key_up` / `handle_long_press_key` 自动检测受管键,命中则走后台守护路径
- Pipeline 层面完全透明,无需修改现有任务定义
### 生命周期管理
- `MaaControllerPostInactive` 自动释放所有受管键
- `post_stop()` 自动 post inactive action 释放持有的输入状态
- 析构函数确保清理
## API 变更
新增 C API:
```c
MaaCtrlId MaaControllerPostSetBackgroundManagedKeys(
MaaController* ctrl, const int32_t* keycodes, MaaSize count);
```
新增 Python / Node.js 绑定:`post_set_background_managed_keys(keys)`
行为变更:
- `MaaControllerPostInactive` 现在会额外释放所有后台受管键
- 任务停止时自动释放持有的输入状态
## 变更范围
- 新增:`BackgroundManagedKeyInput` 守护类(~500 行核心实现)
- 修改:ControllerAgent 路由逻辑、Win32ControlUnitMgr、Tasker 生命周期
- 新增:Agent Client/Server 远程支持
- 新增:Python / Node.js 语言绑定
- 更新:中英文文档
## 测试计划
- [x] Python binding 测试覆盖 `post_set_background_managed_keys`
- [x] Agent 测试覆盖远程托管按键场景
- [x] 终末地实际验证:后台 WASD 长按移动
- [x] 任务停止/inactive 后确认按键正确释放
- [x] 多键同时托管场景验证
---
此 PR 是"实现终末地后台移动"计划的按键控制部分,与 feat/win32-mouse-lock-follow(鼠标锁定跟随)配合使用。
🤖 Generated with [Claude Code](https://claude.com/claude-code)
## Summary by Sourcery
为 Win32 后台托管密钥 guardian 提供支持,并通过 Python、Node.js、agent
客户端/服务端的控制器选项和绑定暴露该功能,同时更新相应的测试和文档。
新特性:
- 为 Win32 控制器引入 `BackgroundManagedKeyInput` guardian,用于在托管域中维护指定按键,并通过该
guardian 路由匹配的按键操作。
- 新增 `MaaCtrlOption_BackgroundManagedKeys` 控制器选项,并将其贯通到
`ControllerAgent`、`Win32ControlUnitMgr` 和 `Win32ControlUnitAPI`。
- 在 Python 和 Node.js 控制器绑定中暴露后台托管密钥配置,并通过 agent 客户端/服务端协议传递该选项。
增强:
- 扩展 Win32 控制管理器的“非活动”处理逻辑,以释放后台托管密钥,并将 guardian 路由集成到点击和按键操作中。
文档:
- 在中英文的集成接口与控制方法指南中,记录 `BackgroundManagedKeys` 控制器选项以及 Win32 后台托管密钥
guardian 的行为。
测试:
- 增加 Python 绑定和 agent 测试,用于验证 Win32 与非 Win32 控制器的后台托管密钥选项行为。
<details>
<summary>Original summary in English</summary>
## Summary by Sourcery
Add Win32 background managed key guardian support and expose it through
controller options and bindings for Python, Node.js, agent
client/server, with tests and docs updated accordingly.
New Features:
- Introduce BackgroundManagedKeyInput guardian for Win32 controllers to
maintain specified keys in a managed domain and route matching key
operations through it.
- Add MaaCtrlOption_BackgroundManagedKeys controller option and wire it
through ControllerAgent, Win32ControlUnitMgr, and Win32ControlUnitAPI.
- Expose background managed key configuration in Python and Node.js
controller bindings and propagate the option over agent client/server
protocols.
Enhancements:
- Extend Win32 control manager inactive handling to release background
managed keys and integrate guardian routing into click and key
operations.
Documentation:
- Document the BackgroundManagedKeys controller option and Win32
background managed key guardian behavior in English and Chinese
integrated interface and control method guides.
Tests:
- Add Python binding and agent tests to validate background managed keys
option behavior for Win32 and non-Win32 controllers.
</details>
<details>
<summary>Original summary in English</summary>
## Summary by Sourcery
为 Win32 后台托管密钥 guardian 提供支持,并通过 Python、Node.js、agent
客户端/服务端的控制器选项和绑定暴露该功能,同时更新相应的测试和文档。
新特性:
- 为 Win32 控制器引入 `BackgroundManagedKeyInput` guardian,用于在托管域中维护指定按键,并通过该
guardian 路由匹配的按键操作。
- 新增 `MaaCtrlOption_BackgroundManagedKeys` 控制器选项,并将其贯通到
`ControllerAgent`、`Win32ControlUnitMgr` 和 `Win32ControlUnitAPI`。
- 在 Python 和 Node.js 控制器绑定中暴露后台托管密钥配置,并通过 agent 客户端/服务端协议传递该选项。
增强:
- 扩展 Win32 控制管理器的“非活动”处理逻辑,以释放后台托管密钥,并将 guardian 路由集成到点击和按键操作中。
文档:
- 在中英文的集成接口与控制方法指南中,记录 `BackgroundManagedKeys` 控制器选项以及 Win32 后台托管密钥
guardian 的行为。
测试:
- 增加 Python 绑定和 agent 测试,用于验证 Win32 与非 Win32 控制器的后台托管密钥选项行为。
<details>
<summary>Original summary in English</summary>
## Summary by Sourcery
Add Win32 background managed key guardian support and expose it through
controller options and bindings for Python, Node.js, agent
client/server, with tests and docs updated accordingly.
New Features:
- Introduce BackgroundManagedKeyInput guardian for Win32 controllers to
maintain specified keys in a managed domain and route matching key
operations through it.
- Add MaaCtrlOption_BackgroundManagedKeys controller option and wire it
through ControllerAgent, Win32ControlUnitMgr, and Win32ControlUnitAPI.
- Expose background managed key configuration in Python and Node.js
controller bindings and propagate the option over agent client/server
protocols.
Enhancements:
- Extend Win32 control manager inactive handling to release background
managed keys and integrate guardian routing into click and key
operations.
Documentation:
- Document the BackgroundManagedKeys controller option and Win32
background managed key guardian behavior in English and Chinese
integrated interface and control method guides.
Tests:
- Add Python binding and agent tests to validate background managed keys
option behavior for Win32 and non-Win32 controllers.
</details>
</details>
---------
Co-authored-by: ZeroAd-06 <190387379+ZeroAd-06@users.noreply.github.com>1 parent 0c34cef commit e69d7be
21 files changed
Lines changed: 722 additions & 1 deletion
File tree
- docs
- en_us
- zh_cn
- include
- MaaControlUnit
- MaaFramework
- source
- MaaAgentClient/Client
- MaaAgentServer/RemoteInstance
- MaaFramework/Controller
- MaaWin32ControlUnit
- Input
- Manager
- binding
- NodeJS/src/apis
- Python/maa
- test
- agent
- python
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
359 | 359 | | |
360 | 360 | | |
361 | 361 | | |
| 362 | + | |
| 363 | + | |
| 364 | + | |
362 | 365 | | |
363 | 366 | | |
364 | 367 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
99 | 99 | | |
100 | 100 | | |
101 | 101 | | |
| 102 | + | |
102 | 103 | | |
103 | 104 | | |
104 | 105 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
359 | 359 | | |
360 | 360 | | |
361 | 361 | | |
| 362 | + | |
| 363 | + | |
| 364 | + | |
362 | 365 | | |
363 | 366 | | |
364 | 367 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
99 | 99 | | |
100 | 100 | | |
101 | 101 | | |
| 102 | + | |
102 | 103 | | |
103 | 104 | | |
104 | 105 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
101 | 101 | | |
102 | 102 | | |
103 | 103 | | |
| 104 | + | |
104 | 105 | | |
105 | 106 | | |
106 | 107 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
234 | 234 | | |
235 | 235 | | |
236 | 236 | | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
237 | 245 | | |
238 | 246 | | |
239 | 247 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2460 | 2460 | | |
2461 | 2461 | | |
2462 | 2462 | | |
| 2463 | + | |
| 2464 | + | |
| 2465 | + | |
| 2466 | + | |
| 2467 | + | |
| 2468 | + | |
| 2469 | + | |
| 2470 | + | |
| 2471 | + | |
| 2472 | + | |
| 2473 | + | |
| 2474 | + | |
| 2475 | + | |
| 2476 | + | |
| 2477 | + | |
| 2478 | + | |
| 2479 | + | |
| 2480 | + | |
| 2481 | + | |
| 2482 | + | |
| 2483 | + | |
2463 | 2484 | | |
2464 | 2485 | | |
2465 | 2486 | | |
| |||
Lines changed: 15 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
42 | 42 | | |
43 | 43 | | |
44 | 44 | | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
45 | 60 | | |
46 | 61 | | |
47 | 62 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
45 | 45 | | |
46 | 46 | | |
47 | 47 | | |
| 48 | + | |
| 49 | + | |
48 | 50 | | |
49 | 51 | | |
50 | 52 | | |
| |||
1250 | 1252 | | |
1251 | 1253 | | |
1252 | 1254 | | |
| 1255 | + | |
| 1256 | + | |
| 1257 | + | |
| 1258 | + | |
| 1259 | + | |
| 1260 | + | |
| 1261 | + | |
| 1262 | + | |
| 1263 | + | |
| 1264 | + | |
| 1265 | + | |
| 1266 | + | |
| 1267 | + | |
| 1268 | + | |
| 1269 | + | |
| 1270 | + | |
| 1271 | + | |
| 1272 | + | |
| 1273 | + | |
| 1274 | + | |
| 1275 | + | |
| 1276 | + | |
| 1277 | + | |
| 1278 | + | |
| 1279 | + | |
| 1280 | + | |
1253 | 1281 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
290 | 290 | | |
291 | 291 | | |
292 | 292 | | |
| 293 | + | |
293 | 294 | | |
294 | 295 | | |
295 | 296 | | |
| |||
0 commit comments