Cocoa:在屏幕上获取当前鼠标位置 [英] Cocoa: Getting the current mouse position on the screen

查看:1853
本文介绍了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:在屏幕上获取当前鼠标位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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