我正在努力学习为Mac编写代码.我曾经是一个Java家伙,所以我希望我遇到的问题是对Cocoa的一个简单的误解.
我有以下代码:
-(IBAction)beginEventMonitor:(id)sender {
_eventMonitor = [NSEvent addGlobalMonitorForEventsMatchingMask:(NSLeftMouseUpMask)
handler:^(NSEvent *incomingEvent) {
//NSWindow *targetWindowForEvent = [incomingEvent window];
NSLog(@"Got a mouse click event at %@", NSStringFromPoint([incomingEvent locationInWindow]));
}];
}
-(IBAction)stopEventMonitor:(id)sender {
if (_eventMonitor) {
[NSEvent removeMonitor:_eventMonitor];
_eventMonitor = nil;
}
}
Run Code Online (Sandbox Code Playgroud)
这是一个简单的钩子,告诉我何时在全局级别发生鼠标点击.处理程序正在运行,但incomingEvent的内容似乎没有设置为任何东西.我能找到的唯一有用的信息是点击时鼠标的位置,以及被点击的窗口的windowId.
我不应该能够获得更多信息吗?我没有正确设置显示器吗?我真的希望能够知道点击了哪个窗口,但我甚至找不到将鼠标位置或windowId转换为有用的方法的方法.