简体   繁体   English

java swing同时拥有两个鼠标按钮

[英]java swing hold both mouse buttons

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. 我正在使用Swing和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 .. 我试图分离方法并使用bool值来决定是否按下了鼠标按钮,然后我设置了一个条件,以确定它们是否同时被按下,但是这样做也很有效。

This is an SSCCE that does what you want... ie if I understood your question correctly. 这是一个SSCCE做你想要的...即如果我正确理解你的问题。

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 ();
            }
        });
    }
}

It seems that it's not possible do it directly, since mouse events are fired sequentially. 它似乎不可能直接执行,因为鼠标事件是按顺序触发的。 See, for example, this SO question/answers . 例如,请参阅此SO问题/答案

So you will need to decide what " at the same time " actually means to you (ie how close in time thay should be). 因此,您需要确定“ 同时 ”对您实际意味着什么(即应该在多长时间内接近)。 Then you can capture two separate events and compare their getWhen() values. 然后,您可以捕获两个单独的事件并比较它们的getWhen()值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM