The problem is that AppKit (which provides the NSEvent class) and Quartz Display Services (which provides the CGWarpMouseCursorPosition ) use different coordinate systems for the screen.
In quartz, the origin of the coordinate system is in the upper left corner of the main screen, which is the screen that contains the menu bar, and the Y coordinates increase as you move the screen down .
In AppKit, the origin of the coordinate system is located in the lower left corner of the main screen, and the Y coordinates increase as the screen moves up .
So, if you ask AppKit for the location of the mouse (in screen coordinates), you need to convert the Y coordinate to a quartz coordinate system before passing it to the quartz function.
To convert the Y coordinate, you simply subtract it from the height of the main screen:
NSPoint point = [NSEvent mouseLocation]; point.y = [NSScreen mainScreen].frame.size.height - point.y; CGWarpMouseCursorPosition(point);
source share