如何在鼠标移动之间腾出时间?

时间:2018-02-21 12:24:28

标签: java swing awt mouseevent

我试图获得鼠标移动之间的毫秒时间。我正在使用库来进行鼠标移动事件,以便它可以全局工作。问题是,使用下面的代码,时间总是为零。

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

1 个答案:

答案 0 :(得分:3)

如果您想检测鼠标移动及其属性,请先定义移动:

鼠标移动可以定义为开始将鼠标指针从点 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);
    }
}

在此示例中,我没有公开移动的开始结束点,但是如果您需要,我们的MouseMovementEvent对象会显示这些信息

希望这会有所帮助!