Skip navigation.
 
mlRe: How to detect focus on an NSTextField
FROM : Bill Garrison
DATE : Fri Feb 08 18:29:45 2008

On Feb 8, 2008, at 2:32 AM, Adam Gerson wrote:

> I have done some exhaustive searching on google to find a way to 
> detect if my NSTextField has focus (if the user has click in it or 
> has typed something in it). The best I could find is:
>
> [urlEntryTextField currentEditor];
>
> Which does not appear to work 100% of the time. I have an editable 
> NSTableView that doesn't want to give up focus under certain 
> conditions when I click out of it and into the NSTextView.
>
> I found alot of convoluted posts. Is there an simple way to detect 
> if a control has current focus?


Adam,

If you need to detect when a particular control has become first 
responder, you could subclass the control and override -
becomeFirstResponder to signal your application in some way that the 
control has just gotten focus.

There is the existing -controlTextDidBeginEditing delegate, but it 
doesn't fire until the user acts on the field after it has obtained 
focus.

I just did this last night for an NSSecureTextField.  My subclass 
overrides -becomeFirstResponder to invoke an informally defined 
delegate method.

I needed to detect when the text field first got focus so that I could 
enable a window controller to populate the text field's content from 
an item in the user's keychain.  In IB, I changed the class of the 
secure text field to my subclass and made the window controller its 
delegate.  In my window controller, I then implemented the custom 
control's new custom delegate method, -
controlDidBecomeFirstResponder.  As soon as the secure text field got 
focus, the custom -controlDidBecomeFirstResponder: method fired and I 
was able to respond accordingly.

Maybe something similar would work for you.

Bill



MySecureTextField.m
------------------------------

#import "MySecureTextField.h"
#import "NSControl+FirstResponderNotification.h" // defines the custom 
notification and a corresponding informal delegate method

@implementation MySecureTextField

- (id)initWithFrame:(NSRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code here.
    }
    return self;
}

- (BOOL)becomeFirstResponder;
{
   // If the control's delegate responds to 
controlDidBecomeFirstResponder, invoke it.  Also post a notification.
   BOOL didBecomeFirstResponder = [super becomeFirstResponder];
   NSNotification *notification = [NSNotification 
notificationWithName:MyControlDidBecomeFirstResponderNotification 
object:self];
   if ( [self delegate] && [[self delegate] 
respondsToSelector:@selector(controlDidBecomeFirstResponder:)] ) {
       [[self delegate] controlDidBecomeFirstResponder:notification];
   }
   [[NSNotificationCenter defaultCenter] postNotification:notification];
   return didBecomeFirstResponder;
}
@end



NSControl+FirstResponderNotification.h
--------------------------------------------------------------------------------
extern NSString * const MyControlDidBecomeFirstResponderNotification;
@interface NSControl (FirstResponderNotification)

/*!
@method controlDidBecomeFirstResponder
@abstract An informal protocol method to be invoked by NSControl 
delegates when the receiver becomes first responder.
@param notification A MyControlDidBecomeFirstResponderNotification. 
The notification object is the control that became first responder. 
There is no userInfo.
*/
- (void) controlDidBecomeFirstResponder:(NSNotification *)aNotification;

@end

Related mailsAuthorDate
mlHow to detect focus on an NSTextField Adam Gerson Feb 8, 08:32
mlRe: How to detect focus on an NSTextField Joachim Deelen Feb 8, 10:56
mlRe: How to detect focus on an NSTextField Jens Alfke Feb 8, 17:02
mlRe: How to detect focus on an NSTextField Kyle Sluder Feb 8, 17:08
mlRe: How to detect focus on an NSTextField Bill Garrison Feb 8, 18:29
mlRe: How to detect focus on an NSTextField Adam Gerson Feb 9, 00:19
mlRe: How to detect focus on an NSTextField Adam Gerson Feb 18, 09:41