Setting focus hot keys in Swing is very easy:
tfldPlantsNeeded = new JTextField( FIELD_LEN_MED );
lblPlantsNeeded = new JLabel( "Plants Needed" );
lblPlantsNeeded.setDisplayedMnemonic( 'p' );
lblPlantsNeeded.setLabelFor( tfldPlantsNeeded );
This will give focus to the tfldPlantsNeeded
JTextField when the user presses ALT+p
. It also highlights/displays the character that will trigger the focus change. (In this case, when ALT
is pressed, the 'P' in "Plants" is underlined.)
This is great ... well, kinda. On a Mac, when the user presses ALT
(which is also Option
on the Mac keyboard) the mnemonic is highlited, but the focus change isn't triggered when p
is pressed too. If, however, the user presses Control + Option + p
, then it works as "expected" and focus is changed. (As an aside, if the user DOES press Option + p
, the currently focused text field will get funny characters inserted.)
I know that I can do this myself by specifying custom keybindings via getInputMap
and getActionMap
, but is there a way to change the application global mnemonic modifier so that we can use the automatic keybindings and trigger character highlighting? (In my case, I would like to use Command
or Meta
as the mnemonic modifer key.)