Skip to content

useElectronMouse() 返回的坐标始终为 {x: 0, y: 0},鼠标位置未自动更新 #1200

@leaft

Description

@leaft

Describe the bug

在windows 上发现, 关联 #1181
以下是错误分析,我不确定是否完全正确,请帮忙看看 @LemonNekoGH @nekomeowww

在这个PR中,main windows context 被设置为 onlySameWindow: true
同时,在 @moeru/eventa 中,有如下逻辑

if (onlySameWindow) {
  if (window.webContents.id === options?.raw.ipcMainEvent.sender.id) {
    window?.webContents?.send(messageEventName, eventBody)
  }
  // 否则:静默丢弃
}

而对于鼠标位置更新事件, cursor loop 由主进程定时主动 emit,没有任何来自渲染进程的 IPC 消息触发它。
这导致 所有主进程主动推送的事件(push-style events)被 onlySameWindow 静默丢弃,渲染进程永远收不到坐标更新。

尝试了直接修改 @moeru/eventa 中的 src\adapters\electron\main.ts,可以解决此问题

  if (onlySameWindow) {
-   if (window.webContents.id === options?.raw.ipcMainEvent.sender.id) {
-     window?.webContents?.send(messageEventName, eventBody)
-   }
+   // NOTICE: When the main process emits events proactively (e.g. a cursor
+   // screen point polling loop), there is no originating IPC message, so
+   // `options` is undefined and `options?.raw.ipcMainEvent` does not exist.
+   // Without this guard the condition `window.webContents.id === undefined`
+   // is always false, silently dropping every push-style event sent from
+   // the main process.
+   // Only apply the same-window filter when the emit originates from a
+   // renderer IPC message.
+   if (!options?.raw?.ipcMainEvent || window.webContents.id === options.raw.ipcMainEvent.sender.id) {
+     window?.webContents?.send(messageEventName, eventBody)
+   }
  }

System Info

System:
    OS: Windows 11 10.0.26200
    CPU: (20) x64 13th Gen Intel(R) Core(TM) i5-13600KF
    Memory: 12.35 GB / 31.85 GB
  Binaries:
    Node: 24.13.0
    npm: 11.6.2
    pnpm: 10.30.3
    bun: 1.2.5
  Browsers:
    Chrome: 145.0.7632.160
    Edge: Chromium (140.0.3485.54)
    Internet Explorer: 11.0.26100.7309

Validations

  • Follow our Code of Conduct
  • Read the Contributing Guide.
  • Check that there isn't already an issue that reports the same bug to avoid creating a duplicate.
  • Check that this is a concrete bug. For Q&A, please open a GitHub Discussion instead.

Contributions

  • I am willing to submit a PR to fix this issue
  • I am willing to submit a PR with failing tests (actually just go ahead and do it, thanks!)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No fields configured for Bug.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions