Re: Return and Enter keys in NSTextView
Re: Return and Enter keys in NSTextView
- Subject: Re: Return and Enter keys in NSTextView
- From: Richard Schreyer <email@hidden>
- Date: Thu, 30 Aug 2001 11:48:16 -0700
On Thursday, August 30, 2001, at 08:33 AM, Douglas Davidson wrote:
Don't subclass just for this. What you want is for your textview to
act like the field editor for a textfield. You get this behavior by
calling setFieldEditor:YES. Then you just look for the
NSTextDidBeginEditingNotification (if you are the delegate, you will
get this as textDidEndEditing:).
This is what I initially thought about doing, but then I realized that
it would send the message every time the text ended editing, even if the
user clicked somewhere else in the window, instead of hitting return. I
suppose I could use [NSApp currentEvent] to see how the editing was
ended. That's still an advantage over what I have now, since I didn't
subclass.
- (BOOL)textView:(NSTextView *)aTextView
doCommandBySelector:(SEL)aSelector;
{
if (aSelector == @selector(insertNewline:) && ([NSApp
currentEvent] ... is an unmodified return ...)) {
[aTextView insertNewlineIgnoringFieldEditor:self];
return YES;
}
return NO;
}
This looks like it should work. It at least ends the editing at the
right time, leaving me the same challenge as above, deciding if I
actually want to send the message when I get the textDidEndEditing
call. Finding if it is an unmodified return isn't too obvious.
NSText.h declares NSFormFeedChar, NSNewlineChar, and
NSCarrageReturnChar in addition to NSEnterChar. I suppose that one of
these is a normal return press, and the others are return + modifiers
(or enter + modifiers?). I'll play around to see how that works out.
With this, I think I have enough to check for both unmodified Enter and
unmodified Return presses (in the first case), and to check for
unmodified enters alone in the second case.
I'll see how it all works out. Thanks for your help!
Richard Schreyer