Which mouse button is in the middle?


Radu Murzea:

I'm currently developing a program in Java where a specific event has to be fired only when the user clicks with the left and right mouse buttons at the same time.

Since this is a bit unconventional, I decided to test it first. here 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 and the MouseEvent.BUTTON1right mouse button is represented by MouseEvent.BUTTON3.

If the user's mouse doesn't have a scroll wheel (apparently such a mouse still exists), only set two buttons in the MouseEvent. Does this mean that the right button will be MouseEvent.BUTTON2replaced by MouseEvent.BUTTON3? If yes, how can I change the code to accommodate this requirement? Is there any way to detect this situation?

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

mKorbel :

To determine which mouse button was pressed, the following three methods from SwingUtilities can help you:

  1. isLeftMouseButton
  2. isMiddleMouseButton
  3. isRightMouseButton

Related


Which mouse button is in the middle?

Radu Murzea: I'm currently developing a program in Java where a specific event has to be fired only when the user clicks with the left and right mouse buttons at the same time. Since this is a bit unconventional, I decided to test it first. here is: import jav

Which mouse button is in the middle?

Radu Murzea: I'm currently developing a program in Java where a specific event has to be fired only when the user clicks with the left and right mouse buttons at the same time. Since this is a bit unconventional, I decided to test it first. here is: import jav

Disable middle mouse button

Tom Hamilton Stubber So I've tried going through various other questions, but they mostly focus on disabling middle mouse button paste. Basically, the middle mouse button on my Logitech G500s is broken and keeps "clicking" randomly, increasing any chance of do

Paste with the middle mouse button

Sriharsha Kalluru Are there any apps that use the middle button to paste the same text every time? I've seen tools to paste clipboard text, but I want the same text to be pasted every time I click the middle mouse button. Karan With AutoHotkey the following ca

Disable middle mouse button

Ethan Hunt I need a question to help you, how to disable middle mouse click on any link to open new tab in IE 7,8,9. I have tried many things like return false; e.cancelBubble = true;e.returnValue = false; But there is no way to stop IE's ability to open a Ne

Paste with the middle mouse button

Sriharsha Kalluru Are there any apps that use the middle button to paste the same text every time? I've seen tools to paste clipboard text, but I want the same text to be pasted every time I click the middle mouse button. Karan With AutoHotkey the following ca

Back button to middle mouse button

Protect I want to map the "back" button (xev means "button 7") of a mouse (A4Tech X-7) as the middle mouse button. Is there a way to do this? I'm lost in Google and only find how to map buttons to keyboard shortcuts using xbindkeys etc. Operating system: Cento

Back button to middle mouse button

Protect I want to map the "back" button (xev means "button 7") of a mouse (A4Tech X-7) as the middle mouse button. Is there a way to do this? I'm lost in Google and only find how to map buttons to keyboard shortcuts using xbindkeys etc. Operating system: Cento

Back button to middle mouse button

Protect I want to map the "back" button (xev means "button 7") of a mouse (A4Tech X-7) as the middle mouse button. Is there a way to do this? I'm lost in Google and only find how to map buttons to keyboard shortcuts using xbindkeys etc. Operating system: Cento

Vaadin Grid middle mouse button

F43nd1r I'm trying to simulate normal browser behavior in a vaadin grid, which includes middle-clicking to open in a new tab: addItemClickListener(e -> { boolean newTab = e.getMouseEventDetails().getButton() == MouseEventDetails.MouseButton.MIDDLE

How to handle the middle mouse button?

Gibbs I have an ember component that generates a set of user groups. When the user clicks on one of the list components, it calls the linkAction defined in that component and takes the user to the specific page associated with that list item. However, when the

Alternative hotkey for middle mouse button

some people A few days ago, my middle mouse button broke and could only scroll. The problem was that I used this button a lot and it was the reason he broke, so I searched for a way to "replace" his function and found out about AutoHotKey. I read many docs and

How to handle the middle mouse button?

Gibbs I have an ember component that generates a set of user groups. When the user clicks on one of the list components, it calls the linkAction defined in that component and takes the user to the specific page associated with that list item. However, when the

Vaadin Grid middle mouse button

F43nd1r I'm trying to simulate normal browser behavior in a vaadin grid, which includes middle-clicking to open in a new tab: addItemClickListener(e -> { boolean newTab = e.getMouseEventDetails().getButton() == MouseEventDetails.MouseButton.MIDDLE

Alternative hotkey for middle mouse button

some people A few days ago, my middle mouse button broke and could only scroll. The problem was that I used this button a lot and it was the reason he broke, so I searched for a way to "replace" his function and found out about AutoHotKey. I read a lot of docs

Firefox middle mouse button scrolling

pixel developer How do I get the middle mouse button to work properly in Ubuntu? Clicking the middle button doesn't show the arrow, which allows me to scroll the page by moving the mouse. information: Firefox 3.6.8 Mouse: Microsoft Intellimouse Explorer 3.0 Is

Firefox middle mouse button scrolling

pixel developer How do I get the middle mouse button to work properly in Ubuntu? Clicking the middle button doesn't show the arrow, which allows me to scroll the page by moving the mouse. information: Firefox 3.6.8 Mouse: Microsoft Intellimouse Explorer 3.0 Is

Emulate middle mouse button with AutoHotKey

Buck I want to write a simple script (single line?) to map left shift + right mouse button to middle mouse button (I don't actually have middle mouse button because it's a trackpad). I tried this variation: #(LShift & RButton)::MButton or (LShift & RButton::M

Alternative hotkey for middle mouse button

some people A few days ago, my middle mouse button broke and could only scroll. The problem was that I used this button a lot and it was the reason he broke, so I searched for a way to "replace" his function and found out about AutoHotKey. I read a lot of docs

Vaadin Grid middle mouse button

F43nd1r I'm trying to simulate normal browser behavior in a vaadin grid, which includes middle-clicking to open in a new tab: addItemClickListener(e -> { boolean newTab = e.getMouseEventDetails().getButton() == MouseEventDetails.MouseButton.MIDDLE

How to handle the middle mouse button?

Gibbs I have an ember component that generates a set of user groups. When the user clicks on one of the list components, it calls the linkAction defined in that component and takes the user to the specific page associated with that list item. However, when the

How to handle the middle mouse button?

Gibbs I have an ember component that generates a set of user groups. When the user clicks on one of the list components, it calls the linkAction defined in that component and takes the user to the specific page associated with that list item. However, when the

Alternative hotkey for middle mouse button

some people A few days ago, my middle mouse button broke and could only scroll. The problem was that I used this button a lot and it was the reason he broke, so I searched for a way to "replace" his function and found out about AutoHotKey. I read a lot of docs

Alternative hotkey for middle mouse button

some people A few days ago, my middle mouse button broke and could only scroll. The problem was that I used this button a lot and it was the reason he broke, so I searched for a way to "replace" his function and found out about AutoHotKey. I read a lot of docs

Alternative hotkey for middle mouse button

some people A few days ago, my middle mouse button broke and could only scroll. The problem was that I used this button a lot and it was the reason he broke, so I searched for a way to "replace" his function and found out about AutoHotKey. I read many docs and

"Middle Mouse Button" pastes where the mouse cursor is

Dennis Kuhn [EDIT] : The observed problem is misleading, as it turns out that it stems from the behavior of Google Doc. See the accepted answer. Is it possible to configure the "middle mouse button"-paste option to paste text not where the text cursor is, but

How to map the middle mouse button for double click?

Kira Ubuntu 18.04. When I click the middle mouse button, it should double click. thanks. Edit: Using the Unity desktop. unknown OS: Ubuntu 18:04 Required packages: xdotoolandxbindkeys you can install these packagessudo apt install xdotool xbindkeys gedit .xbin

tkinter: How to autoscroll with middle mouse button?

哈格(Haggar) Right now, I have an implementation that does two things with the middle mouse button: Scroll the wheel to zoom in and out Roll the wheel button to scroll (pan) left, right, top and bottom. But in this function, I keep pressing the scroll button to

TableView row selection click on middle mouse button

West Dandan Currently in TableView, row selection doesn't happen when we middle click. If we click the right or left mouse button, the row is selected. I'm trying to have a function to select a row when the middle button is clicked. I already know that includi