2

I have a Cocoa application I am building which contains an NSSearchField control. I want to enable a keyboard shortcut / key equivalent so when the uses presses COMMAND-OPTION-F, the search field gets focus.

However, after much searching, it is not clear to me what the best way to implement this is. There is not an option to set this for the NSSearchField in Interface Builder.

Is the solution to subclass NSSearchField and listen for the keyDown event (and then see if the key equivalent is pressed?)

1 Answer 1

4

You can always add a menu item with a key equivalent of F i.e. CommandOptionF. In the menu's action, just manually make the search field the first responder using

[window makeFirstResponder:searchField]; 
4
  • Yeah. I didnt really want to do that though, since it is odd (for the user) to have a menu item just to give control to the search field. Jan 14, 2010 at 5:59
  • 1
    Then you can override keyDown: in a controller class, and adjust your responder chain accordingly.
    – Saurabh Sharan
    Jan 14, 2010 at 6:03
  • Well, actually, that is exactly what Safari does Edit > Find > Google Search I guess if it is good enough for apple... Although I would like to know how to do this programmatically. Jan 14, 2010 at 6:03
  • Yeah, generally its good practice to have a menu item for these type of 'global' keyboard shortcuts. Usually best to follow Apple :)
    – Saurabh Sharan
    Jan 14, 2010 at 6:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.