Search code examples
objective-cnsviewnswindownsapplication

NSView cannot grab Ctrl+Tab Keydown event


In my Mac App, I listen to key press events and pass them on to the internal client, depending on modifiers and key code.

Currently, I'm facing the problem, that I can't get a hold of the "Ctrl+Tab" event. It seems that the "App" itself tries to handle this, which makes sense for tab based applications. So I disabled the Tabbingmode, but still, the Ctrl+Tab never fires the KeyDown event. Any other combination of key code and modifier seems to pass just fine.

Any suggestions on how to get the key down event fired for Ctrl+Tab?


Solution

  • In my testing, NSView's -keyDown: method does not seem to get called on NSView subclasses for control-tab key events. However, you can intercept them at the application level with an NSApplication subclass:

    @interface MyApplication: NSApplication
    @end
    
    @implementation MyApplication
    
    - (void)sendEvent:(NSEvent *)event {
        if (event.type == NSEventTypeKeyDown &&
            [event.charactersIgnoringModifiers isEqualToString:@"\t"] &&
            (event.modifierFlags & NSEventModifierFlagDeviceIndependentFlagsMask) == NSEventModifierFlagControl) {
            NSLog(@"ctrl-tab");
        }
    
        [super sendEvent:event];
    }
    
    @end