• 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: Julian Barkway <email@hidden>
  • Date: Fri, 19 Apr 2002 20:21:06 +0200

Hello all,

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

Thanks for the help,

Matt Smith


The way I did it was to override my view's NSResponder's keyDown: message. You can then query the event passed to the method to determine what key has been pressed and then invoke a method in the view's delegate to do whatever needs to be done. The following snippet traps the return key but it shouldn't take too much fiddling to adapt it for the tab key:

- (void) keyDown: (NSEvent *) theEvent
{
BOOL handled = NO;

switch ([theEvent keyCode]) {
case RETURN_KEY_CODE:
handled = [[self delegate] handleReturnKey]; // See if the delegate wants to handle it...
break;
// .... (check for some other keys)...
}
if (!handled) // Not one of 'my' keys or the delegate wasn't interested. See if anybody else wants to know...
[self interpretKeyEvents: [NSArray arrayWithObject: theEvent]];
}

The last line merely duplicates the normal functionality of keyDown:

Hope this helps,

Julian
_______________________________________________
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: programmatically switching the keyboard layout
  • Next by Date: Re: NSBitmapImageRep problem (I think)
  • Previous by thread: Catching keypresses in textfields
  • Next by thread: Re: Catching keypresses in textfields
  • Index(es):
    • Date
    • Thread