My boilerplate listener:
class MyMouseMotionListener implements MouseMotionListener {
public void mouseDragged(MouseEvent e) {
System.out.println("Dragged...");
}
public void mouseMoved(MouseEvent e) {
System.out.println("Moved...");
}}
Simple enough, but what do I add it to in order to listen to system-wide events? I've been researching are things like the GraphicsDevice and AccessibleContext subclasses -- they don't offer the addition of MouseMotionListeners directly but I was hoping they might give me some idea as to how I could implement this.
Edit: This isn't at all event-based but I've found this:
MouseInfo.getPointerInfo().getLocation()
Does actually return the mouse position outside the context of my app, even when the app itself does not have focus. Is there any way to observe this and dispatch an event if its value has changed?
What are the differences between a MouseListener and a MouseMotionListener in Java? We can implement a MouseListener interface when the mouse is stable while handling the mouse event whereas we can implement a MouseMotionListener interface when the mouse is in motion while handling the mouse event.
Move the cursor into the yellow rectangle at the top of the window. You will see one or more mouse-moved events. Press and hold the mouse button, and then move the mouse so that the cursor is outside the yellow rectangle. You will see mouse-dragged events.
The Java MouseMotionListener is notified whenever you move or drag mouse. It is notified against MouseEvent. The MouseMotionListener interface is found in java. awt.
You can subscribe to all mouse events within your Java container hierarchy using Toolkit.addAWTEventListener(AWTEventListener listener, long eventMask)
. The eventMask
parameter determines which events the listener will receive.
So your code would look something like :
Toolkit.getDefaultToolkit().addAWTEventListener(new MyMouseMotionListener(), AWTEvent.MOUSE_MOTION_EVENT_MASK);
UPDATE: You could poll MouseInfo for position but you'll never get button state. You will need to use native code to get button state.
I do not think there is any way without using native code to listen to the mouse cursor outside of the cotainer hierarchy of your application.
I solved the same issue by using the above mentioned ability to get the mouse position upon request. I then launched a new thread to do this continuously durring the rest of the programs execution.
MouseInfo.getPointerInfo().getLocation()
Also I had to make the main class extend Thread thus
public class MouseMotion extends Thread {
This requies you to make a function called run. In your void function simply create an infinite loop
public void run() {
int n=10;
for (int i=0;i<n; n++) //horrible infinite loop
{
Thread.sleep(100); //this will slow the capture rate to 0.1 seconds
PointerInfo a = MouseInfo.getPointerInfo();
Point p = new Point (0,0);
a = MouseInfo.getPointerInfo();
p = a.getLocation();
int x = (int)p.getX(); //getX and getY return doubles so typecast
int y = (int)p.getY();
System.out.println(""+x+" "+y); //to see it grabing locations not needed
}
}
All that is left now is to call the thread when you wana start watching your mouse motion. I start the thread right after my main as such
public static main (String[] args) throws Exception {
Thread thread = new MouseMotion();
thread.start();
...}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With