MouseEvents with JUNG and Java

时光怂恿深爱的人放手 提交于 2020-01-15 11:42:53

问题


I made a PluggableGraphMouse and 2 EditingGraphMousePluggings in my Java with JUNG program. If I set the modifiers to be left click and right click it works perfectly fine, here is the setModifiers code:

ovalMouse.setModifiers(MouseEvent.BUTTON1_MASK);
circleMouse.setModifiers(MouseEvent.BUTTON3_MASK);

What I'd like however is to have left click do one thing and SHIFT + left click (instead of right click) do the other. I've tried every combination I can think of but I can't seem to get it to work. Here are some of the more logical combinations I've tried that don't work:

//My logic here is Button1 AND Shift is down but this doesn't work
circleMouse.setModifiers(MouseEvent.BUTTON1_MASK & MouseEvent.SHIFT_DOWN_MASK);

// My logic here is Button1 AND Shift but this doesn't work either
circleMouse.setModifiers(MouseEvent.BUTTON1_MASK & MouseEvent.SHIFT_MASK);

// Also tried InputEvents but those didn't work either
circleMouse.setModifiers(InputEvent.BUTTON1_DOWN_MASK & InputEvent.SHIFT_DOWN_MASK);

If anyone knows what the correct modifiers are so I can use button 1 for ovalMouse and button 1 + shift for circleMouse please let me know. Thanks.


回答1:


To filter Shift+Button3 in any JUNG2's xxxGraphMousePlugin mouse event that implements MouseListener:

    System.out.println(circleMouse.getModifiers());
    if (( circleMouse.getModifiers() & (MouseEvent.SHIFT_MASK | MouseEvent.BUTTON3_MASK)) == (MouseEvent.SHIFT_MASK | MouseEvent.BUTTON3_MASK)){
        System.out.println(MouseEvent.getMouseModifiersText(circleMouse.getModifiers()));
    }

Update

So, if you want to differentiate a mouse event between BUTTON3 and SHIFT+BUTTON3, the following test will show you:

graphMouse.add(new MyPopupGraphMousePlugin());

protected class MyPopupGraphMousePlugin extends AbstractPopupGraphMousePlugin
implements MouseListener {

    @Override
    protected void handlePopup(MouseEvent e) {
        boolean filtered1 = false;
        boolean filtered2 = false;

        System.out.println(e.getModifiers());
        if (( e.getModifiers() & (MouseEvent.SHIFT_MASK | MouseEvent.BUTTON3_MASK)) == (MouseEvent.SHIFT_MASK | MouseEvent.BUTTON3_MASK)){
            filtered1 = true;
        }
        if (( e.getModifiers() & (MouseEvent.BUTTON3_MASK)) == (MouseEvent.BUTTON3_MASK)){
            filtered2 = true;
        }

        if(filtered2 == true) {
            System.out.println("BUTTON3");
        }
        if(filtered1 == true) {
            System.out.println("SHIFT+BUTTON3");
            //or do something more useful like pop up a JPopupMenu
        }       
    }
}

In the above test under JUNG2:

  1. With the SHIFT key: pressing SHIFT+BUTTON3 (SHIFT key + right-click mouse button) will show both "BUTTON3" and "SHIFT+BUTTON3" messages

  2. Except the SHIFT key: pressing any key + BUTTON3 (any key + right-click mouse button) will only show "BUTTON3" message



来源:https://stackoverflow.com/questions/5958622/mouseevents-with-jung-and-java

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!