本文介绍了Cocoa:在屏幕上获取当前鼠标位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要在Mac上使用Xcode在屏幕上获取鼠标位置。我有一些代码,据说这样做,但我总是返回x和y为0:
I need to get the mouse position on the screen on a Mac using Xcode. I have some code that supposedly does that but i always returns x and y as 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坐标或翻译这个。
What am I doing wrong? How do you get the current position on the screen? Also, ultimately that position (saved in a NSPoint) needs to be copied into a CGPoint to be used with another function so i need to get this either as x,y coordinates or translate this.
推荐答案CGEventRef ourEvent = CGEventCreate(NULL); point = CGEventGetLocation(ourEvent); CFRelease(ourEvent); NSLog(@"Location? x= %f, y = %f", (float)point.x, (float)point.y);
更多推荐
Cocoa:在屏幕上获取当前鼠标位置
发布评论