代码之家  ›  专栏  ›  技术社区  ›  Nick Moore Wain

不带tab键的控件之间的“tab”

  •  0
  • Nick Moore Wain  · 技术社区  · 14 年前

    我有一个窗口,它可以使用tab和shift tab键在控件之间切换。但是,我也希望能够使用上下箭头键在控件之间移动(在本例中)。如何实现这一目标?

    2 回复  |  直到 14 年前
        1
  •  1
  •   Peter Hosey    14 年前

    对于非文本控件,您应该能够在响应器链的某个位置(例如,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;
        }
    }
    

    在文本字段中,代表:

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

    (文本视图委托也有类似的方法。)

        2
  •  2
  •   Peter Hosey    14 年前

    告诉窗口使当前视图的下一个/上一个键视图成为第一个响应者。