开发者

Java detect long mouse press

Is there any way to capture an event if a user press on JList component for more than 3 seco开发者_运维百科nds?

The difficult part i find is the event needs to be trigger even before the user let go of the mouse left button. (which can be easily done by couple mousePressed and mouseReleased)


Heres what i use for similiar situations:

public class Player implements Runnable, MouseListener
{
    public void mousePressed(MouseEvent e)
    {
        holding = true;
        thread = new Thread(this);
        thread.start();
    }

    public void mouseReleased(MouseEvent e)
    {
        holding = false;
        System.out.println("Held for: "+seconds);
    }
    public void mouseClicked(MouseEvent e){}

    public void run()
    {
        try
        {
            while(holding)
            {
                seconds++;
                // put some code here
                if(seconds==3)
                {
                    holding = false;
                    System.out.println("Held for maximum time!");
                }
            }
        }catch(Exception e){e.printStackTrace();}

    private boolean holding;
    private int seconds;
    private Thread thread;
}

add this to your JLabel by calling label.addMouseListener(new Player());


You can set a timer in your mouseDown event listener and execute it every 500 ms after an initial delay of 3000 ms. In your mouseReleased you can cancel that timer. On the run method of the TimerTask object associated with your Timer you can perform the calculation of task you want. Here is my solution proposal:

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

public class Test
{
    public static void main(String[] args)
    {
        final JFrame f = new JFrame();
        String[] data = {"one", "two", "three", "four"};
        JList myList = new JList(data);
        f.add(myList);
        myList.addMouseListener(
            new MouseAdapter()
            {
                private java.util.Timer t;
                public void mousePressed(MouseEvent e)
                {
                    if(t == null)
                    {
                        t = new java.util.Timer();
                    }
                    t.schedule(new TimerTask()
                    {
                        public void run()
                        {
                            System.out.println("My importan task goes here");
                        }
                    },3000,500);
                }

                public void mouseReleased(MouseEvent e)
                {
                    if(t != null)
                    {
                        t.cancel();
                        t = null;
                    }
                }
            }
            );
            f.pack();
            SwingUtilities.invokeLater(new Runnable()
            {
                public void run()
                {
                    f.setVisible(true);
                }
            }
        );
    }
}


Two different solutions (nearly the same as the previous ones) :

    new MouseAdapter() {
        Date pressedTime;
        long timeClicked;

        @Override
        public void mousePressed(MouseEvent e) {
            pressedTime = new Date();
        }
        @Override
        public void mouseReleased(MouseEvent e) {
            timeClicked = new Date().getTime() - pressedTime.getTime();
            if (timeClicked >= 3000) {
                // DO YOUR ACTION HERE
            }
        }
    };

or

new MouseAdapter() {
    boolean mousePressed = false;

    @Override
    public void mousePressed(MouseEvent e) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                mousePressed = true;
                while (mousePressed) {
                    try {
                        Thread.sleep(3000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    if (mousePressed) {
                        // DO YOUR ACTION HERE
                    }
                }
            }
        }).start();
    }

    @Override
    public void mouseReleased(MouseEvent e) {
        mousePressed = false;
    }
}


I have an idea. Its not exactly a listener but what you can do is on mousePressed you can make a timer start. When the timer reaches 3 seconds, your event starts or if they let go, on mouseReleased the the timer stops.


With this code you can detect and manage pressed long or short (like click), one event excludes another.

private int pressStatus = 0; // TO DETECT IF LONG IS REAL LONG OR SHORT PRESSED
private Timer t;

...

@Override
public void mousePressed(final MouseEvent arg0) {
    pressStatus = 0; // to manage simple click or long

    if (t == null) {
        t = new Timer();
    }
    t.schedule(new TimerTask() {
        public void run() {
            pressStatus = 1;

            if (t != null) {
                t.cancel();
                t = null;
            }

            // PRESSED LONG
            int modifiers = arg0.getModifiers();
            if ((modifiers & InputEvent.BUTTON3_MASK) == InputEvent.BUTTON3_MASK) {
                ...
            } else if ((modifiers & InputEvent.BUTTON1_MASK) == InputEvent.BUTTON1_MASK) {
                ....
            }
        }
    }, 700, 1);
}

@Override
public void mouseReleased(MouseEvent arg0) {
    if (t != null) {
        t.cancel();
        t = null;
    }

    // PRESSED SHORT LIKE CLICK
    if (pressStatus == 0) {
        int modifiers = arg0.getModifiers();
        if ((modifiers & InputEvent.BUTTON3_MASK) == InputEvent.BUTTON3_MASK) {
            ...
        } else if ((modifiers & InputEvent.BUTTON1_MASK) == InputEvent.BUTTON1_MASK) {
            ...
        }
    }

    pressStatus = 0;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜