Maintaining text field focus after matrix button push?

I have a situation where I need to enter text into a text field, but another user may press some other buttons on the same window (using keyboard commands) while I’m in the middle of typing, and I need it not to interfere with my text field data entry. The text field is set as first responder, and it holds focus when I press other normal momentary buttons (via keyboard commands), but if I press a radio button (via a keyboard command) I lose focus on the text field. Is there a way to prevent this? What’s interesting is if I just click on the radio button with the mouse, instead of using the key command, the text field focus is maintained. I also seem to lose the ability to tab between my text fields after I have pressed one of the radio buttons (via a keyboard command). Perhaps there is something wrong with the way I’m doing the keyboard assignments? The radio buttons seem to behave differently depending on if I click with a mouse, or use the keyboard shortcut. If I click on them, I keep my text field focus and can tab between text fields, but if I use a keyboard command, I lose focus and can no longer tab between them. The workaround of simply re-issuing the first reponder command with the onClicked won’t work because the text field comes up selected, and I’d lose everything I just typed up to that point if I’m in the middle of typing. Thanks so much for any advice. Hopefully it’s possible. :slight_smile:

-Evan

Hi Evan,

was it possible to use a text view instead of a text field? Solving your problem would be much easier then - for example:

  • make the text view delegate of your matrix
  • subclass NSMatrix, overwrite this method in the subclass and set it as custom class for the Matrix:
- (BOOL)acceptsFirstResponder{
	[[self window] makeFirstResponder:[self delegate]];
	return YES;
}

then you should have the requested behaviour.

The same method wouldn’t succeed with a text field since it uses it’s window’s field editor for editing. For a solution a custom field editor was needed as far as I can see …

D.

I think I almost have it, but how do you overwrite this method in the subclass? Sorry, this is pushing my abilities to the brink. :slight_smile: Thanks.

-Evan

Sorry, my mistake - I should have written ‘override’ not ‘overwrite’. To override a method of a subclasses’ super class you just write an identically named method in the subclass.
In my example your MYSubclass.m file should look like this:

#import "MYSubclass.h"

@implementation MYSubclass

- (BOOL)acceptsFirstResponder{
   [[self window] makeFirstResponder:[self delegate]];
   return YES;
}

@end

MYSubclass.h does not need any modification in your case

hope that helps …

D.

Ok, got that to work, thanks so much, but now is there any way to retain the “OnAction” when I hit enter in the text view? It looks like text views don’t work that way… :slight_smile:

-Evan

Is there a way to have an action on “enter” with a text view the way a text field can? I was using on button down, but I discovered a flaw in the way I’m implementing it, and having the action run on enter is much better. I hope it is possible… :slight_smile:

-Evan

Hi Evan,

it’s probably possible to influence a text view’s behaviour to simulate a text field but I think I might have found a better solution for you - using text fields as you requested at first (sorry - I haven’t thought of this when answering the first time …)

My idea is simple - instead of letting the matrix refuse it’s first responder status (which will mean that the field editor already has lost it’s first responder status) we use a subclass of NSWindow that refuses to switch the first responder to your matrix.

But - this solution has two limitations:

  • text fields and matrix have to be in the same window

  • and the behaviour will affect all text fields of that window
    would that be ok for your project? Then this could be the solution:

  • make the NSMatrix delegate of the window

  • and use this subclass for your window:

#import "MYWindow.h"

@implementation MYWindow

- (BOOL)makeFirstResponder:(NSResponder *)aResponder{
	if ([aResponder isEqual:[self delegate]]) {
		return NO;
	} else {		
		return ([super makeFirstResponder:aResponder]);
	}
}

@end

Please tell me, what you think about this …

Dominik

BRILLIANT!!! :slight_smile: Exactly what I needed. Everything is indeed in the same window, and I really just wanted to take the matrix out as a first responder so this did the trick. Thanks SO much.

-Evan