Re: Catching keypresses in textfields
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.