0

When i use mouseListener and I chek for a middle mouse button it does not react properly I don't know why but it looks like i need to scroll while cliking to get the event to occur some part of my code if it helps

public void mouseClicked(MouseEvent e) {
    if(new Rectangle(0,0,1274,30).contains(Screen.mse)){
        TopMenu.click();
    }else if(new Rectangle(0,31,1100,549).contains(Screen.mse)){
        Map.cliked(e.getButton(),0);
        System.out.println("mouse:"+e.getButton());
    }else if(new Rectangle(1100,30,174,550).contains(Screen.mse)){
        //cliked ModeMenu
    }else if(new Rectangle(0,580,1100,164).contains(Screen.mse)){
        //cliked ToolsMenu
    }else{
        //cliked mode change
    }

    switch(e.getModifiers()) {
      case InputEvent.BUTTON1_MASK: {
        System.out.println("That's the LEFT button");     
        break;
        }
      case InputEvent.BUTTON2_MASK: {
        System.out.println("That's the MIDDLE button");     
        break;
        }
      case InputEvent.BUTTON3_MASK: {
        System.out.println("That's the RIGHT button");     
        break;
        }
      }

}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • 2
    Have a look at [Which mouse button is the middle one?](http://stackoverflow.com/questions/8972267/which-mouse-button-is-the-middle-one). – Russell Zahniser Mar 26 '13 at 20:29

1 Answers1

1

If you look at the javadoxs for MouseEvent, you can see that BUTTON1, BUTTON2 and BUTTON3 are not referred to the left, middle and right mouse buttons. It depends on the mouse what BUTTON 1,2 and 3 mean, so it can happen that BUTTON2 does not refer to the middle Button. To see if the middle Button of your mouse is recognized correctly, try the following:

public void mouseClicked(MouseEvent e){
System.out.println(e.getButton());
}

Now press your middle mouse button. If there is no output in the console, your mouse has no middle button (or it is not recognized properly). If there is an output, it corresponds to the button(1=BUTTON1,2=BUTTON2,3=BUTTON3). If the ouput is 0, then the button is MouseEvent.NOBUTTON, which is unlikely to happen.

Another thing: Try using SwingUtilities.isMiddleButton(MouseEvent e). This may fix some problems with your mouse. If you do so, chage your mouseClicked() method to

public void mouseClicked(MouseEvent e)
{
    if(SwingUtilities.isLeftMouseButton(e))
    {
        System.out.println("That's the LEFT button"); 
    }
    else if(SwingUtilities.isMiddleMouseButton(e))
    {
        System.out.println("That's the MIDDLE button"); 
    }
    else if(SwingUtilities.isRightMouseButton(e))
    {
        System.out.println("That's the RIGHT button"); 
    }
}

(of course with all the other code you wrote above the original switch statement)

D180
  • 722
  • 6
  • 20