6

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!

Community
  • 1
  • 1
Max Chuquimia
  • 7,494
  • 2
  • 40
  • 59

2 Answers2

3

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.

Ken Thomases
  • 88,520
  • 7
  • 116
  • 154
  • 1
    Adding `CGWarpMouseCursorPosition(point);` at the bottom of the PostMouseEvent method did not help - is this what you meant? – Max Chuquimia Mar 16 '13 at 10:27
  • 1
    Adding it teleports the mouse to (0,0) if it touches the edge of the screen. – Max Chuquimia Mar 16 '13 at 10:34
  • 1
    Well, I'd warp before posting the event since warping adds delta to the next event. Couldn't tell you why it would go to (0, 0) if that's not the point you specified. – Ken Thomases Mar 16 '13 at 10:50
  • Putting the warp directly before `CGEventPost(kCGSessionEventTap, theEvent);` stops the mouse from teleporting to (0,0), however the dock still does not show and now the mouse sticks to the edge of the screen if it touches it... Adding the warp to the top of the method stops the mouse sticking, but still doesn't show the dock... – Max Chuquimia Mar 16 '13 at 21:52
  • Ah, the sticking is/was due to the mouse actually moving off the screen, but the warp makes it look like it's still on the screen... – Max Chuquimia Mar 16 '13 at 22:04
  • OK, fixed it! Thank you very much for your help! I'll create a correct answer, but you can enjoy +50 reputation! – Max Chuquimia Mar 16 '13 at 22:08
  • You're welcome. Glad you got it working. Thanks for the bounty. – Ken Thomases Mar 16 '13 at 22:22
0

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)

Max Chuquimia
  • 7,494
  • 2
  • 40
  • 59
  • You should take into account other edges of the screen. As well, with multiple monitors, it is possible for the origin point of the screen to have negative components. – Jonathan Grynspan Mar 16 '13 at 22:27
  • Yes, will do. OK, I didn't know that about extra monitors... Thanks! (my project will be using extra monitors... Lucky you told me!) – Max Chuquimia Mar 17 '13 at 06:25