java - How to catch a CTRL + mouseWheel event with InputMap -
java - How to catch a CTRL + mouseWheel event with InputMap -
i've implemented hotkeys swing application inputmap like
getinputmap(jcomponent.when_in_focused_window).put(keystroke.getkeystroke(keyevent.vk_a, event.ctrl_mask), "selectall"); getactionmap().put("selectall", new selectallaction()); it's working fine. now, how can same thing if want grab the
ctrl + mousewheelup
i've tried combinations such as
getinputmap(jcomponent.when_in_focused_window).put(keystroke.getkeystroke(mouseevent.mouse_wheel, event.ctrl_mask), "zoom"); with no luck
thanks
you can't utilize inputmap/actionmap this. need utilize mousewheellistener. listener can access custom action actionmap. here simple illustration uses "control 1" keystroke:
import java.awt.*; import java.awt.event.*; import javax.swing.*; public class mousewheeltest extends jpanel implements mousewheellistener { private final static string some_action = "control 1"; public static void main(string[] args) { swingutilities.invokelater(new runnable() { @override public void run() { createandshowgui(); } }); } private static void createandshowgui() { jframe frame = new jframe("mousewheeltest"); frame.setdefaultcloseoperation(jframe.exit_on_close); frame.add( new mousewheeltest() ); frame.pack(); frame.setlocationrelativeto( null ); frame.setvisible(true); } public mousewheeltest() { super(new borderlayout()); jtextarea textarea = new jtextarea(10, 40); jscrollpane scrollpane = new jscrollpane(textarea); add(scrollpane, borderlayout.center); textarea.addmousewheellistener(this); action someaction = new abstractaction() { public void actionperformed(actionevent e) { system.out.println("do action"); } }; // command used text area seek different key textarea.getinputmap(jcomponent.when_in_focused_window) .put(keystroke.getkeystroke(some_action), some_action); textarea.getactionmap().put(some_action, someaction); } public void mousewheelmoved(mousewheelevent e) { if (e.iscontroldown()) if (e.getwheelrotation() < 0) { jcomponent component = (jcomponent)e.getcomponent(); action action = component.getactionmap().get(some_action); if (action != null) action.actionperformed( null ); } else { system.out.println("scrolled down"); } } } java swing hotkeys
Comments
Post a Comment