How can I make the Tab key move focus out of a NSTextView?

此生再无相见时 提交于 2019-12-18 11:48:37

问题


I'm using an NSTextView to allow multi-line input. However, due to the nature of my app, users will be more comfortable moving to the next input element when they press TAB.

How can I make TAB exit the NSTextView, while keeping the newline behaviour of the Enter key?


回答1:


You could implement -textView:doCommandBySelector: in your text view's delegate:

- (BOOL)textView:(NSTextView *)aTextView doCommandBySelector:(SEL)aSelector {
    if (aSelector == @selector(insertTab:)) {
        [[aTextView window] selectNextKeyView:nil];
        return YES;
    }

    return NO;
}

See http://developer.apple.com/documentation/Cocoa/Reference/NSTextViewDelegate_Protocol




回答2:


You'll need to implement this in a subclass.

I wrote such a subclass for Translate Text. You're welcome to use it under its BSD license. Here's the header and the implementation file.

… while keeping the newline behaviour of the Enter key?

My main purpose was to send an action to a target when the user presses Enter, and I also have it drop focus from the view. However, both are explicit statements in the code; you can simply comment that code out or delete it.



来源:https://stackoverflow.com/questions/2484072/how-can-i-make-the-tab-key-move-focus-out-of-a-nstextview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!