Using `CGEventSourceSetLocalEventsSuppressionInterval` instead of the deprecated `CGSetLocalEventsSuppressionInterval`

筅森魡賤 提交于 2019-12-07 12:12:32

问题


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?


回答1:


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.




回答2:


Did you ever solve this problem?

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



来源:https://stackoverflow.com/questions/10196603/using-cgeventsourcesetlocaleventssuppressioninterval-instead-of-the-deprecated

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!