Jumping warped cursor

后端 未结 1 1622
逝去的感伤
逝去的感伤 2021-01-07 11:47

I\'m trying to move the location of a Mac cursor using Objective-C along a path outside of any UI element (not just on some window, but around the entire screen irrelevant o

相关标签:
1条回答
  • 2021-01-07 12:30

    The problem is that AppKit (which provides the NSEvent class) and Quartz Display Services (which provides CGWarpMouseCursorPosition) use different coordinate systems for the screen.

    In Quartz, the origin of the screen coordinate system is at the top-left corner of the “main” screen, which is the screen that contains the menu bar, and Y coordinates increase as you move down the screen.

    In AppKit, the origin of the screen coordinate system is at the bottom-left corner of the main screen, and Y coordinates increase as you move up the screen.

    So if you ask AppKit for the mouse location (in screen coordinates), you need to convert the Y coordinate to the Quartz coordinate system before passing it to a Quartz function.

    To transform 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);
    
    0 讨论(0)
提交回复
热议问题