Detect Enter/Tab/Up/Down keys in NSTextView?
cocoa, macos, nstextview, objective-c
Solution
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;
}
Problem
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!