java swing shortcut key / key bindings in a JPanel -
i have jpanel lots of components in it. when user presses "a", want , consume "a", except if user in textbox (or other part of screen accepts "a")--in case, don't want know "a".
in code below, notified of "a", if focus on text box (typing "a" while in textbox puts "a" in textbox , notifies me "a").
jcomponent jc = panel...; inputmap inputmap = jc.getinputmap(jcomponent.when_ancestor_of_focused_component); actionmap actionmap = jc.getactionmap(); inputmap.put(keystroke.getkeystroke(keyevent.vk_a, 0), "qcaccept"); actionmap.put("qcaccept", new abstractaction("qcaccept") { @override public void actionperformed(actionevent e) { system.out.println("a pressed, " + e); } });
inputmap.put(keystroke.getkeystroke(keyevent.vk_a, 0), "qcaccept");
you listening keypressed event. text components listen keytyped event. why both bindings still working. try:
inputmap.put(keystroke.getkeystroke("typed a"), "qcaccept");
Comments
Post a Comment