I want to give NSButton
a highlighted border to guide user to click it. I expect it to be similar to the effect of using becomeFirstResponder()
on NSTextField
as below.
I tried to use becomeFirstResponder()
on NSButton
, but there is no UI change.
I also tried to use button.highlight(true)
, but it highlights the whole button, which is kind of confusing as it is being clicked.
What should I do? Thank you.
As commentators have pointed out doing what you propose is unwise as it would conflict with full keyboard access, which some of your users could have enabled.
There is already a macOS convention to highlight the "default" button in some way – this formerly added a ring around the button, currently it draws the button in a different colour. The default button will be triggered when the user presses the return key (whereas in full keyboard access the space key is the trigger).
You can dynamically set and change the default button using NSWindow
's defaultButtonCell
property. Note this takes an NSButtonCell
, not an NSButton
, you get the former from the latter using the cell
property. So your code to set the default would look something like:
myWindow.defaultButtonCell = oneOfMyButtons.cell;
Following the standard macOS GUI here both indicates to the user the most common button to press and allows them to trigger it from the keyboard using the return key.
HTH