Which mouse button is the middle one?

23,771

Solution 1

To determine which of the Mouse buttons is pressed, these three methods from SwingUtilities could help you:

  1. isLeftMouseButton
  2. isMiddleMouseButton
  3. isRightMouseButton

Solution 2

You can use the utilities methods from SwingUtilties:

SwingUtilities.isLeftMouseButton(MouseEvent anEvent)   
SwingUtilities.isRightMouseButton(MouseEvent anEvent) 
SwingUtilities.isMiddleMouseButton(MouseEvent anEvent)

Solution 3

There is also MouseEvent.isPopupTrigger(). This method should return true, if the right mouse button is pressed.

Share:
23,771

Related videos on Youtube

Radu Murzea
Author by

Radu Murzea

I'm a backend PHP developer working for Pentalog, where I build awesome applications using the amazing Symfony framework. My current location is Cluj-Napoca (Romania), one of Eastern Europe's main IT outsourcing center. I thrive in challenging environments and am never afraid to get my hands dirty. My passions include software development, psychology and poker.

Updated on May 06, 2020

Comments

  • Radu Murzea
    Radu Murzea about 4 years

    I'm currently developing a program in Java where a certain event must be triggered only when the user clicks with both the left and the right click on a button.

    Since it is a little unconventional, I decided to first test this. Here it is:

    import javax.swing.JFrame;
    import javax.swing.JButton;
    import javax.swing.JLabel;
    import java.awt.event.MouseListener;
    import java.awt.event.MouseEvent;
    
    public class GUI
    {
        private JFrame mainframe;
        private JButton thebutton;
    
        private boolean left_is_pressed;
        private boolean right_is_pressed;
    
        private JLabel notifier;
    
        public GUI ()
        {
            thebutton = new JButton ("Double Press Me");
            addListen ();
            thebutton.setBounds (20, 20, 150, 40);
    
            notifier = new JLabel (" ");
            notifier.setBounds (20, 100, 170, 20);
    
            mainframe = new JFrame ("Double Mouse Tester");
            mainframe.setDefaultCloseOperation (JFrame.DISPOSE_ON_CLOSE);
            mainframe.setResizable (false);
            mainframe.setSize (400, 250);
    
            mainframe.setLayout (null);
    
            mainframe.add (thebutton);
            mainframe.add (notifier);
    
            mainframe.setVisible (true);
    
            left_is_pressed = right_is_pressed = false;
        }
    
        private void addListen ()
        {
            thebutton.addMouseListener (new MouseListener ()
            {
                @Override public void mouseClicked (MouseEvent e) { }
                @Override public void mouseEntered (MouseEvent e) { }
                @Override public void mouseExited (MouseEvent e) { }
    
                @Override public void mousePressed (MouseEvent e)
                {
                    //If left button pressed
                    if (e.getButton () == MouseEvent.BUTTON1)
                    {
                        //Set that it is pressed
                        left_is_pressed = true;
    
                        if (right_is_pressed)
                        {
                            //Write that both are pressed
                            notifier.setText ("Both pressed");
                        }
    
                    }
                    //If right button pressed
                    else if (e.getButton () == MouseEvent.BUTTON3)
                    {
                        //Set that it is pressed
                        right_is_pressed = true;
    
                        if (left_is_pressed)
                        {
                            //Write that both are pressed
                            notifier.setText ("Both pressed");
                        }
                    }
                }
    
                @Override public void mouseReleased (MouseEvent e)
                {
                    //If left button is released
                    if (e.getButton () == MouseEvent.BUTTON1)
                    {
                        //Set that it is not pressed
                        left_is_pressed = false;
    
                        //Remove notification
                        notifier.setText (" ");
                    }
                    //If right button is released
                    else if (e.getButton () == MouseEvent.BUTTON3)
                    {
                        //Set that it is not pressed
                        right_is_pressed = false;
    
                        //Remove notification
                        notifier.setText (" ");
                    }
                }
            });
        }
    }
    

    I tested it and it works, but there is a problem.

    As you can see, the left mouse button is represented by MouseEvent.BUTTON1 and the right mouse button by MouseEvent.BUTTON3.

    If the user has a mouse which doesn't have a scroll wheel (apparently such mice still exist), then only two buttons are set in MouseEvent. Does that mean that the right button will be represented by MouseEvent.BUTTON2 instead of MouseEvent.BUTTON3? If yes, how can I change my code to accomodate this? Is there any way I can detect something like this?

    I read anything I could find on the MouseListener interface and on MouseEvent, but I couldn't find something about this.

    • Radu Murzea
      Radu Murzea over 12 years
      @PetarMinchev this wouldn't be an issue if I was the only user... but I will publish my program online so many people will probably use it (or at least try it).
    • Ingo
      Ingo over 12 years
      There are 3 button mice without scroll wheel.
    • Loduwijk
      Loduwijk over 9 years
      And mice with scroll wheel with only 2 buttons.
  • Radu Murzea
    Radu Murzea over 12 years
    @mKorbel hmmm nice, didn't know these methods existed. So with this I can check if the mouse has a wheel. But what about my other question ? (if there is no wheel, should I check for MouseEvent.BUTTON2 instead of MouseEvent.BUTTON3 ?)
  • mKorbel
    mKorbel over 12 years
    I think that no then (PS2 / Server Console) mouse have got two buttons LEFT & RIGHT
  • texclayton
    texclayton almost 12 years
    Be aware that isMiddleMouseButton will return TRUE if the user Alt-clicks with the Left button. Also, isRightMouseButton will return TRUE if the user Ctrl-clicks with the Left button.
  • maaartinus
    maaartinus over 10 years
    @texclayton: So you're saying that the methods are pretty unusable if you need multiple buttons with modifiers... any workaround?
  • salbeira
    salbeira over 10 years
    These method's implementation just checks weather or not the button mask (1<<1)(1<<2)(1<<3) anded with the mods is unequal to 0 - or basically if the getButton() method == BUTTON1/2/3
  • mKorbel
    mKorbel over 10 years
    @salbeira Mouse can has 5 buttons, don't to use numbers as constants in Swing
  • Mordan
    Mordan over 6 years
    those methods are unreliable. I had a bug because of them. When you press the right mouse button and the left mouse button together.. the left mouse button is detected as the right button!!! This is on Windows Java 8.
  • mKorbel
    mKorbel over 6 years
    @Mordan please to create an SSCCE / MCVE, ask the question with this description, add the link to this thread, answer, maybe bug, maybe feature
  • tay10r
    tay10r about 4 years
    The only downside to this is that release events return false for this function, so it's not valid in that context.