Change mnemonic modifier key in Java/Swing -


setting focus hot keys in swing easy:

  tfldplantsneeded = new jtextfield( field_len_med );   lblplantsneeded = new jlabel( "plants needed" );   lblplantsneeded.setdisplayedmnemonic( 'p' );   lblplantsneeded.setlabelfor( tfldplantsneeded ); 

this give focus tfldplantsneeded jtextfield when user presses alt+p. highlights/displays character trigger focus change. (in case, when alt pressed, 'p' in "plants" underlined.)

this great ... well, kinda. on mac, when user presses alt (which option on mac keyboard) mnemonic highlited, focus change isn't triggered when p pressed too. if, however, user presses control + option + p, works "expected" , focus changed. (as aside, if user press option + p, focused text field funny characters inserted.)

i know can myself specifying custom keybindings via getinputmap , getactionmap, is there way change application global mnemonic modifier can use automatic keybindings , trigger character highlighting? (in case, use command or meta mnemonic modifer key.)

apparently isn't straightforward might think, there way.

first of all, menus (jmenu) there property controlled , feel called menu.shortcutkeyswhich can set manually. sets mnemonic modifier menus in specific , feel. if want more information feel free ask.

in order set mnemonic modifier everything, need override default toolkit (toolkit). first of all, run main method find following lines

system.out.println(system.getproperty("java.awt.headless")); system.out.println(system.getproperty("awt.toolkit")); 

if first line null of false (see java.awt.toolkit getdefaulttoolkit()) second line give class name used default toolkit system. use windows , second line gives output sun.awt.windows.wtoolkit. create class overrides getfocusacceleratorkeymask in default toolkit. me looks this

public class mytoolkit extends wtoolkit {      @override     public int getfocusacceleratorkeymask() {         return inputevent.ctrl_down_mask;     } } 

finally, have tell system use it. in application, put line

system.setproperty("awt.toolkit", "packagename.mytoolkit"); 

where need set correct package path class. make sure line placed before starting gui related code, preferably in first lines of main. should set control global mnemonic modifier (or use meta_down_mask if that's want. @ java.awt.event.inputevent mask list.).


Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

android - Keyboard hides my half of edit-text and button below it even in scroll view -

css - Make div keyboard-scrollable in jQuery Mobile? -