我需要使用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坐标获取或转换。