In my app I display a sheet with an NSComboBox in it. If the user uses the arrow keys to choose an entry from the menu and then Return to select it the sheet's OK button is also actioned, as it has Return as its key equivalent. I'd like to stop these Return keys acting as OK clicks.
After trying lots of things (apart from subclassing NSComboBox which started turning into a nightmare) I'm trying to ignore OK clicks if the combo box is the first responder and the current event is a return key key-up, but the NSPanel that the sheet is subclassed from always returns itself as the current first responder. I was expecting a field editor but all I get is the NSPanel.
1) Is there a better way of doing this? The user REALLY wants keyboard based data entry rather than mousing around.
2) If this is the best way, how can I tell that the combo box is the first responder?
Any help gratefully accepted. Surely this has been dealt with before?
Rev. Andy
Asked
Active
Viewed 383 times
1

Rev. Andy
- 81
- 1
- 5
-
did you try by making combobox firstresponder? – Anoop Vaidya Mar 04 '13 at 14:37
-
I think the problem is that while the combo box's menu is showing there isn't a clear value for first responder - which is why it ends up being pointed at the window. – Rev. Andy Mar 06 '13 at 12:07
-
1The horrible workaround is to record the time that the menu was dismissed (in comboBoxWillDismiss:) and check the time when the OK button selector is called - if there's less that 0.1s between them I ignore the OK. The OK button still flashes but at least the general functionality works. Ugly. – Rev. Andy Mar 06 '13 at 12:11
1 Answers
1
There is a workaround, its not pretty but it will work:
Register for both notifications NSComboBoxWillPopUpNotification and NSComboBoxWillDismissNotification.
- (void)comboBoxWillPopUp:(NSNotification *)notification
{
okButton.keyEquivalent = @"";
}
- (void)comboBoxWillDismiss:(NSNotification *)notification
{
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0), dispatch_get_main_queue(), ^{
okButton.keyEquivalent = @"\r";
});
}

Elden
- 636
- 1
- 6
- 21