Skip navigation.
 
mlRe: [newbie] /r invoking button instead of going to textfield?
FROM : Matt Neuburg
DATE : Mon Sep 16 19:26:45 2002

On Sun, 15 Sep 2002 13:18:41 -0700, Ando Sonenblick <<email_removed>>
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_removed>, 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_removed>
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.

Related mailsAuthorDate
ml[newbie] /r invoking button instead of going to textfield? Ando Sonenblick Sep 15, 22:18
mlRe: [newbie] /r invoking button instead of going to textfield? Ondra Cada Sep 15, 22:28
mlRe: [newbie] /r invoking button instead of going to textfield? Ando Sonenblick Sep 15, 23:32
mlRe: [newbie] /r invoking button instead of going to textfield? James DiPalma Sep 16, 00:15
mlRe: [newbie] /r invoking button instead of going to textfield? Ondra Cada Sep 16, 00:55
mlRe: [newbie] /r invoking button instead of going to textfield? Matt Neuburg Sep 16, 19:26