如何获得鼠标移动之间的时间?

How to get time between mouse moves?

我正在尝试获取鼠标移动之间的毫秒时间。我正在为鼠标移动事件使用一个库,以便它在全球范围内工作。问题是下面的代码时间总是零。

        @Override
        public void mouseMoved(GlobalMouseEvent event)
        {
            //times the mouse has moved
            moveCount++;

            //if it's the first time moving mouse then it's the start time
            if (moveCount % 2 != 0)
            {
                startTime = System.currentTimeMillis();
            }

            //if it's the second time moving the mouse then it's the end 
            //time
            if (moveCount % 2 == 0)
            {
                long endTime = System.currentTimeMillis();
                long time = (endTime - startTime);

                System.out.println(time);
            }
        }

如果你想检测鼠标移动及其属性,让我们先定义移动:

一次鼠标移动可以定义为开始将鼠标指针从A点移动到B点,然后鼠标指针在 B 点停留 超过 阈值 以将其检测为运动结束。

如果我们像上面那样定义问题,我们可以通过在鼠标开始移动时启动一个计时器然后在鼠标移动时重新启动该计时器来解决它。然后,如果计时器在 阈值 之后超时,我们可以假设这是之前开始的运动的结束。那么鼠标指针从A点到达B点所经过的时间就是该次移动的时间。

为了以适当的方式编写上述想法,我们可以按以下形式定义我们的 MouseMovementListenerMouseMovementEvent 和其他实用程序 类:

我们的自定义事件:

import java.awt.Point;

public class MouseMovementEvent {
    private Point startPoint;
    private Point endPoint;
    private Long  startNanoTime;
    private Long  endNanoTime;

    public MouseMovementEvent(Point startPoint, Point endPoint, Long startNanoTime, Long endNanoTime) {
        this.startPoint = startPoint;
        this.endPoint = endPoint;
        this.startNanoTime = startNanoTime;
        this.endNanoTime = endNanoTime;
    }

    public Point getStartPoint() {
        return startPoint;
    }
    public Point getEndPoint() {
        return endPoint;
    }
    public Long getStartNanoTime() {
        return startNanoTime;
    }
    public Long getEndNanoTime() {
        return endNanoTime;
    }       
    public Long getMotionDurationInNanos() {
        return endNanoTime - startNanoTime;
    }
}

侦听器接口:

public interface MouseMovementListener {
    void movementOccured(MouseMovementEvent e);
}

然后是监听鼠标移动的代码,将其转化为移动事件:

import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;

import javax.swing.JComponent;
import javax.swing.Timer;

public class MouseMovementMonitor {

    private Point startPoint;
    private Point currentPoint;
    private Long  startTime;
    private Timer timer;

    public MouseMovementMonitor(JComponent componentToMonitor, int motionTimeDelay, final MouseMovementListener movementListener) {
        timer = new Timer(motionTimeDelay, new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {

                if(startPoint != null && !startPoint.equals(currentPoint)) {
                    movementListener.movementOccured(
                            new MouseMovementEvent(startPoint, currentPoint, startTime, System.nanoTime())
                        );
                }

                startPoint = null; // either can be 
                startTime  = null;

                // or reset process can be as follow instead of the two above lines: 
                // startPoint = currentPoint;
                // startTime  = System.nanoTime();

                timer.stop();
            }

        });

        componentToMonitor.addMouseMotionListener(new MouseMotionAdapter() {

            @Override
            public void mouseMoved(MouseEvent e) {
                if(startPoint == null) {
                    startPoint = e.getPoint();
                    startTime  = System.nanoTime();
                }
                currentPoint = e.getPoint();
                //
                if(timer.isRunning())
                    timer.restart();
                else
                    timer.start();
            }
        });
    }
}

最后测试一下:

import java.awt.BorderLayout;
import java.awt.Color;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.WindowConstants;

public class Test {
    public static void main(String[] args) {
        JPanel p = new JPanel();
        p.setBackground(Color.PINK);

        new MouseMovementMonitor(p, 500, new MouseMovementListener() {
            @Override
            public void movementOccured(MouseMovementEvent e) {
                System.out.println("Movement Duration in nanos: " + e.getMotionDurationInNanos());              
            }
        });

        JFrame f = new JFrame();
        f.setBounds(200,200,200,200);
        f.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
        f.setLayout(new BorderLayout());
        f.add(p, BorderLayout.CENTER);
        f.setVisible(true);
    }
}

在这个例子中,我没有公开移动的 startend 点,但是我们的 MouseMovementEvent 对象有如果您需要这些信息。

希望这会有所帮助!