Skip to content

fix(MacOS): global event 的一些小补丁#1233

Merged
MistEO merged 11 commits intoMaaXYZ:mainfrom
YJBeetle:MacOSGlobalEventPatch
Apr 1, 2026
Merged

fix(MacOS): global event 的一些小补丁#1233
MistEO merged 11 commits intoMaaXYZ:mainfrom
YJBeetle:MacOSGlobalEventPatch

Conversation

@YJBeetle
Copy link
Copy Markdown
Contributor

@YJBeetle YJBeetle commented Mar 26, 2026

几点更新:

  • touch down 时候重新更新窗口位置,避免期间窗口被拖动情况
  • 截图时候不截鼠标 避免影响识别
  • touch down时候记录鼠标位置 Up之后还原鼠标位置

由 Sourcery 提供的摘要

调整 macOS 全局输入处理和截图行为,以更好地保持光标状态并避免将光标捕捉到截图中。

错误修复:

  • 在按下触控时刷新窗口信息,以处理在注入输入之前目标窗口已被移动的情况。
  • 在模拟触控事件前后保存并恢复系统光标位置,使交互结束后光标位置保持不变。
  • 在使用 ScreenCaptureKit 截图时禁用光标渲染,避免鼠标指针影响识别效果。

增强改进:

  • 在 macOS 输入路径中使用 std::ignore 统一处理未使用的压力(pressure)参数。
Original summary in English

Summary by Sourcery

Adjust macOS global input handling and screenshot behavior to better preserve cursor state and avoid capturing the cursor.

Bug Fixes:

  • Refresh window info on touch down to handle cases where the target window was moved before input injection.
  • Save and restore the system cursor position around simulated touch events so cursor location is unchanged after interactions.
  • Disable cursor rendering in ScreenCaptureKit screenshots to avoid the mouse pointer affecting recognition.

Enhancements:

  • Standardize unused pressure parameter handling using std::ignore in macOS input paths.

Copy link
Copy Markdown
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - 我发现了 1 个问题,并给出了一些高层次的反馈:

  • 为所有 touch_down 调用共用同一个 saved_cursor_pos_,在多点触控或手势重叠的情况下可能会产生异常行为;建议为每个触点分别跟踪光标位置,或者仅在对应的 touch_down / touch_up 配对完成时再恢复光标。
  • touch_up 中调用 CGDisplayMoveCursorToPoint 之前,最好先记录是否确实保存过有效的光标位置(例如使用一个布尔标记),以避免在 touch_down 被跳过或失败时,将光标恢复到未初始化或过期的位置。
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- 为所有 `touch_down` 调用共用同一个 `saved_cursor_pos_`,在多点触控或手势重叠的情况下可能会产生异常行为;建议为每个触点分别跟踪光标位置,或者仅在对应的 `touch_down` / `touch_up` 配对完成时再恢复光标。
-`touch_up` 中调用 `CGDisplayMoveCursorToPoint` 之前,最好先记录是否确实保存过有效的光标位置(例如使用一个布尔标记),以避免在 `touch_down` 被跳过或失败时,将光标恢复到未初始化或过期的位置。

## Individual Comments

### Comment 1
<location path="source/MaaMacOSControlUnit/Input/GlobalEventInput.mm" line_range="104-105" />
<code_context>
         return false;
     }

+    // 恢复鼠标到 touch_down 之前的位置
+    CGDisplayMoveCursorToPoint(CGMainDisplayID(), saved_cursor_pos_);
+
     return true;
</code_context>
<issue_to_address>
**issue:** 考虑一下在没有先调用 touch_down 的情况下调用 touch_up 会发生什么(此时 saved_cursor_pos_ 仍是默认初始化)。

由于 `saved_cursor_pos_` 是默认初始化的,并且在 `touch_up` 中总会被使用,如果在没有先成功调用 `touch_down` 的情况下调用 `touch_up`,光标会被重置到 (0,0)。请在恢复前增加一个检查,确认确实保存过有效的位置(例如使用布尔标记,或跟踪对应触点是否已经开始)。
</issue_to_address>

Sourcery 对开源项目是免费的——如果你觉得这些代码评审有帮助,欢迎分享 ✨
帮我变得更有用!请在每条评论上点 👍 或 👎,我会根据你的反馈改进后续评审。
Original comment in English

Hey - I've found 1 issue, and left some high level feedback:

  • Using a single saved_cursor_pos_ for all touch_down calls may misbehave with multi-touch or overlapping gestures; consider tracking cursor position per contact or only restoring when the corresponding touch_down/touch_up pair completes.
  • Before calling CGDisplayMoveCursorToPoint in touch_up, it may be safer to track whether a valid cursor position was actually saved (e.g., with a boolean flag) to avoid restoring to an uninitialized or stale position if touch_down was skipped or failed.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- Using a single `saved_cursor_pos_` for all `touch_down` calls may misbehave with multi-touch or overlapping gestures; consider tracking cursor position per contact or only restoring when the corresponding `touch_down`/`touch_up` pair completes.
- Before calling `CGDisplayMoveCursorToPoint` in `touch_up`, it may be safer to track whether a valid cursor position was actually saved (e.g., with a boolean flag) to avoid restoring to an uninitialized or stale position if `touch_down` was skipped or failed.

## Individual Comments

### Comment 1
<location path="source/MaaMacOSControlUnit/Input/GlobalEventInput.mm" line_range="104-105" />
<code_context>
         return false;
     }

+    // 恢复鼠标到 touch_down 之前的位置
+    CGDisplayMoveCursorToPoint(CGMainDisplayID(), saved_cursor_pos_);
+
     return true;
</code_context>
<issue_to_address>
**issue:** Consider what happens if touch_up is called without a prior touch_down (saved_cursor_pos_ is default-initialized).

Because `saved_cursor_pos_` is default-initialized and always used on `touch_up`, calling `touch_up` without a prior successful `touch_down` will reset the cursor to (0,0). Please gate the restore behind a check that a valid position was actually saved (e.g., a boolean flag or tracking that the corresponding contact started).
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@YJBeetle YJBeetle changed the title MacOS global event 的一些小补丁 fix(MacOS): global event 的一些小补丁 Mar 26, 2026
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR refines macOS control-unit behavior for input injection and screencap to better preserve user cursor state and prevent the cursor from affecting image recognition.

Changes:

  • Disable cursor rendering in ScreenCaptureKit screenshots (showsCursor = NO).
  • Refresh target window info on touch_down for GlobalEvent input to handle window movement before injection.
  • Save and restore system cursor position around GlobalEvent touch interactions; standardize unused pressure handling via std::ignore.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 4 comments.

File Description
source/MaaMacOSControlUnit/Screencap/ScreenCaptureKitScreencap.mm Configure ScreenCaptureKit capture to not include the cursor.
source/MaaMacOSControlUnit/Input/PostToPidInput.mm Replace (void)pressure with std::ignore = pressure.
source/MaaMacOSControlUnit/Input/GlobalEventInput.mm Update window info on touch down; save/restore cursor position; use std::ignore for pressure.
source/MaaMacOSControlUnit/Input/GlobalEventInput.h Add member to store saved cursor position.

@MistEO MistEO requested a review from neko-para March 27, 2026 19:20
@neko-para
Copy link
Copy Markdown
Contributor

看起来没啥问题,ai提到的可以保护下

@YJBeetle
Copy link
Copy Markdown
Contributor Author

YJBeetle commented Apr 1, 2026

ai提到的问题都修了 这些建议提的很可以 看下没问题的话可以合并了

@MistEO MistEO merged commit 3eb20a9 into MaaXYZ:main Apr 1, 2026
19 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants