Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect Enter/Tab/Up/Down keys in NSTextView?

Cocoa noob here. I'm wondering how I can capture the Enter and tab keys onKeyDown whilst the user is typing in an NSTextView?

Thanks!

like image 586
Connor Avatar asked Dec 08 '12 09:12

Connor


1 Answers

The easiest way is to implement the - (BOOL)textView:(NSTextView *)aTextView doCommandBySelector:(SEL)aSelector delegate method and look for the insertNewline: and insertTab: selectors.

- (BOOL)textView:(NSTextView *)aTextView doCommandBySelector:(SEL)aSelector
{
    if (aSelector == @selector(insertNewline:)) {
        // Handle the Enter key
        return YES;
    } else if (aSelector == @selector(insertTab:)) {
        // Handle the Tab key
        return YES;
    }

    return NO;

}
like image 113
Steve Shepard Avatar answered Oct 14 '22 21:10

Steve Shepard