SOLVED: Using the tab key to cycle through custom NSViews?

  • Sending this for the archives (hope I can save some other poor soul
    some grief).
    ---

    I cannot say that this is the best way to solve the problem of using
    tab to cycle through custom NSViews, but I did finally get it to work.

    The key was this method in NSWindow:

    NSWindow: selectNextKeyView:

    This action method searches for a candidate key view and, if it finds
    one, invokes makeFirstResponder: to establish it as the first
    responder.
    - (void)selectNextKeyView:(id)sender
    Discussion
    The candidate is one of the following (searched for in this order):
      * The current first responder's next valid key view, as returned by
    NSView's nextValidKeyView method
      * The object designated as the receiver's initial first responder
    (using setInitialFirstResponder:) if it returns YES to an
    acceptsFirstResponder message
      * Otherwise, the initial first responder's next valid key view,
    which may end up being nil.

    At first, I tried to implement performKeyEquivalent: in my view,
    looking for the tab key. What I observe is that NSWindow passes the
    tab key down the responder chain starting with the
    initialfirstResponder (not the firstResponder!). When I switched to
    keyDown:, then it went right to the first responder. [Thus, you can
    use either method subject to the differences noted.]

    Thus, the full set of custom NSView methods I used for cycling is below:

    - (BOOL)acceptsFirstResponder
    {
        return YES;
    }

    //- (BOOL)performKeyEquivalent:(NSEvent *)theEvent
    - (BOOL)keyDown:(NSEvent *)theEvent // canBecomeKeyView
    {
        NSString *characters;

        // if using performKeyEquivalent: enable below
        // if([[self window] firstResponder] != self) return NO;

        // get the pressed key
        characters = [theEvent charactersIgnoringModifiers];

        if ([characters isEqual:@"\t"]) {
            [[self window] selectNextKeyView:self];

            return YES;
        } else {
            return NO;
        }
    }
    - (NSView *)nextValidKeyView
    {
        NSView *a;
        return [super [super nextKeyView]];
    }

    Implementing shift-tab is an exercise left for the reader.
previous month september 2007 next month
MTWTFSS
          1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
Go to today