【问题标题】:DDHotKey - Tracking without canceling eventDDHotKey - 跟踪而不取消事件
【发布时间】:2013-02-15 02:50:20
【问题描述】:

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


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

InstallApplicationEventHandler(&dd_hotKeyHandler, 1, &eventSpec, NULL, NULL);

以及事件处理程序本身:

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;
}

【问题讨论】:

    标签: objective-c macos cocoa nsevent ddhotkey


    【解决方案1】:

    不,热键功能的全部意义在于事件被注册键的应用程序吞没。

    您想要一个global event monitor,它允许您观察系统中其他任何地方发生的关键事件,但不会影响它们。

    [NSEvent addGlobalMonitorForEventsMatchingMask:NSKeyUpMask
                                           handler:^(NSEvent * event){
        // See if the key is the one you want and act on it.
    }];
    

    【讨论】:

    • 全局事件监控仅在启用可访问性时才有效,这是我不希望我的用户打扰的事情。
    • 没有办法解决这个问题(除了以 root 身份运行)。在 OS X 上可以通过三种方式全局查看关键事件:Carbon Hot Keys、CGEventTaps 和 NSEvent 监视器(我怀疑它们是前者的包装器。后者都需要打开辅助功能。
    • 啊,我明白了。我认为有一些解决方法。无论如何,谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-17
    • 2012-10-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多