Java mouse pressed - no events

is there a way in Java to directly check if one of the mouse buttons is pressed without using events, listeners, etc.? I would like to have a thread that checks every 100 milliseconds if the mouse button is clicked and then does something. Therefore, if the user holds the mouse button for a while, he calls several answers.

So, I am looking for a method that gives the state of the mouse without going through the usual event processing system.

thanks

+5
source share
4 answers

I believe this is not possible in Java. Well, it is possible through JNI, but it is a world of pain.

, . 100 :

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

public class Test {

    public static void main(String[] args) {
        JFrame frame = new JFrame();
        final JLabel label = new JLabel("Click on me and hold the mouse button down");
        label.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
        frame.getContentPane().add(label);
        label.addMouseListener(new TimingMouseAdapter());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    private static class TimingMouseAdapter extends MouseAdapter {
        private Timer timer;

        public void mousePressed(MouseEvent e) {
            timer = new Timer(100, new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    System.out.println("Mouse still pressed...");
                }
            });
            timer.start();
        }

        public void mouseReleased(MouseEvent e) {
            if (timer != null) {
                timer.stop();
            }
        }

    }
}

, (, ) , .

+4

, - , .

, . 100 .

+4

, , , ?

, , , . 100 - . .

, , , , , . .

, , , .

+2

Listener . . Global Event Listeners AWTEventListener, .

.

0

All Articles