Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simulated MouseEvent not working properly OSX

Back in 2010, Pierre asked this question (his accepted answer doesn't work for me).

I'm having the same problem: I am able to successfully move the mouse around (and off!?!) the screen programmatically from my Cocoa Application, however bringing the mouse to the location of my dock doesn't show it (and some other applications aren't registering the mouse moved event, eg. games that remove the mouse)

The method I am using is thus:

void PostMouseEvent(CGMouseButton button, CGEventType type, const CGPoint point)
{
  CGEventRef theEvent = CGEventCreateMouseEvent(NULL, type, point, button);
  CGEventSetType(theEvent, type);
  CGEventPost(kCGSessionEventTap, theEvent);
  CFRelease(theEvent);
}

And then when I want to move the mouse I run:

PostMouseEvent(0, kCGEventMouseMoved, mouseLocation);

Note that this code DOES generate mouseover events for things such as links.

Now that's it's 2013, is it possible to fix this issue?

Thanks for your time!

like image 685
Max Chuquimia Avatar asked Mar 04 '13 04:03

Max Chuquimia


1 Answers

I would both warp the cursor and generate the mouse-move event. I know from experience, for example, that warping the cursor, while it doesn't generate an event itself, modifies the subsequent mouse move event to include the moved distance in its mouse delta. I don't know if your synthesized move event will include the proper delta values on its own.

like image 82
Ken Thomases Avatar answered Oct 18 '22 02:10

Ken Thomases