Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Java, what event is *continuously* fired on mouse button down?

I would like to know if there exists in Java an event that is continuously fired when a mouse button is being held down (pressed), even if the mouse is not moved. I could not find it in typical MouseListeners:

  • MouseDragged is fired only if the user move the mouse while holding the button down
  • MousePressed is fired only once when the button has been pressed

And that's about it. Any idea how to make such an event?

Cheers

jy

like image 296
Jean-Yves Avatar asked Dec 30 '09 10:12

Jean-Yves


1 Answers

There is an obvious reason why this event is not available in MouseListener: it's going to spam you with events such that everything is slowed down to a halt. Do you want to receive this event every second, every ms, or even more often? If you need that you'll have to do it yourself.

For stearing this process you of course need mousePressed and mouseReleased to determine whether a button is currently held down. Then you need to run some kind of loop that generates the corresponding events you'd like to have.

You might also want to work via polling, i.e. extend your MouseListener class such that it can tell you whether a button is still held down, and whereever you need those events you can actively poll for the button. It depends on how you want to use these events, which approach is more suitable.

like image 71
Frank Avatar answered Sep 30 '22 06:09

Frank