Simulating mouse clicks on Mac OS X does not work for some applications
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.)
Simulated Mouseevents ignored on macOS in OpenGL
I found the solution myself and it is really simple:
First of all I forgot that usleep is in microseconds and not miliseconds.
Furthermore apparently macOS buffers the clicks or keyboard inputs in the native environment but OpenGL doesn't. So of course it works if you pause for 1 second between clicks/keyboard presses but in OpenGL it just discards them if they are too fast. The solution is to wait the a small amount between clicks or keypresses. For me worked 100ms, that means usleep(100000), before and after the press down.
Simulated MouseEvent not working properly OSX
OK, so evidently MacOSX needs the mouse to be at exactly the edge of the screen for the dock to show!
Because I keep my dock on the left-side of the screen (due to many programs keeping vital buttons at the bottom of their windows), all I had to do was say
if (mouseLocation.x < 0)
{
mouseLocation.x = 0;
}
And it worked!
I am also using KenThomases' idea to warp the cursor as well.
(this answer is marked correct as it allows me to show the dock - however there are still some applications that are not responding to mouse input)
CGEventPost does not always move the mouse macOS
I found the answer.
I changed this line:
event = CGEventCreateMouseEvent(None, type, (x, y), kCGMouseButtonLeft)
CGEventPost(kCGHIDEventTap, event)
To this:
CGPostMouseEvent((x, y), True, 0, False)
And of course imported: CGPostMouseEvent
As a result, moving the mouse works everywhere and in all applications. The reason why it works is because it turns out CGPostMouseEvent
occurs on a deeper level of the local machine than CGEventPost
.
Simulate mouse on Mac
CGDisplayMoveCursorToPoint()
only moves the image of the cursor, it does not generate any events. You should create and post mouse events of type kCGEventMouseMoved
to simulate moving the mouse. Your own method would do it:
[self postMouseEventWithButton:0 withType:kCGEventMouseMoved andPoint:point];
For clicks, you are already doing it the right way, I think. One thing you should also do is set the click count properly on both the mouse down and mouse up events, like so:
CGEventSetIntegerValueField(event, kCGMouseEventClickState, 1);
... because some applications need it.
(See also Simulating mouse clicks on Mac OS X does not work for some applications)
If your code doesn't work, I'm not sure why; it looks OK to me. Try posting to kCGSessionEventTap
instead of kCGHIDEventTap
and see if it helps. Also, you don't need the CGEventSetType()
call since the type is already set in the creation call.
Related Topics
C++ Difference Between 0 and 0.0
Partial Specialization of Function Templates
Why Is the New Operator Allowed to Return *Void to Every Pointer-Type
Understanding Lapack Calls in C++ with a Simple Example
How to Read from Memory Just Like from a File Using iOStream
C++11 Thread Class How to Use a Class Member Function
Class' Keyword in Variable Definition in C++
Reading Directly from an Std::Istream into an Std::String
Disable Sleep Mode in Windows Mobile 6
How to Specify Vc11 Lambda Calling Convention
Eclipse Mingw C++ Cannot Find -Lpthread
What Is the Meaning of Double Curly Braces Initializing a C-Struct
Cmake Link Library Target Link Error
Why Class Size Depend Only on Data Members and Not on Member Functions
How to Overload Array Index Operator for Wrapper Class of 2D Array