是否需要用户界面才能在Cocoa中进行全局事件监视? [英] Is user interface required in order to do global event monitoring in Cocoa?

查看:912
本文介绍了是否需要用户界面才能在Cocoa中进行全局事件监视?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的代码片段(不是MVC),代码看起来像下面

 #include< Cocoa / cocoa。 h。 

int main(argc,* argv []){
[NSApplication sharedApplication]
[NSEvent addGlobalMonitorForEventsMatchingMask:NSKeyUpMask handler:^(NSEvent * evt){....} ]
[NSApp run]
}

当编译为命令行二进制和运行,全局事件监控工作,允许程序从系统首选项 - >隐私&安全;然后我打包它一个.app,并运行它,全球监视器停止工作,即使允许应用程序从隐私&安全。



我刚刚接触Cocoa,为了为一个打包的应用程序实现一个简单的全局监视器,我需要做什么?

解决方案


blockquote>

您是将CLI程序放入应用程序包还是使用新的图形化Cocoa应用程序?



我建议你使用Xcode的项目窗口创建一个新的GUI应用程序,然后使用以下代码片段作为你的applicationDidFinishLaunching:实现:

  - (void)applicationDidFinishLaunching:(NSNotification *)aNotification 
{
Boolean isTrusted = AXIsProcessTrustedWithOptions(CFDictionaryCreate(NULL,(const void * []){kAXTrustedCheckOptionPrompt},(const void * []){kCFBooleanTrue} ,1,NULL,NULL));
if(isTrusted)
{
[NSEvent addGlobalMonitorForEventsMatchingMask:NSKeyUpMask handler:^(NSEvent * evt){NSLog(@Received NSKeyUp event。 }];
}
}

这将自动打开系统首选项窗格,用户选择您的应用程序应该受信任。
AXIsProcessTrustedWithOptions 需要OS X 10.9(Mavericks)。



更新

有一些陷阱关于在调试全局事件监视器时支持辅助设备:




  • 通过XcodeBuild& Run启动应用程序包附件调试程序,Xcode还需要在隐私预设窗格中授予辅助功能权限

  • 当安装了监视器的Cocoa应用程序的窗口位于前景。


I have a simple snippet(not MVC) of code looking like the following

# include <Cocoa/cocoa.h> 

int main(argc, *argv[]) {
    [NSApplication sharedApplication]
    [NSEvent addGlobalMonitorForEventsMatchingMask:NSKeyUpMask handler:^(NSEvent *evt){ .... }]
    [NSApp run]
}

When compiled as command line binary and run, the global event monitoring works, after allowing the program from system preferences -> privacy & security; then I packed it int an .app, and ran it, the global monitor stopped working even after allowing the app from privacy & security.

I'm new to Cocoa, in order to implement a simple global monitor for a packaged app, what do I need to do else?

解决方案

then I packed it int an .app

Did you put your CLI program in an app bundle or did you start over with a new graphical Cocoa Application?

I'd suggest you create a new GUI app using Xcode's project window and then use the following snippet as your applicationDidFinishLaunching: implementation:

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    Boolean isTrusted = AXIsProcessTrustedWithOptions(CFDictionaryCreate(NULL, (const void*[]){ kAXTrustedCheckOptionPrompt }, (const void*[]){ kCFBooleanTrue }, 1, NULL, NULL));
    if(isTrusted)
    {
        [NSEvent addGlobalMonitorForEventsMatchingMask:NSKeyUpMask handler:^(NSEvent *evt){ NSLog(@"Received NSKeyUp event."); }];
    }
}

This will automatically bring up the System Preference pane that allows the user choose wether your application should be trusted. AXIsProcessTrustedWithOptions requires OS X 10.9 (Mavericks).

Update
There are some pitfalls regarding "Support for assistive devices" when debugging a global event monitor:

  • When launching the app bundle via Xcode "Build & Run" with an attached debugger, Xcode also needs to be granted Accessibility rights in the Privacy pref pane
  • The global event monitor does not receive events when a window from the Cocoa app that installed the monitor is in the foreground.

这篇关于是否需要用户界面才能在Cocoa中进行全局事件监视?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆