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: Wed, 29 Aug 2001 19:56:36 -0700
On Wednesday, August 29, 2001, at 04:31 PM, Douglas Davidson wrote:
Overriding NSTextView's keyDown: usually is not the best thing to do.
This will get you raw key events as they come in, before they have been
processed by any of the text machinery--in particular, input management
or key binding. Do you really want every press of the return key, even
those that might end up being passed to some input method rather than
ending up as returns in the text?
Usually it is better to intervene somewhere else in the processing of
events. NSTextView's keyDown: passes events to interpretKeyEvents:,
which is where they enter key binding and input management. They come
out either as insertText: or as doCommandBySelector: (see NSResponder.h
for these three methods).
In particular, an enter or return will (with the standard key bindings)
end up using doCommandBySelector: to call insertNewline: on the
NSTextView. If the textview is not a field editor, this will call
insertText: to insert a newline character. If it is a field editor
(for example, when editing a textfield) this will end editing instead.
You may wish to override insertNewline: instead. This will tell you
when the user does the equivalent of pressing enter or return, by
whatever means, or however they have remapped their keybindings.
Unfortunately, I think I need to know which key was pressed, enter or
return.
In my program (internet chat), I actually have 2 different situations
I'm working with.
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)
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 fairly annoying, so I am trying to provide a shortcut where the
user can press return to get a newline, and enter to trigger the send
button.
I actually have this basically functioning now, with some sample code
off of Omni's Dev List archives (where I got the keyDown: override
idea), but it doesn't always work. Sometimes return presses get through
without sending chat. It looked like an imperfect solution, which is
why I'm here trying to find a better way to do it.
Richard Schreyer