How to check if a NSTextField has focus.

Discussion in 'Mac Programming' started by petron, Jan 23, 2010.

  1. petron macrumors member

    Joined:
    May 22, 2009
    Location:
    Malmo, Sweden
    #1
    Hi,
    I wonder if there is a way to simply find out if a specific NSTextField is active or in Focus.

    Probably one can use the firstResponder... like this but I get warning
    NSResponder may not respond to delegate...

    Code:
    NSTextField *aFiled = [thisWindow firstResponder] delegate]
    if (aField == muTextField) {
        NSLog(@"muTextField is in focus");
    } else {
       NSLog(@"muTextField is not in focus");
    }

    Are there any other means of getting this done ?

    I will appreciate a tip.
    Thank you in advance.
    /petron
     
  2. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #2
    Just compare the firstResponder directly:
    Code:
    if ([thisWindow firstResponder] == muTextField) ...
     
  3. petron thread starter macrumors member

    Joined:
    May 22, 2009
    Location:
    Malmo, Sweden
    #3
    Hi Kainjow

    It does not help.

    It never be equal, I have tested it already. I got it equal only if I refer to "delegate" as in my example,then it works but there is a warning.

    Any clue why ?

    /petron
     
  4. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
  5. petron thread starter macrumors member

    Joined:
    May 22, 2009
    Location:
    Malmo, Sweden
    #5
    Thanks for trying.
    In NSDocument application.

    I have several NSTextFields and depending on which one is active/in Focus a specified action is required when NSButton is pressed.

    I got it working by doing this, but do not understand why.

    Code:
        if ([(id)[thisWindow firstResponder] delegate] == (id)bookmarkField) {
            NSLog(@" field active make action A");
        } else {
            NSLog(@" field not active make action B");
        }
    
    /petron
     
  6. Sydde macrumors 68020

    Sydde

    Joined:
    Aug 17, 2009
    #6
    I believe a text field will send NSControl's delegate a -control:textShouldBeginEditing: whenever it gets focus (assuming it is enabled and editable). You could make your NSDocument object (or some other object) a delegate to the text fields and use that message to set the action on the button.
     
  7. petron thread starter macrumors member

    Joined:
    May 22, 2009
    Location:
    Malmo, Sweden
    #7
    Hi Sydde,
    Well it can be done, but I am not interested when it gets focus, I am interested if it has focus when I press a button or press enter.

    I got it working when pressing button , but when pressing enter I do get different firstResponder. This bothers me.
    What happen with responder when we press ENTER, Why it is not longer the highlighted NSTextField...

    Lots of Questions..

    Thanks any way.
    /petron
     
  8. Sydde macrumors 68020

    Sydde

    Joined:
    Aug 17, 2009
    #8
    For a NSTextField, enter is a signal that editing is done. Toucan also capture this with a delegate method, -control:shouldEndEditing: . This can be useful if you want the program to decide which field should get focus (e.g., if the content of the field might suggest targeting a different field). I have not studied up much on the responder chain, you might want to.
     
  9. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #9
    Oh I just realized... The first responder may be the field editor. you may want to read up on that too.
     
  10. petron thread starter macrumors member

    Joined:
    May 22, 2009
    Location:
    Malmo, Sweden
    #10
    Thanks for comments I got it working in another way. No sides effects as when using the firstRespnder construction.

    in the
    - (void)awakeFromNib

    I set up the target and action of the NSTextField

    Code:
        [muTextField setTarget:self];
        [muTextField setAction:@selector(makeAnAction:)];
    
    Then pressing ENTER the makeAnAction method is called.
    This works fine for me.

    /petron
     
  11. Mikhail1799 macrumors newbie

    Joined:
    Feb 9, 2010
    #11
    To determine if NSTextField is in focus, you can use

    Code:
    - (BOOL)isTextFieldInFocus:(NSTextField *)textField
    {
    	BOOL inFocus = NO;
    	
    	inFocus = ([[[textField window] firstResponder] isKindOfClass:[NSTextView class]]
    			   && [[textField window] fieldEditor:NO forObject:nil]!=nil
    			   && [textField isEqualTo:(id)[(NSTextView *)[[textField window] firstResponder]delegate]]);
    	
    	return inFocus;
    }
    
     
  12. The Cappy macrumors member

    Joined:
    Nov 9, 2015
    Location:
    Dunwich Fish Market
    #12
    Fails if there are two NSTextViews in that window. The check needs to be more direct.
     

Share This Page