首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >DDHotKey -跟踪而不取消事件

DDHotKey -跟踪而不取消事件
EN

Stack Overflow用户
提问于 2013-02-28 19:03:19
回答 1查看 216关注 0票数 2

我正在使用DDHotKey跟踪一些全系统的键盘快捷键。当事件被触发时,只有我的应用程序才能得到它。是否可以在不阻止事件被传递到其原始目标应用程序的情况下观察它?

下面是这个模块注册事件处理程序的方式:

代码语言:javascript
复制
InstallApplicationEventHandler(&dd_hotKeyHandler, 1, &eventSpec, NULL, NULL);

事件处理程序本身:

代码语言:javascript
复制
OSStatus dd_hotKeyHandler(EventHandlerCallRef nextHandler, EventRef theEvent, void *userData) {
    @autoreleasepool {
        EventHotKeyID hotKeyID;
        GetEventParameter(theEvent, kEventParamDirectObject, typeEventHotKeyID, NULL, sizeof(hotKeyID),NULL,&hotKeyID);

        UInt32 keyID = hotKeyID.id;

        NSPredicate *predicate = [NSPredicate predicateWithFormat:@"hotKeyID = %u", keyID];
        NSSet *matchingHotKeys = [[DDHotKeyCenter sharedHotKeyCenter] hotKeysMatchingPredicate:predicate];
        if ([matchingHotKeys count] > 1) { NSLog(@"ERROR!"); }
        DDHotKey *matchingHotKey = [matchingHotKeys anyObject];

        NSEvent *event = [NSEvent eventWithEventRef:theEvent];
        NSEvent *keyEvent = [NSEvent keyEventWithType:NSKeyUp
                                             location:[event locationInWindow]
                                        modifierFlags:[event modifierFlags]
                                            timestamp:[event timestamp]
                                         windowNumber:-1
                                              context:nil
                                           characters:@""
                          charactersIgnoringModifiers:@""
                                            isARepeat:NO
                                              keyCode:[matchingHotKey keyCode]];

        [matchingHotKey invokeWithEvent:keyEvent];
    }

    return noErr;
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-02-28 20:31:09

不,热键功能的全部要点是事件被注册键的应用程序所吞没。

您需要一个全局事件监视器,它允许您观察系统中其他地方发生的关键事件,但不影响它们。

代码语言:javascript
复制
[NSEvent addGlobalMonitorForEventsMatchingMask:NSKeyUpMask
                                       handler:^(NSEvent * event){
    // See if the key is the one you want and act on it.
}];
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15143571

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档