cocoaappkitnsresponder

Determine whether all controls can get focus (i.e. become first responder)


I need to detect whether any NSControl, such as an NSButton, can get the focus by using the Tab key.

It can, if the user has enabled the option in the System Preferences under Keyboard / Shortcuts / Keyboard / "Change the way Tab moves focus".

More specifically, I like to make a NSButton the first responder but only if the "all controls can get focus" mode is enabled. Maybe I could ask the control if it accepts the firstReponder status?

I tried calling acceptsFirstResponder on a button, but that returns YES even if it can't become firstReponder by using the Tab key, so that's not the right way.


Solution

  • canBecomeKeyView is the property/method you're looking for.

    From Cocoa Event Handling Guide, Keyboard Interface Control

    The acceptsFirstResponder method controls whether a responder accepts first responder status when its window asks it to (that is, when makeFirstResponder: is called with the responder as the parameter). The canBecomeKeyView method controls whether the Application Kit allows tabbing to a view. It calls acceptsFirstResponder, but it also checks for other information before determining the value to return, such as whether the view is hidden and whether full keyboard access is on.