Simulate mouse click to window instead of screen

孤人 提交于 2019-12-08 05:38:43

问题


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

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... ;-)


回答1:


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

CGEventPostToPSN

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

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

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




回答2:


You can use probably use the Accessibility APIs.

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.



来源:https://stackoverflow.com/questions/726952/simulate-mouse-click-to-window-instead-of-screen

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!