Mouse click simulation on Mac OS X does not work for some applications

I am writing an application for Mac OS X 10.6 and later in C ++. One part of the application is to model mouse movement and mouse clicks. I am doing this currently by placing CGEvent objects using CGEventPost(kCGHIDEventTap, event); .

This works, for the most part - I can simulate mouse movement and click just fine, but it doesn't seem to work in some areas. For example:

  • In Mozilla Firefox and Safari, I can click on all the menus, but I can’t click on the link on the website. When I try, the link is highlighted, but the browser never follows the link. However, I can right-click on the link, select "open the link in a new tab", and everything will work as expected. Solved - creating a mouse event using CGEventCreateMouseEvent(...) makes the event work in a web browser.
  • I can click the "Dashboard" icon to expand the toolbar, but I can not click the "i" button on any widget of the panel widgets. Similarly, none of the search results from the search widget work.

This inconsistency lies along the boundaries of the application. What could be the reason?

+7
c ++ events mouse macos
source share
4 answers

What you need to do to convince these applications that you actually generated the click is to explicitly set the "click state" field in the mouse event to 1 (by default it is 0). The following code will do this:

 CGEventSetIntegerValueField(event, kCGMouseEventClickState, 1); 

It should also be set to 1 for mouse down, but using CGEventCreateMouseEvent (), not CGEventCreate (), which runs for you.

I tested this and it works in the "i" buttons on the toolbar and Spotlight search results.

(Aside, if you simulated a double click, you need to set the click state to 2 for both the mouse and the mouse for the second click event.)

+5
source share

Most menus are activated with the mouseDown event. Hyperlinks follow the mouseUp event. The "i" button only works when you click the mouse, but not for long. All this seems to indicate that you have a problem with synchronization, try a few hours pressed.

+1
source share

Use OSXVnc . I see that they use CGPostMouseEvent() instead of CGPostEvent() .

0
source share
0
source share

All Articles