Skip navigation.
 
mlRe: "Enter" key down
FROM : Douglas Davidson
DATE : Thu May 29 19:22:17 2008

On May 29, 2008, at 10:13 AM, Aki Inoue wrote:

> When you want to handle key events in NSTextView, see -
> textView:doCommandBySelector: delegate method.
> http://developer.apple.com/documentation/Cocoa/Reference/ApplicationKit/Classes/NSTextView_Class/Reference/Reference.html#/
> /apple_ref/occ/instm/NSObject/textView:doCommandBySelector:


Let me summarize what was said when this question last came up:

Here is the normal sequence when a text view receives key 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.  An arbitrary text view can be made to act in either of these 
ways by calling setFieldEditor:.

To do something special for an enter vs. a return, you should be able 
to implement the text view delegate method

- (BOOL)textView:(NSTextView *)aTextView doCommandBySelector:
(SEL)aSelector;

and, if aSelector == @selector(insertNewline:), examine [NSApp 
currentEvent] to make sure that it is a key event (it should be, 
unless someone is calling doCommandBySelector: directly), and if so 
what key was pressed.

Alternatively, if you wished to do this in a text view subclass rather 
than in the delegate, you could override insertNewline: to similar 
effect.

Douglas Davidson

Related mailsAuthorDate
ml"Enter" key down Micha Fuhrmann May 29, 14:03
mlRe: "Enter" key down I. Savant May 29, 15:33
mlRe: "Enter" key down Aki Inoue May 29, 19:13
mlRe: "Enter" key down Douglas Davidson May 29, 19:22
mlRe: "Enter" key down I. Savant May 29, 19:27