我使用JUNG程序在Java中制作了PluggableGraphMouse和2 EditingGraphMousePluggings。如果我将修饰符设置为左键单击和右键单击,则效果很好,这是setModifiers代码:
ovalMouse.setModifiers(MouseEvent.BUTTON1_MASK);
circleMouse.setModifiers(MouseEvent.BUTTON3_MASK);
但是我想要的是让鼠标左键做一件事,而按住SHIFT +鼠标左键(而不是右键)做另一件事。我尝试了所有我能想到的组合,但似乎无法使它起作用。这是我尝试过的一些不合逻辑的组合,但无效:
//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);
如果有人知道正确的修饰符是什么,那么我可以为椭圆形鼠标使用按钮1,为circleMouse使用按钮1 +移位,请告诉我。谢谢。
最佳答案
要在实现JUNG2
的任何MouseListener
的xxxGraphMousePlugin鼠标事件中过滤Shift + Button3:
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()));
}
更新资料
因此,如果要区分
BUTTON3
和SHIFT+BUTTON3
的鼠标事件,以下测试将向您显示: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
}
}
}
在上述
JUNG2
下的测试中:使用SHIFT键:按下
SHIFT+BUTTON3
(SHIFT键+鼠标右键)将同时显示“ BUTTON3”和“ SHIFT + BUTTON3”消息除SHIFT键外:按
any key + BUTTON3
(任意键+鼠标右键)仅显示“ BUTTON3”消息