问题描述
我使用这个代码在我的应用程序中捕获全局键盘快捷键。
I'm using this code to capture Global Keyboard Shortcuts in my app.
这几乎在我运行它的每台计算机上都工作得很好。我试了一个全新的Retina Macbook Pro本周, addGlobalMonitorForEventsMatchingMask
根本不工作。它甚至不记录每个键,像我已经设置为这里做调试。
This works great on almost every computer I run it on. I tried it on a brand new Retina Macbook Pro this week and addGlobalMonitorForEventsMatchingMask
doesn't work at all. It doesn't even log every key like I have it set up to do here for debugging.
有更可靠的方法吗?现在我加载到 applicationDidFinishLaunching
。
Is there a more reliable way to do this? Right now I load this on applicationDidFinishLaunching
.
我认为加载它作为自己的方法更有意义在App Delegate中,但我不知道它的语法将是什么样子。
I think it might make more sense to load it as its own method in the App Delegate but I'm not sure what the syntax of that would look like.
[NSEvent addGlobalMonitorForEventsMatchingMask:NSKeyDownMask handler:^(NSEvent *event){
NSLog(@"sequence = %li", (unsigned long)[event modifierFlags]);
// Activate app when pressing cmd-c
if([event modifierFlags] == 1048840 && [[event charactersIgnoringModifiers] compare:@"c"] == 0) {
推荐答案
在OSX 10.9(Mavericks)到系统首选项>安全&隐私权>隐私权>辅助功能 - 请确保您的应用已被选中。
In OSX 10.9 (Mavericks) the setting has moved to System Preferences > Security & Privacy > Privacy > Accessibility - make sure your app is checked.
这篇关于addGlobalMonitorForEventsMatchingMask在某些计算机上不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!