Equiv key. for nsbutton does not work as expected for multiple nsbuttons

I have two nsbuttons in my interface builder. For all three of these nsbuttons, I set the Key Equilant to "Return". And I also set the following look for all these buttons.

I have three different actions for all three buttons and the connections are correct. If I use a mouse click, then the appropriate actions are taken.

After starting the app, first my first button has focus, hit the back button, the 1st button action is performed. Next, I pressed the tabs key, the focus was changed to the 2nd button, the back key was pressed, but the 1st button action is done. the tab key is pressed, the focus has moved to the 3rd button, the return key is pressed, the 1st button action is performed.

What I am missing here. Why is the appropriate action not executed when the return key is pressed on the nsbutton, even focus is highlighted.


source to share

2 answers

It looks like you are using keyboard navigation to switch between buttons and activate the selected one. In this case, the return key usually corresponds to pressing the selected button. However, since you assigned Return as a shortcut to one or more buttons, the responder chain searches for and finds a button with the equivalent of the equivalent key, so the button message is sent instead.

Try clearing the key equivalent for all three buttons. I think this will give the behavior you are looking for.

If you are not using keyboard navigation, it is not clear why the tab button has any effect. However, if you are trying to do something like making the buttons loop by default from one button to the next, you will need to change the keyboard equivalent every time the button is pressed. I wouldn't recommend this at all - I don't think users would like the default button to change from one moment to the next. If you need, then here's the code:

- (IBAction)nextButton:(NSButton*)sender
    int tag = [sender tag];
    NSView *superview = [sender superview];
    if ([sender.keyEquivalent isEqualToString:@"\r"]) {
        NSButton *nextButton = [superview viewWithTag:(tag % 3) + 1];
        nextButton.keyEquivalent = @"\r";
        sender.keyEquivalent = @"";


This assumes you have three buttons and each is configured with a method nextButton:

as its action. In addition, buttons have tags 1, 2, and 3, respectively. The idea here is that when a default button is selected (that is, one that has a return key as its equivalent), it sets the next button key to be equivalent to Return and sets its own equivalent to nothing.

Obviously, you can change the way the code works - for example, you might want each button to trigger a different action. In this case, just call each call with a generic method that does the same job as the code above.

The lesson is that if you set the same key equivalent for multiple buttons, the first one to be found will be "clicked". If you want to change the equivalent of a key, you need to change the equivalent set for your various buttons.



I'm not sure what you mean by changing focus when you press the tab key. I don't see this behavior (I have set up the initial first responder and the following key connections). All three buttons are blue, but only the first one pulses no matter which keys I press. After some experimentation, I found that the button that is at the top of the list (in the window's object list) is the one that takes action when return is pressed.

I can't find any documentation on this, but I don't think you can set the same key equivalent for multiple buttons in the same window.



All Articles