• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Catching keypresses in textfields
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Catching keypresses in textfields


  • Subject: Re: Catching keypresses in textfields
  • From: Douglas Davidson <email@hidden>
  • Date: Fri, 19 Apr 2002 16:26:47 -0700

On Thursday, April 18, 2002, at 09:53 PM, Matthew Smith wrote:

How would I go about getting a textfield to catch the key events that are sent to it when a user types in it's field?

For instance: I have a console window in my program that allows the user to type command directly into it rather the use the interface, but need to let the user ad tabs to indent their commands. How would I get the textfield to print a tab in itself when the tab key is pressed rather then choose the next responder...

The last time I answered a question of this sort it was for a textview. Let me first repeat what I said then:

"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:.

A tab or backtab will (with the standard key bindings) end up using doCommandBySelector: to call insertTab: or insertBacktab: on the NSTextView. If the textview is not a field editor, insertTab: will call insertText: to insert a tab character; insertBacktab: will do nothing. If it is a field editor, this will end editing instead.

You should be able to implement the text view delegate method

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

and check whether aSelector == @selector(insertTab:), and at that point do something other than the default."

Now, if you are dealing with a textfield rather than a textview, then you will automatically get a shared textview configured as a field editor when your textfield has focus. The easiest thing then would probably be to implement the control delegate method

- (BOOL)control:(NSControl *)control textView:(NSTextView *)textView doCommandBySelector:(SEL)commandSelector;

in your textfield's delegate, and do something similar.

Douglas Davidson
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.

References: 
 >Catching keypresses in textfields (From: Matthew Smith <email@hidden>)

  • Prev by Date: Re: NSStepper - How can I tell which arrow has been clicked?
  • Next by Date: Re: Checking if volume is busy?
  • Previous by thread: Re: Catching keypresses in textfields
  • Next by thread: Scripting implementation - accepting/returning ae records?
  • Index(es):
    • Date
    • Thread