模拟鼠标单击窗口而不是屏幕

时间:2022-11-03 18:11:22

Is it possible in MacOS X to send a mouse click to a specific window?

在MacOS X中是否可以将鼠标单击发送到特定窗口?

I already managed to send a click to the entire screen via CGPostMouseEvent. The windows i want to control overlap each other, so my next idea was to bring the proper window to the front before triggering the click. It works but ends in a total mess... ;-)

我已经设法通过CGPostMouseEvent向整个屏幕发送一个点击。我想要控制的窗口相互重叠,所以我的下一个想法是在触发点击之前将正确的窗口带到前面。它的工作原理却以一团糟的方式结束...... ;-)

3 个解决方案

#1


It is possible to send events to Cocoa applications via the undocumented

可以通过未记录的事件将事件发送到Cocoa应用程序

CGEventPostToPSN

Here is some code sample from Dave Keck. He posted a little application on the mailing list.

以下是Dave Keck的一些代码示例。他在邮件列表上发布了一个小应用程序。

customEvent = [NSEvent mouseEventWithType: [event type]
                                 location: [event locationInWindow]
                            modifierFlags: [event modifierFlags] | NSCommandKeyMask
                                timestamp: [event timestamp]
                             windowNumber: WID
                                  context: nil
                              eventNumber: 0
                               clickCount: 1
                                 pressure: 0];

CGEvent = [customEvent CGEvent];
CGEventPostToPSN(&psn, CGEvent);

To archive it, i pasted more source code on pastie.org

为了存档,我在pastie.org上粘贴了更多的源代码

For reference: The whole thread on cocao-dev mailing list

供参考:cocao-dev邮件列表上的整个帖子

#2


You can use probably use the Accessibility APIs.

您可以使用可能使用辅助功能API。

It's a bit more complicated, but it should work.

它有点复杂,但它应该工作。

#3


Maybe you can use the Window Manager API: SetUserFocusWindow() and then create the mouse event.

也许您可以使用Window Manager API:SetUserFocusWindow()然后创建鼠标事件。

#1


It is possible to send events to Cocoa applications via the undocumented

可以通过未记录的事件将事件发送到Cocoa应用程序

CGEventPostToPSN

Here is some code sample from Dave Keck. He posted a little application on the mailing list.

以下是Dave Keck的一些代码示例。他在邮件列表上发布了一个小应用程序。

customEvent = [NSEvent mouseEventWithType: [event type]
                                 location: [event locationInWindow]
                            modifierFlags: [event modifierFlags] | NSCommandKeyMask
                                timestamp: [event timestamp]
                             windowNumber: WID
                                  context: nil
                              eventNumber: 0
                               clickCount: 1
                                 pressure: 0];

CGEvent = [customEvent CGEvent];
CGEventPostToPSN(&psn, CGEvent);

To archive it, i pasted more source code on pastie.org

为了存档,我在pastie.org上粘贴了更多的源代码

For reference: The whole thread on cocao-dev mailing list

供参考:cocao-dev邮件列表上的整个帖子

#2


You can use probably use the Accessibility APIs.

您可以使用可能使用辅助功能API。

It's a bit more complicated, but it should work.

它有点复杂,但它应该工作。

#3


Maybe you can use the Window Manager API: SetUserFocusWindow() and then create the mouse event.

也许您可以使用Window Manager API:SetUserFocusWindow()然后创建鼠标事件。