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

编程入门 行业动态 更新时间:2024-10-12 03:27:19
本文介绍了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:在屏幕上获取当前鼠标位置

本文发布于:2023-10-08 01:10:36,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1471068.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:鼠标   位置   屏幕上   Cocoa

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!