使用 [NSEvent mouseLocation] 的位置错误

发布于 2024-12-17 03:11:30 字数 641 浏览 4 评论 0原文

我为 Mac 制作了一个 iPhone 远程鼠标控制器应用程序:iPhone 应用程序将坐标值发送到 Mac,然后 Mac 处理鼠标位置值。

为了获取 Mac 上的当前鼠标位置,接收者调用 [NSEvent mouseLocation]。

x 的值始终正确,但 y 的值错误。

我使用了“while”循环来处理这个事件。

while (1) {
    mouseLoc = [NSEvent mouseLocation];

    while ((msgLength = recv(clientSocket, buffer, sizeof(buffer), 0)) != 0) {
          CGPoint temp;
          temp.x = mouseLoc.x;
          temp.y = mouseLoc.y; // wrong value
          ........

每个循环周期的y值不同。例如,第一次循环时y值为400,下一次循环时y值为500;然后 y 在下一个循环中再次变为 400。

鼠标指针不断上下移动,两个不同的y值之和始终为900。(我认为是因为屏幕分辨率是1440 * 900。)

我不知道为什么会发生这种情况,该怎么办,以及如何解决调试它。

I make a iphone remote mouse controller application for Mac: the iPhone application sends the coordinate values to the Mac, which then processes mouse location value.

To get the current mouse location on the Mac, the receiver calls [NSEvent mouseLocation].

The value for x is always correct, but the value for y is wrong.

I used a "while" loop to process this event.

while (1) {
    mouseLoc = [NSEvent mouseLocation];

    while ((msgLength = recv(clientSocket, buffer, sizeof(buffer), 0)) != 0) {
          CGPoint temp;
          temp.x = mouseLoc.x;
          temp.y = mouseLoc.y; // wrong value
          ........

A y value is different at each loop period. For example, y value is 400 at first loop, y is 500 at next loop; then y is 400 again at next loop.

The mouse pointer is coming up and down continuously, and sum of two different y values is always 900. (I think because the screen resolution is 1440 * 900.)

I don't know why it happens, what to do, and how to debug it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

风苍溪 2024-12-24 03:11:30

这是一种获得正确 Y 值的方法:

while (1) {
mouseLoc = [NSEvent mouseLocation];
NSRect screenRect = [[NSScreen mainScreen] frame];
NSInteger height = screenRect.size.height;

while ((msgLength = recv(clientSocket, buffer, sizeof(buffer), 0)) != 0) {
      CGPoint temp;
      temp.x = mouseLoc.x;
      temp.y = height - mouseLoc.y; // wrong value
      ........

基本上,我已经获取了屏幕高度:

NSRect screenRect = [[NSScreen mainScreen] frame];
NSInteger height = screenRect.size.height;

然后我获取屏幕高度并从中减去 mouseLocation 的 Y 坐标,因为 mouseLocation 从底部/左侧返回坐标,这将为您提供从顶部开始的 Y 坐标。

temp.y = height - mouseLoc.y; // right value

这在我的控制鼠标位置的应用程序中起作用。

Here is a way you can get the proper Y value:

while (1) {
mouseLoc = [NSEvent mouseLocation];
NSRect screenRect = [[NSScreen mainScreen] frame];
NSInteger height = screenRect.size.height;

while ((msgLength = recv(clientSocket, buffer, sizeof(buffer), 0)) != 0) {
      CGPoint temp;
      temp.x = mouseLoc.x;
      temp.y = height - mouseLoc.y; // wrong value
      ........

Basically, I've grabbed the screen height:

NSRect screenRect = [[NSScreen mainScreen] frame];
NSInteger height = screenRect.size.height;

Then I take the screen height and subtract the Y coordinate of mouseLocation from it because mouseLocation returns coordinates from the bottom/left this will give you the Y coordinate from the top.

temp.y = height - mouseLoc.y; // right value

This is working in my app that controlling the mouse position.

分开我的手 2024-12-24 03:11:30

我不知道为什么它会在没有看到更多代码的情况下发生变化,但很可能它与 mouseLoc = [NSEvent mouseLocation]; 返回一个点有关,该点的原点位于屏幕的左下角,而不是通常的左上角。

I don't know why it would be changing without seeing more of your code, but there is a good possibility it has something to do with the fact that mouseLoc = [NSEvent mouseLocation]; returns a point whose origin is at the bottom left of the screen, instead of the top left where it would normally be.

过去的过去 2024-12-24 03:11:30

获取正确的位置代码:

CGPoint mousePoint = CGPointMake([NSEvent mouseLocation].x, [NSScreen mainScreen].frame.size.height - [NSEvent mouseLocation].y);

Get right position code:

CGPoint mousePoint = CGPointMake([NSEvent mouseLocation].x, [NSScreen mainScreen].frame.size.height - [NSEvent mouseLocation].y);
我为君王 2024-12-24 03:11:30

斯威夫特5:

let mousePosition = CGPoint(x: NSEvent.mouseLocation.x, y: (NSScreen.main?.frame.size.height)! - NSEvent.mouseLocation.y)

Swift 5:

let mousePosition = CGPoint(x: NSEvent.mouseLocation.x, y: (NSScreen.main?.frame.size.height)! - NSEvent.mouseLocation.y)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文