0

Possible Duplicate:
how to obtain mouse click coordinates outside my window in Java

I basically need to find out the location of the mouse pointer. i've tried this:

package main;

import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;

public class MouseListen implements MouseMotionListener{

    public void mouseDragged(MouseEvent arg0) {     
    }

    public void mouseMoved(MouseEvent arg0) {
        mouseX = arg0.getX();
        mouseY = arg0.getY();
    }

   public int mouseX, mouseY;

} 

Now that only works when inside the JFrame window. How could I possible find the x and y position of the mouse outiside the JFrame?

Community
  • 1
  • 1
Shaun Wild
  • 1,237
  • 3
  • 17
  • 34
  • If you knew how to do that in C, C++ or Assembly, might be [JNI - Java Native Interface](http://en.wikipedia.org/wiki/Java_Native_Interface) can help you in this :-) – nIcE cOw Apr 05 '12 at 05:12
  • This is easy to do with the help of the end user. Just get them to click on a point in a screen shot. As shown [here](http://stackoverflow.com/a/6092439/418556). – Andrew Thompson Apr 05 '12 at 06:39

2 Answers2

3
MouseInfo.getPointerInfo().getLocation().x;
MouseInfo.getPointerInfo().getLocation().y;
Reg
  • 10,717
  • 6
  • 37
  • 54
1

You want the X and Y coordinates of mouse pointer on screen? Try using MouseEvent.getXOnScreen() and MouseEvent.getYOnScreen() (Since Java 1.6)

Raze
  • 2,175
  • 14
  • 30