Ben
Ben

Reputation:

Simulate mouse click to window instead of screen

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

Upvotes: 1

Views: 3464

Answers (3)

Ben
Ben

Reputation:

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

Upvotes: 4

user39113
user39113

Reputation: 297

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

Upvotes: 1

Ecton
Ecton

Reputation: 10722

You can use probably use the Accessibility APIs.

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

Upvotes: 1

Related Questions