Re: [newbie] /r invoking button instead of going to textfield?
Re: [newbie] /r invoking button instead of going to textfield?
- Subject: Re: [newbie] /r invoking button instead of going to textfield?
- From: Matt Neuburg <email@hidden>
- Date: Mon, 16 Sep 2002 10:26:45 -0700
On Sun, 15 Sep 2002 13:18:41 -0700, Ando Sonenblick <email@hidden>
said:
>
>
Easy one for you been-around-the-blockers,
>
>
I have a window with an NSTextField and an NSButton in it. The button is
>
set to have return activate it. However, with the NSTextField as the first
>
responder it "eats" any return key presses.
>
>
So how do I make it so that the return key activates the button no matter
>
who else may be the first responder?
I have a solution but I found it far from easy to arrive at. I find Cocoa's
way of dealing with the return key in this situation inconsistent and
unpredictable. Sometimes it eats the return key and causes the whole text
to be selected, sometimes it doesn't and the button gets pressed. To bring
order to the situation, I catch the delegate method
control:textView:doCommandBySelector: and force the behavior I want using
delayed messaging. So:
- (BOOL)control:(NSControl *)control
textView:(NSTextView *)textView doCommandBySelector:(SEL)command {
if ([NSStringFromSelector(command) isEqualToString: @"insertNewline:"]) {
if ([[control window] makeFirstResponder: [control window]]) {
if ([[findButton keyEquivalent] isEqualToString: @"\r"])
[findButton performSelector: @selector(performClick:)
withObject: nil
afterDelay: 0.05];
}
return YES;
}
return NO;
}
Here, "findButton" is the default button, but I suppose you could
generalize this easily using -defaultButtonCell.
m.
--
matt neuburg, phd = email@hidden,
http://www.tidbits.com/matt
pantes anthropoi tou eidenai oregontai phusei
Subscribe to TidBITS! It's free and smart.
http://www.tidbits.com/
_______________________________________________
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.