Is it possible for MouseMotionListener to listen for all mouse movement events in the system?

My template 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 why add it to listen to system-wide events? I learn things like the GraphicsDevice and AccessibleContext subclasses - they don't offer the addition of MouseMotionListeners directly, but I was hoping they could give me some idea of โ€‹โ€‹how I could implement this.

Edit: This is not an event at all, but I found this:

 MouseInfo.getPointerInfo().getLocation() 

Actually returns the mouse position outside the context of my application, even if the application has no focus. Is there a way to watch this and send an event if its value has changed?

+5
source share
5 answers

You can subscribe to all mouse events in the Java container hierarchy using Toolkit.addAWTEventListener(AWTEventListener listener, long eventMask) . The eventMask parameter determines which events the listener will receive.

So your code will look something like this:

Toolkit.getDefaultToolkit().addAWTEventListener(new MyMouseMotionListener(), AWTEvent.MOUSE_MOTION_EVENT_MASK );

+5
source

UPDATE: you can poll MouseInfo for a position, but you will never get the state of a button. You will need to use your own code to get the state of the button.

I donโ€™t think there is any way without using native code to listen to the mouse cursor outside the cotainer hierarchy of your application.

+3
source

I solved the same problem using the above option to get the mouse position on request. Then I started a new thread to do this continuously, doing the rest of the program execution.

MouseInfo.getPointerInfo (). getLocation ()

I also had to make a main class to continue Thread in this way

 public class MouseMotion extends Thread { 

This requires that you create a function called run. In your void function, just 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 remains now is to trigger a stream when you start to watch the movement of the mouse. I start the topic right after my main

 public static main (String[] args) throws Exception { Thread thread = new MouseMotion(); thread.start(); ...} 
+2
source

If you want to listen / capture all mouse events in the system (for example, not only in the window of your application), you will need a mouse hook .

0
source

There are several libraries for this, one of which I regularly use for applications.

JNativeHook has the exceptional ability to handle both its own mice and keyboard events. (google is too lazy for me to go on disruptive activities. Although you can just load the library and it works just like a normal mouse event after you make two calls to the library.

I would like, when I was surfing on google, someone would put this library in a stream like this. I use stackoverflow all the time, but I am not a registered member because I never ask for help in public.

-4
source

Source: https://habr.com/ru/post/901333/


All Articles