I'm trying to get the millisecond time between times when the mouse moves. I'm using a library for the mouse move event so that it works globally. The problem is that with the code below the time is always zero.
@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);
}
}
If you want to detect a mouse movement and it's properties let's define the movement first:
A mouse movement can be defined as starting to move the mouse pointer from point A to point B and then the mouse pointer stay at point B for more than a threshold to detect it as the end of the movement.
If we define the problem like the above, we can solve it by starting a timer as the mouse starts to move and then restart that timer while the mouse is moving. Then if the timer timed out after a threshold we can assume that as an end to that movement started before. Then the time elapsed in which mouse pointer reached from point A to B is the time of that movement.
To code the above idea in a decent way we can define our MouseMovementListener
, MouseMovementEvent
and other utility classes in the following form:
Our custom event:
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;
}
}
The listener interface:
public interface MouseMovementListener {
void movementOccured(MouseMovementEvent e);
}
Then the code to monitor mouse moves and translate it to Movement Event:
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();
}
});
}
}
And finally testing it:
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);
}
}
In this example I didn't expose the start and end points of the movement but our MouseMovementEvent
object has those information if you want them.
Hope this would be helpful!