views:

40

answers:

2

I have a window which happily tabs between its controls using the tab and and shift-tab keys. However, I also want to be able to move between controls with the up and down arrow keys (in this case). How can this be achieved?

+1  A: 

For non-text controls, you should be able to get by with something like this somewhere in the responder chain (e.g., your NSWindowController):

- (void)keyDown:(NSEvent *)event {
    NSWindow *window = [self window];

    switch ([[event characters] objectAtIndex:0]) {
        case NSUpArrowFunctionKey:
            [window makeFirstResponder:[[window firstResponder] previousValidKeyView]];
            break;
        case NSDownArrowFunctionKey:
            [window makeFirstResponder:[[window firstResponder] nextValidKeyView]];
             break;
       default:
            [super keyDown:event];
            break;
    }
}

And in text field delegates:

- (BOOL)control:(NSControl *)control textView:(NSTextView *)textView doCommandBySelector:(SEL)command {
    NSWindow *window = [control window];

    if (command == @selector(moveUp:)) {
        [window makeFirstResponder:[[window firstResponder] previousValidKeyView]];
        return YES;
    } else if (command == @selector(moveDown:)) {
        [window makeFirstResponder:[[window firstResponder] nextValidKeyView]];
        return YES;
    }

    return NO;        
}

(There's a similar method for text view delegates.)

Wevah
Oops! Thanks for the edit…copy/paste fail.
Wevah
+1  A: 

Tell the window to make the current view's next/previous key view the first responder.

Peter Hosey