问题
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