(OS X)在程序化鼠标点击期间防止键盘

时间:2014-08-30 13:47:47

标签: macos nsevent

我正在尝试将keynav移植到OS X.它基本上正在工作,但由于无法更改(全局)受监控事件,我遇到了问题。

是否可以在鼠标单击事件期间禁止键盘修改器?也许使用序列,例如用“控制键启动”+鼠标点击创建键盘事件?

这是我的鼠标点击方法:

- (void)clickMouseAtPoint:(CGPoint)point
{
    CGEventRef move = CGEventCreateMouseEvent(
                                               NULL, kCGEventMouseMoved,
                                               point,
                                               kCGMouseButtonLeft // ignored
                                               );

    CGEventRef click1_down = CGEventCreateMouseEvent(
                                                     NULL, kCGEventLeftMouseDown,
                                                     point,
                                                     kCGMouseButtonLeft
                                                     );
    CGEventRef click1_up = CGEventCreateMouseEvent(
                                                   NULL, kCGEventLeftMouseUp,
                                                   point,
                                                   kCGMouseButtonLeft
                                                   );

    CGEventPost(kCGHIDEventTap, move);
    CGEventPost(kCGHIDEventTap, click1_down);
    CGEventPost(kCGHIDEventTap, click1_up);

    CFRelease(click1_up);
    CFRelease(click1_down);
    CFRelease(move1);
}

1 个答案:

答案 0 :(得分:0)

所以这就是我最终的结果。特别是在这里做的背景:当按下ctrl +逗号时,keynav被覆盖到整个桌面,用户可以使用按住ctrl + SomeKeys来导航十字准线,并通过a模拟鼠标点击交叉的位置。第二个ctrl +逗号(在下面的代码片段中等待第二次单击时设置awaitClick)。

现在,在单击完成之前,控制键修饰符将从键盘事件中删除。此外,当显示keynav时,所有键盘输入都与键码-1交换。我通过试验证明了这一点,这抑制了按键。

如果这是一个好方法,我不能说。但到目前为止它可靠地运作。

CGEventRef
myCGEventCallback(CGEventTapProxy proxy, CGEventType type,
                  CGEventRef event, void *refcon)
{
    if ((type != kCGEventKeyDown) && (type != kCGEventKeyUp))
        return event;

    CGKeyCode keycode = (CGKeyCode)CGEventGetIntegerValueField(event, kCGKeyboardEventKeycode);

    EWAppDelegate *appInstance = [NSApplication sharedApplication].delegate;
    CGEventFlags flags = CGEventGetFlags(event);

    if (_awaitClick && keycode == (CGKeyCode)43 && type == kCGEventKeyDown)
    {
        flags = flags & ~kCGEventFlagMaskControl;
        CGEventSetFlags(event, flags);
        NSLog(@"suppressed control key");
    }

    BOOL didHandle = NO;

    if (type == kCGEventKeyDown)
    {
        didHandle = [appInstance handleNavKey:keycode flags:flags];
    }

    NSLog(@"pressed keycode %d", keycode);

    if (didHandle || _navIsSHowing) {
        CGEventSetIntegerValueField(event, kCGKeyboardEventKeycode, (int64_t)-1);
        NSLog(@"nav");
    }

    return event;
}

- (BOOL)registerGlobalKeyBoardModifier
{
    CFMachPortRef      eventTap;
    CGEventMask        eventMask;
    CFRunLoopSourceRef runLoopSource;

    eventMask = ((1 << kCGEventKeyDown) | (1 << kCGEventKeyUp));
    eventTap = CGEventTapCreate(kCGSessionEventTap, kCGHeadInsertEventTap, 0,
                                eventMask, myCGEventCallback, NULL);
    if (!eventTap) {
        fprintf(stderr, "failed to create event tap\n");
        return NO;
    }

    runLoopSource = CFMachPortCreateRunLoopSource(
                                                  kCFAllocatorDefault, eventTap, 0);

    CFRunLoopAddSource(CFRunLoopGetCurrent(), runLoopSource,
                       kCFRunLoopCommonModes);

    CGEventTapEnable(eventTap, true);

    return YES;
}