Skip navigation.
 
mlRe: Help needed with the key down event in NSTextField
FROM : Douglas Davidson
DATE : Tue Jun 20 17:43:54 2006

On Jun 20, 2006, at 5:45 AM, Vinay Prabhu wrote:

> I am using NSTextFiled in my application.
> I have subclassed the NSTextField and implemented the
> "keyDown" and "keyUp" methods.
>
> The problem is, when any key pressed, only the "keyUp" is called 
> "keyDown"
> is not called.
> Any idea, when "keyDown" is called?
>


First, NSTextField and similar controls generally do not deal with 
editing.  When a control is editing, a field editor (an NSTextView) 
is used.  Read the documentation on field editors.

Second, you probably do not want to deal with key events at the 
keyDown: and keyUp: level.  Here is my usual introduction to key 
event processing:

> 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, non-text keys like enter or return will (with the 
> standard key bindings) end up using doCommandBySelector: to call 
> methods like insertNewline: on the NSTextView.
>
> If you are using an NSTextView, you should be able to implement the 
> text view delegate method
>
> - (BOOL)textView:(NSTextView *)aTextView doCommandBySelector:(SEL)
> aSelector;
>
> and examine the selector.  If you are using an NSTextField or 
> similar control, you can implement their delegate method
>
> - (BOOL)control:(NSControl *)control textView:(NSTextView *)
> textView doCommandBySelector:(SEL)aSelector;
>
> If you're interested in normal text keys, then the best option 
> would be to use an NSFormatter.  That allows you to validate 
> partial and/or complete strings.
>
> If you want to do something other than what NSFormatter does, you 
> can use text-did-change notifications and the like, but the details 
> depend on exactly what you want to do.


Douglas Davidson

Related mailsAuthorDate
mlHelp needed with the key down event in NSTextField Vinay Prabhu Jun 20, 14:45
mlRe: Help needed with the key down event in NSTextField Nick Kreeger Jun 20, 17:28
mlRe: Help needed with the key down event in NSTextField Douglas Davidson Jun 20, 17:43
mlRE: Help needed with the key down event in NSTextField Vinay Prabhu Jun 21, 09:13
mlRe: Help needed with the key down event in NSTextField Chris Suter Jun 21, 09:18