spligak
spligak

Reputation: 207

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

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?

Upvotes: 5

Views: 6580

Answers (5)

Michael Myers
Michael Myers

Reputation: 191875

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

Upvotes: 5

Trucido
Trucido

Reputation: 39

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();
...}

Upvotes: 3

Richard Sanborn
Richard Sanborn

Reputation: 1

There are a few libraries for this, one of which I use on a regular basis for applications.

JNativeHook has exceptional ability to handle both native mouse and keyboard events. (google it I am too lazy to go to the subversion. Although you can just download the library, and it works just like a regular mouse event after you make two calls to the library.

I wish when I was surfing google someone would have posted this library on a thread like this. I use stackoverflow all the time but I'm not a registered member, because I never ask for help publicly.

Upvotes: -4

basszero
basszero

Reputation: 30014

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.

Upvotes: 3

cbrulak
cbrulak

Reputation: 15629

If you want to listen/capture all mouse events on the system (as in, not just your application window), you'll need a mouse hook.

Upvotes: 0

Related Questions