你好。我正在尝试监视用户在可可应用程序中按下的键。
我使用了这段代码:
// this code works!
CGEventMask keyboardMaskKeyDown = CGEventMaskBit(kCGEventKeyDown);
keyboardEventresult = [NSEvent addGlobalMonitorForEventsMatchingMask:keyboardMaskKeyDown handler:^(NSEvent *keyboardEvent)
{
keyboardEventresult = keyboardEvent;
_currentKeystr = [NSString stringWithFormat:@"%c",[[keyboardEvent characters]characterAtIndex:0]];
NSLog(@"Pressed key: %@",_currentKeystr);
[hiddentextfield setStringValue:[NSString stringWithFormat:@"%@",_currentKeystr]];
}];
但问题是当我改变时:
addGlobalMonitorForEventsMatchingMask
到
addLocalMonitorForEventsMatchingMask
我收到一条错误消息Cannot initialize a parameter of type NSEvent *(^)(NSEvent *_strong) with an rvalue of type void(^)(NSEvent *_strong)
在这里你可以看到苹果正在做类似的事情
_eventMonitor = [NSEvent addLocalMonitorForEventsMatchingMask:
(NSLeftMouseDownMask | NSRightMouseDownMask | NSOtherMouseDownMask | NSKeyDownMask)
handler:^(NSEvent *incomingEvent)
有想法该怎么解决这个吗?