gpt4 book ai didi

cocoa - 跟踪光标位置 cocoa 并返回相对于窗口的鼠标位置

转载 作者:行者123 更新时间:2023-12-03 17:56:55 24 4
gpt4 key购买 nike

我想我会发布一个关于在 cocoa 应用程序中连续跟踪 cocoa 中鼠标位置的问题和答案(在 OSX 10.5.8 中使用 xcode 3.1.4)。

这是一个 self 回答问题,因为我终于弄清楚了,但在这里找不到我认为描述了我如何解决它的答案

可能有些人可能认为它比使用事件点击更容易,而且在 10.6 中没有屏蔽功能,这非常简单:)

最佳答案

这就是我的做法。

首先这是我用来设置跟踪区域的代码:

NSTrackingArea* trackingArea = [ [ NSTrackingArea alloc] initWithRect:[ mainWindow frame]       options:(NSTrackingMouseMoved | NSTrackingActiveAlways ) owner:self userInfo:nil);

[container addTrackingArea:trackingArea];

当我创建主窗口时,我在设置所有内容时添加此消息:

 [window setAcceptsMouseMovedEvents:YES];

在我想要捕获事件的类的头文件中(对我来说,它与我创建跟踪区域在同一个类中),我对 NSResponder 进行子类化,然后实现我想要捕获的事件,这对我来说是鼠标已移动。

-(void) mouseMoved: (NSEvent *) thisEvent
{
NSPoint cursorPoint = [ thisEvent locationInWindow ];
NSLog(@"X coordinate is %f and Y coordinate is %f",cursorPoint.x,cursorPoint.y);
}

希望这对您有所帮助:)

谢谢!

关于cocoa - 跟踪光标位置 cocoa 并返回相对于窗口的鼠标位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11845767/

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