java swing hold both mouse buttons
Asked Answered
M

2

7

I want to implement a method where the user needs to hold the left and right mouse buttons at the same time.

I'm using Swing and Java 1.7. I've tried this, but it doesn't detect the both-buttons case like I'd expect it to:

public void mousePressed(MouseEvent e) {    
     if (SwingUtilities.isLeftMouseButton(e) && SwingUtilities.isRightMouseButton(e)){
              ///code here
     }
}

i tried to separate methods and use bool values to decide if the mouse button is pressed and then i set a condition to find out if both of them are pressed at the same time , but that didint work out too ..

Matthei answered 11/4, 2013 at 19:21 Comment(2)
i tried to separate methods and use bool values to decide if the mouse button is pressed and then i set a condition to find out if both of them are pressed at the same time , but that didint work out too .. - Sounds like a reasonable approach. Post your SSCCE that shows what you tried.Milburr
thank you very much for you interest ... but the code postat below works , it was almost the same, but the decistion if the mouse is pressed was based on if (SwingUtilities.isLeftMouseButton(e)) { left = true } and that didnt work ... strange :DPhantasm
F
6

This is an SSCCE that does what you want... i.e. if I understood your question correctly.

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class StackOverflow15957076 extends MouseAdapter
{
    private JLabel status;

    private boolean isLeftPressed;
    private boolean isRightPressed;

    public StackOverflow15957076 ()
    {
        JFrame frame = new JFrame ();
        frame.setDefaultCloseOperation (JFrame.DISPOSE_ON_CLOSE);

        JPanel panel = new JPanel (new FlowLayout (FlowLayout.CENTER));

        status = new JLabel ("waiting for both mouse buttons...");

        status.addMouseListener (this);

        panel.add (status);

        frame.add (panel);

        frame.pack ();
        frame.setVisible (true);

        isLeftPressed = false;
        isRightPressed = false;
    }

    @Override
    public void mousePressed (MouseEvent e)
    {
        if (SwingUtilities.isLeftMouseButton (e))
        {
            isLeftPressed = true;
        }
        else if (SwingUtilities.isRightMouseButton (e))
        {
            isRightPressed = true;
        }

        if (isLeftPressed && isRightPressed)
        {
            status.setText ("both buttons are pressed");
        }
    }

    @Override
    public void mouseReleased (MouseEvent e)
    {
        if (SwingUtilities.isLeftMouseButton (e))
        {
            isLeftPressed = false;
        }
        else if (SwingUtilities.isRightMouseButton (e))
        {
            isRightPressed = false;
        }

        status.setText ("waiting for both mouse buttons...");
    }

    public static void main (String[] args)
    {
        SwingUtilities.invokeLater (new Runnable ()
        {
            @Override
            public void run ()
            {
                new StackOverflow15957076 ();
            }
        });
    }
}
Foresheet answered 11/4, 2013 at 19:32 Comment(6)
I would have upvoted but you used MouseEvent.BUTTON1/BUTTON3 instead of the SwingUtilities.isXXX methods (as used by the OP) which are easier to understand.Milburr
@Milburr The methods in SwingUtilities are a nicer abstraction, but I think it's the same functionality... You use which-ever you think it's best :) .Foresheet
@Radu Murzea can you please to edit your question and to add the same code with two if(SwingUtilities.is..., works correctly, common issue all multiple key (mouse) events should be tested separatellyChambray
SwingUtilities are a nicer abstraction - that's the idea, "isLeftButton" is easier to understand than "BUTTON1". There is no guessing what the intent of the code is. The API is full of higher level abstractions. We are continually advising people in the forum to use these abstractions when possible.Milburr
@Milburr and mKorbel: OK, I edited my answer and changed those conditions.Foresheet
@Radu Murzea I think that you'll k*** me, you can to post two codes separete code, both are correct, both are excelent answers !!! :-)Chambray
E
1

It seems that it's not possible do it directly, since mouse events are fired sequentially. See, for example, this SO question/answers.

So you will need to decide what "at the same time" actually means to you (i.e. how close in time thay should be). Then you can capture two separate events and compare their getWhen() values.

Expertism answered 11/4, 2013 at 19:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.