Java swing: how to detect when the mouse is at rest
I wrote 开发者_StackOverflow社区some code that has a jpanel with a mouse listener and mouse motion listener, and it is working fine, but I run want some animations when the mouse is not moving. how can this be detected? Is there a way to slang some mouse events for a stationary mouse?
You can detect that the mouse is not moving by the fact that you are not getting motion events :-). Just use that.
You could e.g. set a timer that is reset every time a motion event fires. If the timer expires without a motion event (e.g. after 1s), you can start your animation until a motion event occurs.
As far as I know, you can however only get motion events while your app has focus. I believe this is by design, and I don't know whether you can get motion events without focus (that could arguably be considered a security vulnerability, as you could spy on what the user is doing).
精彩评论