Bouncing warped cursor

I am trying to move the location of the Mac cursor using Objective-C along a path outside any user interface element (not only in some window, but around the entire screen, not related to the mouse hovering over it). I don’t want to directly deform the mouse into a position, but move it gradually (that is, iterate through the loop and move the cursor 1 pixel to the right in each iteration).

The problem is that the cursor constantly jumps back and forth along the horizontal center line of the screen (if I start the cursor at y = 289, it goes to y = 511 and then goes back to y = 289, and therefore forward, since my screen 800 pixels high), even if I don’t move it at all.

NSPoint mPoint = [NSEvent mouseLocation]; NSPoint mouseWarpLocation = NSMakePoint(mPoint.x, mPoint.y); CGWarpMouseCursorPosition(mouseWarpLocation); 

The code above actually distorts the mouse with its current position, but for some reason the cursor jumps back and forth along the horizontal center line. Any thoughts on why or what I can do to fix this?

0
source share
1 answer

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); 
+2
source

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


All Articles