Why does CGWarpMouseCursorPosition cause a delay? If this is not so, then what?

I have a code here that limits the mouse to an area on the screen, it works relatively well, with one big problem. The mouse does not move cleanly / smoothly when moving along the edges of the area, but instead jumps very intermittently, I believe that this may be due to the fact that CGWarpMouseCursorPosition causes a delay on each "deformation".

Can anyone say that this is something in my code causing this delay, or if it is actually a mouse warp function. If this is a mouse warp function, is there a way to get a smooth mouse movement? I did the same thing in a flash and it works flawlessly, I know that the cycle does not just take so much time to execute that it slows down because it only works 4 or 5 times.

CGEventRef mouse_filter(CGEventTapProxy proxy, CGEventType type, CGEventRef event, void *refcon) { CGPoint point = CGEventGetLocation(event); float tX = point.x; float tY = point.y; if( tX <= 700 && tX >= 500 && tY <= 800 && tY >= 200){ // target is inside OK area, do nothing }else{ CGPoint target; //point inside restricted region: float iX = 600; // inside x float iY = 500; // inside y // delta to midpoint between iX,iY and tX,tY float dX; float dY; float accuracy = .5; //accuracy to loop until reached do { dX = (tX-iX)/2; dY = (tY-iY)/2; if((tX-dX) <= 700 && (tX-dX) >= 500 && (tY-dY) <= 800 && (tY-dY) >= 200){ iX += dX; iY += dY; } else { tX -= dX; tY -= dY; } } while (abs(dX)>accuracy || abs(dY)>accuracy); target = CGPointMake(roundf(tX), roundf(tY)); CGWarpMouseCursorPosition(target); } return event; } int main(int argc, char *argv[]) { NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; CFRunLoopSourceRef runLoopSource; CGEventMask event_mask; event_mask = CGEventMaskBit(kCGEventMouseMoved) | CGEventMaskBit(kCGEventLeftMouseDragged) | CGEventMaskBit(kCGEventRightMouseDragged) | CGEventMaskBit(kCGEventOtherMouseDragged); CFMachPortRef eventTap = CGEventTapCreate(kCGHIDEventTap, kCGHeadInsertEventTap, 0, event_mask, mouse_filter, NULL); if (!eventTap) { NSLog(@"Couldn't create event tap!"); exit(1); } runLoopSource = CFMachPortCreateRunLoopSource(kCFAllocatorDefault, eventTap, 0); CFRunLoopAddSource(CFRunLoopGetCurrent(), runLoopSource, kCFRunLoopCommonModes); CGEventTapEnable(eventTap, true); CFRunLoopRun(); CFRelease(eventTap); CFRelease(runLoopSource); [pool release]; exit(0); } 
+6
source share
2 answers

As you have discovered, CGSetLocalEventsSuppressionInterval fixes your problem.

However, it is deprecated since 10.6. Apple docs say:

This feature is not recommended for general use due to undocumented special cases and unwanted side effects. The recommended replacement for this function is CGEventSourceSetLocalEventsSuppressionInterval , which allows you to adjust the suppression interval for a specific event source, affecting only events sent using this event source.

Unfortunately, replacing CGEventSourceSetLocalEventsSuppressionInterval does not work with CGWarpMouseCursorPosition movements.

Instead, use CGAssociateMouseAndMouseCursorPosition(true) immediately after warp:

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

The documentation does not mention this behavior, but seems to cancel the suppression interval after the warp.

+4
source

You are probably looking for CGSetLocalEventsSuppressionInterval (), a method deprecated since 10.6 ... it still works, albeit in 10.7.

http://developer.apple.com/library/mac/#documentation/Carbon/Reference/QuartzEventServicesRef/DeprecationAppendix/AppendixADeprecatedAPI.html#//apple_ref/c/func/CGSetLocalEventsSuppressionInterval

+1
source

Source: https://habr.com/ru/post/902024/


All Articles