fix(Win32-Input): 支持系统鼠标左右键互换设置#1330
Open
laoyijiehe wants to merge 3 commits into
Open
Conversation
Contributor
There was a problem hiding this comment.
Hey - 我发现了 1 个问题,并给出了一些整体层面的反馈:
- 在
IsMouseButtonSwapped()中缓存GetSystemMetrics(SM_SWAPBUTTON)的结果,会导致运行时更改系统鼠标按键设置时无法被检测到;建议要么每次都查询,要么在接收到WM_SETTINGCHANGE时增加一个刷新该值的机制。 GetMappedContact对任意original_contact都直接应用^ 1,这会改变超出 0/1 范围的值;如果这里只允许 0/1,有必要通过断言或限制范围来明确这一点,或者在文档中显式说明这个假设。
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- Caching the result of `GetSystemMetrics(SM_SWAPBUTTON)` in `IsMouseButtonSwapped()` means changes to the system mouse button setting at runtime won’t be picked up; consider either querying each time or adding a mechanism to refresh this value on `WM_SETTINGCHANGE`.
- `GetMappedContact` blindly applies `^ 1` to any `original_contact`, which will change values beyond 0/1; if only 0/1 are valid here, it would be clearer to assert or clamp the range, or document this assumption explicitly.
## Individual Comments
### Comment 1
<location path="source/MaaWin32ControlUnit/Input/InputUtils.h" line_range="66-68" />
<code_context>
+}
+
+// 获取兼容鼠标左右按键交换后的鼠标按键映射
+inline int GetMappedContact(int original_contact)
+{
+ if (IsMouseButtonSwapped()) {
+ // 左右键交换后,左键为1,右键为0,异或取反输出即可
+ return original_contact ^ 1;
</code_context>
<issue_to_address>
**issue (bug_risk):** 基于异或的映射会错误地重新映射非左右键的按键(例如中键/X 键)。
`original_contact ^ 1` 只有在 `original_contact` 被保证仅为 0 或 1 时才是正确的。按当前对 0–4 这些 contact 的处理方式,它还会把 2↔3、4↔5 等也翻转,从而在交换左右键时改变中键和 X 键的行为。相反,应当只交换 0 和 1,并保持其它所有 contact 不变,例如:
```cpp
inline int GetMappedContact(int original_contact)
{
if (!IsMouseButtonSwapped()) {
return original_contact;
}
if (original_contact == 0) return 1;
if (original_contact == 1) return 0;
return original_contact;
}
```
</issue_to_address>帮我变得更有用!请在每条评论上点 👍 或 👎,我会根据你的反馈改进后续的 Review。
Original comment in English
Hey - I've found 1 issue, and left some high level feedback:
- Caching the result of
GetSystemMetrics(SM_SWAPBUTTON)inIsMouseButtonSwapped()means changes to the system mouse button setting at runtime won’t be picked up; consider either querying each time or adding a mechanism to refresh this value onWM_SETTINGCHANGE. GetMappedContactblindly applies^ 1to anyoriginal_contact, which will change values beyond 0/1; if only 0/1 are valid here, it would be clearer to assert or clamp the range, or document this assumption explicitly.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- Caching the result of `GetSystemMetrics(SM_SWAPBUTTON)` in `IsMouseButtonSwapped()` means changes to the system mouse button setting at runtime won’t be picked up; consider either querying each time or adding a mechanism to refresh this value on `WM_SETTINGCHANGE`.
- `GetMappedContact` blindly applies `^ 1` to any `original_contact`, which will change values beyond 0/1; if only 0/1 are valid here, it would be clearer to assert or clamp the range, or document this assumption explicitly.
## Individual Comments
### Comment 1
<location path="source/MaaWin32ControlUnit/Input/InputUtils.h" line_range="66-68" />
<code_context>
+}
+
+// 获取兼容鼠标左右按键交换后的鼠标按键映射
+inline int GetMappedContact(int original_contact)
+{
+ if (IsMouseButtonSwapped()) {
+ // 左右键交换后,左键为1,右键为0,异或取反输出即可
+ return original_contact ^ 1;
</code_context>
<issue_to_address>
**issue (bug_risk):** XOR-based mapping will incorrectly remap non-left/right contacts (e.g., middle/X buttons).
`original_contact ^ 1` only works if `original_contact` is guaranteed to be 0 or 1. With the current handling of contacts 0–4, it will also remap 2↔3, 4↔5, etc., changing the behavior of middle/X buttons when swapping. Instead, only swap 0 and 1 and leave all other contacts unchanged, for example:
```cpp
inline int GetMappedContact(int original_contact)
{
if (!IsMouseButtonSwapped()) {
return original_contact;
}
if (original_contact == 0) return 1;
if (original_contact == 1) return 0;
return original_contact;
}
```
</issue_to_address>Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
5c24347 to
2ead700
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
问题描述
MaaFramework v5.10.4 Win32 输入层将
contact=0/1直接硬编码为左/右键消息或SendInput标志,未读取系统SM_SWAPBUTTON设置。Windows 开启「左右键互换」后:
修复方案
在
InputUtils.h中统一做逻辑主键/副键 remap:IsMouseButtonSwapped()读取GetSystemMetrics(SM_SWAPBUTTON)GetMappedContact()使用异或^1实现 0↔1 互换contact_to_xxx函数,使用映射后的按键修改的文件
source/MaaWin32ControlUnit/Input/InputUtils.h关联 Issue
Fixes #1323
说明
此 PR 替代已关闭的旧 PR(#1329)。
Summary by Sourcery
在 Win32 输入工具中添加对 Windows 系统“鼠标主次按钮交换”设置的支持,使逻辑接触点能够遵从用户的左右键配置。
Bug Fixes:
SeizeInput和MessageInput能够正确处理点击事件。Enhancements:
Original summary in English
Summary by Sourcery
Add support for Windows system mouse button swap settings in the Win32 input utilities so logical contacts respect the user’s left/right button configuration.
Bug Fixes:
Enhancements: