Reputation: 17829
essentially I want to run a method whenever the user goes from not moving their mouse at all, to moving it. I have no clue how to go about this.
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
public class CustomMouseListener implements MouseMotionListener, MouseListener{
//whatever other methods I have (irrelevant for the question)
public void mouseMoved(MouseEvent e){
//code goes here
//but right now it fires every time the mouse is moved, which
//is way too much, I only need one per move
}
}
Upvotes: 1
Views: 349
Reputation: 347194
If you're only interested in knowing two things, when the mouse started to move and when the mouse stopped moving, you could use a javax.swing.Timer
to insert a delay between events, so that it will only be tiggered when the delay is reached...
public class CustomMouseListener implements MouseMotionListener, MouseListener{
private javax.swing.Timer moveTimer;
private boolean moving = false;
public CustomMouseListener() {
moveTimer = new javax.swing.Timer(25, new ActionListener() {
public void actionPerformed(ActionEvent evt) {
moving = false;
// Method to be called when you want to
// to know when the mouse has stopped moving...
}
});
moveTimer.setRepeats(false);
}
//whatever other methods I have (irrelevant for the question)
public void mouseMoved(MouseEvent e){
if (moving || moveTimer.isRunning()) {
moveTimer.restart();
} else {
moving = true;
moveTimer.start();
// Method to call when you want to know when the mouse
// has started moving...
}
}
}
Basically, when 25 milliseconds pass without mouseMoved
being called, the javax.swing.Timer
will be triggered....You may want to play with the threshold a little...
Upvotes: 1
Reputation: 4598
One way to do it would be save the last time it moved. If current time - lastMovedTime > x then call your listeners or mouseStartedMoving() method
public class CustomMouseListener implements MouseMotionListener, MouseListener{ public final static long TIME_DIFFERNCE_FOR_IDLE = 800;//milliseconds long lastMoveTime = -1;
public void mouseMoved(MouseEvent e){
long currentTime = System.currentTimeMillis();
long diff = lastMoveTime - currentTime ;
if(lastMoveTime == -1 || diff > TIME_DIFFERNCE_FOR_IDLE ){
lastMoveTime();
}
lastMoveTime = System.currentTimeMillis();
}
}
void lastMoveTime(){
//do what u need to when mouse starts mooving
}
Another Need to adda polling thread. Can make a default thread pool of size 1. ADd one task (Runnable) in the run method sleep for 1 second (or 2 or 800 milliseconds - depends what you define as pause between moves)
Anyway in your original code keep track of the current mouse position x,y and expose to the runnable.
The runnable keeps track of previous mouse position. Also have a state variable in Runnable that is an enum - {INITIAL, STATIONARY, MOVING}.
Initially its INITIAL, if you get mouse move position and its INITIAL it goes to MOVING
IF MOVING and for X ticks in the Runnable it does not move goes to STATIONARY. Again on Mouse move it goes to MOVING.
When it goes from INITIAL to MOVING OR STATIONARY to MOVING, can have listeners who are called or just a special method - mouseStartedMoving()
And do whatever there.
Upvotes: 1
Reputation: 5762
Plain algorithm
0. Fire mouse listener for every second with (x=x1, y=y1)
1. Store (x,y) of mouse pointer;
2. If (x,y) == (x1,y1) for another 15(or whatever you consider as time interval) sec
3. make account of (x1,y1);
4. Else do nothing;
5. If(x1,y1) changed after 15 sec
6. call mouseMove();
Upvotes: 3