Java判断鼠标左右键同时按下

The button mask returned by InputEvent.getModifiers() reflects only the button that changed state, not the current state of all buttons. (Note: Due to overlap in the values of ALT_MASK/BUTTON2_MASK and META_MASK/BUTTON3_MASK, this is not always true for mouse events involving modifier keys). To get the state of all buttons and modifier keys, use InputEvent.getModifiersEx().

InputEvent.getModifiers()只能反射单个键的更改状态,如果想获得组合键的状态,必须用InputEvent.getModifiersEx().

下面的代码我测试过,可以判断:

gText.addMouseListener(new MouseAdapter() {
     public void mousePressed(MouseEvent e)  {
         if(e.getButton()==e.BUTTON1 )
             gLabel.setText("button1"); //判断左键

         if( e.getButton()==e.BUTTON3)
             gLabel.setText("button3");//判断右键

         if(e.getModifiersEx()==(e.BUTTON3_DOWN_MASK + e.BUTTON1_DOWN_MASK))
             gLabel.setText("bt1+bt3!"); //判断左右键同时按下

         if(e.getModifiersEx()==(e.SHIFT_DOWN_MASK + e.BUTTON1_DOWN_MASK))
             gLabel.setText("shift+bt1");// shift+左键
             //其他雷同,修改常量值就可以
     }
});
 

 

你可能感兴趣的:(java)