Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy is CGWarpMouseCursorPosition causing a delay? If it is not, what is?
    primarykey
    data
    text
    <p>I have code here that restricts the mouse to a region on the screen, it is working relatively well, with only one big problem. The mouse doesn't get moved cleanly/smoothly when running along the edges of the area, it instead jumps in a very choppy manner, I believe this might be due to CGWarpMouseCursorPosition causing a delay upon each "warp".</p> <p><strong>Can anyone tell if it's something in my code that is causing this delay, or if it is in fact the mouse warp function. If it is the mouse warp function, is there any way I can get a smooth relocation of the mouse?</strong> I've done the same thing in flash and it works flawlessly, I know that the loop isn't just taking so much time to execute that it's slowing things down because it only runs maybe 4 or 5 times. </p> <pre><code>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 &lt;= 700 &amp;&amp; tX &gt;= 500 &amp;&amp; tY &lt;= 800 &amp;&amp; tY &gt;= 200){ // target is inside O.K. 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) &lt;= 700 &amp;&amp; (tX-dX) &gt;= 500 &amp;&amp; (tY-dY) &lt;= 800 &amp;&amp; (tY-dY) &gt;= 200){ iX += dX; iY += dY; } else { tX -= dX; tY -= dY; } } while (abs(dX)&gt;accuracy || abs(dY)&gt;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); } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload