Search code examples
javaawtrobot

How to register MousemotionListener


How to Register MouseMotionListener without using Applet,JFrame,JPanel or anything.Because i want to capture mouse positions when mouse is just moved in a System?

import java.awt.event.MouseEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.*;
import java.awt.*; 
class Mouseposition extends MouseAdapter
{
public void mouseMoved(MouseEvent e)
{
System.out.println("MOuse x : "+MouseInfo.getPointerInfo().getLocation().x+ "Mouse Y : "+MouseInfo.getPointerInfo().getLocation().y);
}
public static void main(String args[])
{
//--- register for Mouse events
----
while(true);
}
}

Solution

  • You can't, Java won't listen to global OS events in this way.

    You could use a JNI/JNA hook into the OS, for example or example or use a Thread to constantly poll the MouseInfo class, for example and example, which probably isn't very efficient...