Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to have a MouseMotionListener listen to all system mouse motion events?

Tags:

java

swing

awt

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?

like image 875
spligak Avatar asked Feb 02 '09 19:02

spligak


People also ask

What is the difference between mouse listener and mouse motion listener?

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.

How do you use a mouse motion listener?

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.

Which of the following mouse events is handled by the interface MouseMotionListener?

The Java MouseMotionListener is notified whenever you move or drag mouse. It is notified against MouseEvent. The MouseMotionListener interface is found in java. awt.


3 Answers

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);

like image 152
Michael Myers Avatar answered Oct 06 '22 06:10

Michael Myers


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.

like image 28
basszero Avatar answered Oct 06 '22 06:10

basszero


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();
...}
like image 43
Trucido Avatar answered Oct 06 '22 04:10

Trucido