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
James Goodwin’s code will not work. mousePressed and mouseReleased are both fired from the GUI thread, so blocking in mousePressed will prevent mouseReleased from ever being fired, meaning the loop will continue forever.
If you already have a seperate thread for processing then use mousePressed to indicate to that thread that the event should start, and mouseReleased to stop.
If you don’t have a separate thread and don’t want the hassle, a Timer is probably the easiest way. javadoc on Timer.
Specifically, you should create a TimerTask that does whatever it is you want to do multiple times and queue it using Timer.schedule:
I’m pretty sure this is the simplest way, as it doesn’t involve faffing around with inter-thread communication.
Oh, and as Peter said, you will have to add code to account for the user mousing down on your app and mousing up somewhere else.