Constrain mouse movement to region in OS X without jitter
<p>I would like to constrain mouse movement to a specific rectangular region of the screen in OS X 10.11. I modified some code from <a href="https://web.archive.org/web/20201112011027/http://www.hamsoftengineering.com/codeSharing/MouseTools/MouseTools.html" rel="nofollow noreferrer">MouseTools</a> (below) to do this but it's jittery when you hit the edge of the screen. How can I get rid of this jitter?</p>
<pre><code>// gcc -Wall constrain.cpp -framework ApplicationServices -o constrain

#include &lt;ApplicationServices/ApplicationServices.h&gt;

int main (int argc, const char * argv[]) {
    if(argc != 5) {
        printf(&quot;Usage: constrain left top right bottom\n&quot;);
        return 0;
    }
    int leftBound = strtol(argv[1], NULL, 10);
    int topBound = strtol(argv[2], NULL, 10);
    int rightBound = strtol(argv[3], NULL, 10);
    int bottomBound = strtol(argv[4], NULL, 10);
    CGEventTapLocation tapLocation = kCGHIDEventTap;
    CGEventSourceRef sourceRef = CGEventSourceCreate(kCGEventSourceStatePrivate);
    while(true) {
        CGEventRef mouseEvent = CGEventCreate(NULL);
        CGPoint mouseLoc = CGEventGetLocation(mouseEvent);
        int x = mouseLoc.x, y = mouseLoc.y;
        if(x &lt; leftBound || x &gt; rightBound || y &lt; topBound || y &gt; bottomBound) {
            if(x &lt; leftBound) x = leftBound;
            if(x &gt; rightBound) x = rightBound;
            if(y &lt; topBound) y = topBound;
            if(y &gt; bottomBound) y = bottomBound;
            CGEventRef moveMouse = CGEventCreateMouseEvent(sourceRef, kCGEventMouseMoved, CGPointMake(x, y), 0);
            CGEventPost(tapLocation, moveMouse);
            CFRelease(moveMouse);
        }
        CFRelease(mouseEvent);
        usleep(8*1000); // 8ms, ~120fps
    }
    CFRelease(sourceRef);
    return 0;
}
</code></pre>
<p>Here are some other things I tried: <a href="https://web.archive.org/web/20200103161752/http://osxbook.com/book/bonus/chapter2/altermouse/" rel="nofollow noreferrer">Receiving, Filtering, and Modifying Mouse Events</a> shows how to create a callback for mouse movements using <code>CGEventTapCreate</code>. In the code it says &quot;We can change aspects of the mouse event. For example, we can use <code>CGEventSetLocation(event, newLocation)</code>.&quot; Unfortunately this doesn't actually change the mouse location (complete example <a href="https://gist.github.com/kylemcdonald/3f4abf55e718f6e80fd2335c5ac077cc" rel="nofollow noreferrer">here</a>). The only ways I've been able to change the mouse location are by doing a <code>CGEventPost</code> or <code>CGWarpMouseCursorPosition</code>. I also tried using <code> kCGHIDEventTap</code> instead of <code>kCGSessionEventTap</code>, and I checked that I had super user permissions and accessibility enabled for the app. It seems like it should work because there is another example of <a href="https://web.archive.org/web/20200503003001/http://osxbook.com/book/bonus/chapter2/alterkeys/" rel="nofollow noreferrer">modifying key presses</a> which works correctly. Instead of using <code>CGEventSetLocation</code> I also tried using <code>CGEventSetIntegerValueField</code> (like in the keyboard example) to set the x and y deltas to 0, but this didn't change anything.</p>
<p>Finally, I also tried using <code>CGEventPost</code> and <code>CGWarpMouseCursorPosition</code> inside the <code>CGEventCallback</code>, the &quot;invisible border&quot; still it is more jittery than the above code.</p>