注册全局热键,不支持启用的辅助设备 [英] Register a global hotkey without support for assistive devices enabled

查看:120
本文介绍了注册全局热键,不支持启用的辅助设备的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用此代码,我可以注册一个全局事件处理程序:

Using this code, I may register a global event handler:

[NSEvent addGlobalMonitorForEventsMatchingMask: NSKeyDownMask 
                                       handler: ^(NSEvent *incomingEvent) {

           NSString *chars = [[incomingEvent characters] lowercaseString];
           unichar character = [chars characterAtIndex:0];    

           // do something useful
           NSLog(@"keydown globally! Which key? This key: %c", character);

        }];

不幸的是,如果支持辅助设备已启用。如果没有启用辅助设备,则不会传递任何事件。

Unfortunately, events get passed along to this monitor, if support for assistive devices is enabled. Without assistive devices being enabled, no events get passed along.

形成文档:

Key-related events may only be monitored if accessibility is enabled or if your 
application is trusted for accessibility access (see AXIsProcessTrusted).

我想知道,如果存在另一种方法,它会传递事件而不强制用户启用

虽然我在Apple的文档中没有找到解决方案,但是解决方案必须存在。例如。 MAS下载的Alfred版本允许定义热键。

While I didn't find a solution in Apple's docs, a solution must exist. E.g. the MAS-downloaded version of Alfred allows to define a hotkey.

有趣的是,Alfred的偏好设置仅显示特殊键,并指出某些特殊键组合可能无法正常工作

Interestingly, Alfred's preferences only shows special keys and points out, that certain special key combinations may not work.

由于我基本上想显示/隐藏一个全局非激活面板,我可能应该只准备一个系统服务。是否应该?

Since I basically want to show / hide a 'global' non-activating panel, I probably should simply prepare a system service. Should I?

推荐答案

您可以尝试创建一个Quartz事件敲击器, kCGSessionEventTap 作为位置。

You could try creating a Quartz Event Tap, with kCGSessionEventTap as the location.

Quartz Event Services参考

来自 Mac OS X Internals 的示例代码

Quartz Event Services Reference
Sample code from Mac OS X Internals

这篇关于注册全局热键,不支持启用的辅助设备的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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