4

When programmatically moving the mouse cursor, you must set CGSetLocalEventsSuppressionInterval to 0 so the events come in in real-time as opposed to with a 250 millisecond delay.

Unfortunately, CGSetLocalEventsSuppressionInterval is marked as deprecated in Snow Leopard.

The alternative is CGEventSourceSetLocalEventsSuppressionInterval(CGEventSourceRef source, CFTimeInterval seconds); https://developer.apple.com/library/mac/#documentation/Carbon/Reference/QuartzEventServicesRef/Reference/reference.html#//apple_ref/c/func/CGEventSourceSetLocalEventsSuppressionInterval

-(void) mouseMovement:(CGEventRef) newUserMouseMovement
{
    //Move cursor to new position
    CGSetLocalEventsSuppressionInterval(0.0); //Deprecated in OS X 10.6
    CGWarpMouseCursorPosition(NSPointToCGPoint(newMousePosition));
    CGSetLocalEventsSuppressionInterval(0.25); //Deprecated in OS X 10.6

    //--OR--//

    CGEventSourceRef source = ???;
    CGEventSourceSetLocalEventsSuppressionInterval(source, 0.0);
    CGWarpMouseCursorPosition(NSPointToCGPoint(newMousePosition));
    CGEventSourceSetLocalEventsSuppressionInterval(source, 0.25);
}

I can't get the latter method to work.

So I guess my question is how do I get the CGEventSourceRef required for that function?

Is it the event source for the user's normal mouse movement? Or for my manual warping of the cursor?

3 Answers 3

5

Event sources don't seem to be explained anywhere, and no one knows how to use them.

CGPoint warpPoint = CGPointMake(42, 42);
CGWarpMouseCursorPosition(warpPoint);
CGAssociateMouseAndMouseCursorPosition(true);

Call CGAssociateMouseAndMouseCursorPosition( true ) immediately after a warp call to make the Quartz events system drop the delay for this specific warp.

3

Did you ever solve this problem?

Have you tried using CGEventCreateSourceFromEvent(...) to create your CGEventSourceRef from a CGEventRef?

1
  • 1
    I was unable to find the CGEventSourceRef for the warp. The apple docs state the warp works "without generating or posting an event", so I'm not sure if it even has a source. From the answer I just accepted, though, 'CGAssociateMouseAndMouseCursorPosition(true);' works with warp!
    – ck_
    Jul 9, 2013 at 21:51
0

I ran into the same problem of wanting an alternative to the deprecated CGSetLocalEventsSuppressionInterval to use in conjunction with CGWarpMouseCursorPosition and this worked in C++:

CGEventSourceRef source = CGEventSourceCreate(CGEventSourceStateID::kCGEventSourceStateCombinedSessionState);
CGEventSourceSetLocalEventsSuppressionInterval(source, 0);
CGWarpMouseCursorPosition(CGPointMake(x, y));
CFRelease(source);

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.