简体   繁体   中英

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!

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.

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM