I'm writing an application for Mac OS X 10.6 and later in C++. One part of the application needs to simulate mouse movement and mouse clicks. I do this currently by posting CGEvent
objects using CGEventPost(kCGHIDEventTap, event);
.
This works, for the most part - I can simulate mouse movement and clicks just fine, but it seems to fail in some areas. For example:
CGEventCreateMouseEvent(...)
makes the event work within web browser.This inconsistency is along application boundaries. What might be the cause?
Press and hold the power button, wait for it to power off, then wait a few seconds more before releasing the power button. Your Mac should restart normally. Tip: The other reason your Mac freezes and you can't click anything is that an application lacks memory.
Turn Your Mac's Bluetooth Off and On If you use a Magic Mouse, minor glitches with Bluetooth can result in your Apple wireless mouse not working. The same may occur with third-party mice that work over Bluetooth. In that case, disabling and re-enabling Bluetooth on your Mac can help.
In the Applications folder, you will find an icon with the name Auto Mouse Click. Double Click on the Auto Mouse Click icon to launch it. You can optionally right click on the software icon in the dock and select the option to keep the Auto Mouse Click in dock for easy launching of the application.
What you need to do to convince these applications that you have in fact generated a click is to explicitly set the value of the "click state" field on the mouse up event to 1 (it defaults to 0). The following code will do it:
CGEventSetIntegerValueField(event, kCGMouseEventClickState, 1);
It also has to be set to 1 for the mouse down, but by using CGEventCreateMouseEvent() rather than CGEventCreate() that gets done for you.
I have tested this and it works in the 'i' buttons in the dashboard and the Spotlight search results.
(As an aside, if you were simulating a double click you would need to set the click state to 2 for both the mouse down and mouse up events of the second click.)
Most menus are activated with the mouseDown event. Hyperlinks are followed after the mouseUp event. The "i" button only works when the mouse has been clicked but not a long time. All this seem to show that you have a timing problem, try several pressed timing.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With