Your best bet is to subclass NSTextView
to get the functionality that you want. Here is a quick example:
MyTextView.h
@interface MyTextView : NSTextView
{
id target;
SEL action;
}
@property (nonatomic, assign) id target;
@property (nonatomic, assign) SEL action;
@end
MyTextView.m
@implementation MyTextView
@synthesize target;
@synthesize action;
- (void)keyDown:(NSEvent *)theEvent
{
if ([theEvent keyCode] == 36) // enter key
{
NSUInteger modifiers = [theEvent modifierFlags];
if ((modifiers & NSShiftKeyMask) || (modifiers & NSAlternateKeyMask))
{
// shift or option/alt held: new line
[super insertNewline:self];
}
else
{
// straight enter key: perform action
[target performSelector:action withObject:self];
}
}
else
{
// allow NSTextView to handle everything else
[super keyDown:theEvent];
}
}
@end
Setting the target and action would be done as follows:
[myTextView setTarget:someController];
[mytextView setAction:@selector(omgTheUserPressedEnter:)];
For more details on key codes and the full suite of NSResponder
messages like insertNewline:
, see the excellent answers to my question about NSEvent
keycodes: Where can I find a list of key codes for use with Cocoa's NSEvent class?