跳跃的弯曲光标 [英] Jumping warped cursor

查看:103
本文介绍了跳跃的弯曲光标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Objective-C在任何UI元素之外的路径上移动Mac光标的位置(不仅在某些窗口上,而且在整个屏幕上都与鼠标悬停无关).我不想直接将鼠标翘曲到某个位置,而是逐渐将鼠标移到某个位置(即循环遍历,并在每次迭代中将光标向右移动1个像素).

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 of what the mouse is hovering over). I don't want to directly warp the mouse to a position, but rather progressively move it there (i.e. iterate through a loop, and in each iteration move the cursor 1 pixel to the right).

问题是光标不断在屏幕的水平中心线上来回跳转(如果我将光标从y = 289开始,它会跳转到y = 511,然后又跳回到y = 289,依此类推第四,因为我的屏幕是800像素高),即使我实际上没有将其移动到任何地方.

The problem is that the cursor constantly jumps back and forth over the horizontal center line of the screen (if I start the cursor at y=289, it jumps to y=511, and then back to y=289, and so forth, as my screen is 800 pixels high) even if I don't actually move it anywhere.

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

上面的代码有效地将鼠标扭曲到其当前位置,但是由于某种原因,光标在水平中心线上来回跳转.关于为什么或我可以采取什么措施来解决此问题?

The code above effectively warps the mouse to its current position, but for some reason the cursor jumps back and forth over the horizontal center line. Any thoughts as to why or what I can do to fix it?

推荐答案

问题是AppKit(提供NSEvent类)和Quartz Display Services(提供CGWarpMouseCursorPosition)为屏幕使用了不同的坐标系.

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

在Quartz中,屏幕坐标系的原点位于主"屏幕(包含菜单栏的屏幕)的左上角角,并且Y坐标随着您的增加而增加在屏幕上下移.

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.

在AppKit中,屏幕坐标系的原点位于主屏幕的左下角角,并且当您将屏幕上移时,Y坐标会增加.

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.

因此,如果要求AppKit提供鼠标位置(在屏幕坐标中),则需要先将Y坐标转换为Quartz坐标系,然后再将其传递给Quartz函数.

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.

要变换Y坐标,只需从主屏幕的高度中减去它即可:

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);

这篇关于跳跃的弯曲光标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆