Re: Return and Enter keys in NSTextView
Re: Return and Enter keys in NSTextView
- Subject: Re: Return and Enter keys in NSTextView
- From: Douglas Davidson <email@hidden>
- Date: Thu, 30 Aug 2001 08:33:15 -0700
On Wednesday, August 29, 2001, at 07:56 PM, Richard Schreyer wrote:
>
First:
>
As part of a general public chat, I have a small NSTextView, around 2
>
or 3 lines high. I want to send that chat whenever the user presses
>
return or enter. I didn't use a NSTextField because I wanted the
>
scrolling behavior that I get from a textview, in case the users
>
message runs longer than the few lines shown.
>
>
Overriding insertNewLine: would work perfectly well here, so this one
>
is pretty much solved now. I guess the final step would be to just
>
send a new message to the textviews delegate (probably simpler than
>
trying to add a small target/action system)
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:).
>
Second:
>
This is the more interesting problem. To send a private message to a
>
single user, I have a small window with a NSTextView and a send
>
button. Since these messages will be somewhat larger, I want to let
>
the user add newlines with the return key. When using a standard
>
textview, the user needs to move his hands to the mouse to press the
>
send button, since the active textview would not let a return/enter
>
press trigger the default button.
This is somewhat similar. You want the textview to act like a field
editor, as above, except that you want return to be bound to
insertNewlineIgnoringFieldEditor:, something that otherwise you would
get with option-return. There may be a way to do this without
subclassing, but I can't think of one right now. You could subclass,
override insertNewline:, and check [NSApp currentEvent] to see if it is
an unmodified return; if it is, call [super
insertNewlineIgnoringFieldEditor:sender], and if not just call [super
insertNewline:sender].
That's a bit of a hack, but if I think of something better I'll let you
know.
Douglas Davidson