Mouse Motion Listener
How can i detect when the mose moves when the mouse is not located on the Frame?
Here is my mouse motion listener
privateclass EventHandlerimplements AWTEventListener{
private JPanel myPanel;
public EventHandler(JPanel thepanel){
myPanel = thepanel;
}
publicvoid eventDispatched(AWTEvent event){
if (event.getID() == MouseEvent.MOUSE_MOVED){
MouseEvent me = (MouseEvent)event;
myScreenShooter.setCaptureLocation(me.getX(), me.getY());
myPanel.repaint();
}
}
}
and I register it with a JPanel which I extended
Toolkit.getDefaultToolkit().addAWTEventListener(new EventHandler(this), AWTEvent.MOUSE_MOTION_EVENT_MASK);
But the only time the motion is detected is when the mouse is within the panel...I need to know when it is moved anywhere on the screen.
Any ideas?
Thanks
[1437 byte] By [
Singee15a] at [2007-10-1 13:36:41]

import java.awt.*;
import java.awt.event.*;
import java.text.NumberFormat;
import javax.swing.*;
public class MotionTest
{
public MotionTest()
{
JLabel label = new JLabel();
label.setHorizontalAlignment(JLabel.CENTER);
new Monitor(label);
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().add(label);
f.setSize(300,100);
f.setLocation(200,200);
f.setVisible(true);
}
public static void main(String[] args)
{
new MotionTest();
}
}
class Monitor
{
JLabel label;
Point lastLocation;
boolean continueToMonitor;
NumberFormat nf;
public Monitor(JLabel label)
{
this.label = label;
lastLocation = new Point();
continueToMonitor = true;
nf = NumberFormat.getInstance();
nf.setMaximumFractionDigits(1);
new Thread(runner).start();
}
Runnable runner = new Runnable()
{
public void run()
{
while(continueToMonitor)
{
Point p = MouseInfo.getPointerInfo().getLocation();
double distance = p.distance(lastLocation);
if(distance != 0)
{
Toolkit.getDefaultToolkit().beep();
label.setText("mouse moved " + nf.format(distance));
lastLocation = p;
}
else
label.setText("mouse still");
try
{
Thread.sleep(250);
}
catch(InterruptedException ie)
{
System.err.println("interrupt: " + ie.getMessage());
}
}
}
};
}