gpt4 book ai didi

Cocoa:获取鼠标在屏幕上的当前位置

转载 作者:行者123 更新时间:2023-12-03 16:01:03 28 4
gpt4 key购买 nike

我需要使用 Xcode 获取 Mac 屏幕上的鼠标位置。我有一些代码应该可以做到这一点,但我总是将 x 和 y 返回为 0:

void queryPointer()
{

NSPoint mouseLoc;
mouseLoc = [NSEvent mouseLocation]; //get current mouse position

NSLog(@"Mouse location:");
NSLog(@"x = %d", mouseLoc.x);
NSLog(@"y = %d", mouseLoc.y);

}

我做错了什么?如何获取屏幕上的当前位置?另外,最终该位置(保存在 NSPoint 中)需要复制到 CGPoint 中以便与另一个函数一起使用,因此我需要将其获取为 x,y 坐标或将其转换。

最佳答案

作者的原始代码不起作用,因为他/她正在尝试将 float 打印为 %d。正确的代码是:

NSPoint mouseLoc = [NSEvent mouseLocation]; //get current mouse position
NSLog(@"Mouse location: %f %f", mouseLoc.x, mouseLoc.y);

您无需前往 Carbon 即可执行此操作。

关于Cocoa:获取鼠标在屏幕上的当前位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1117065/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com