1

Is there any way to get mouse coordinates on a click of desktop screen, i dont want to click inside the java frame, want to click the mouse pointer straight away on desktop and have to know the x,y coordinates ? please help me out? (windows)

Rectangle rectScreenSize = new Rectangle(x1,y1,x2,y2);
BufferedImage biScreen = robot.createScreenCapture (rectScreenSize);

finally want to pass the coordinates for rectangle, to determine the screen size for robot class?

mKorbel
  • 109,525
  • 20
  • 134
  • 319
jcrshankar
  • 1,165
  • 8
  • 25
  • 45
  • *Can you know where I'm clicking on my monitor/Desktop?* The answer is same for your this question. – Harry Joy Dec 16 '11 at 13:30
  • @HarryJoy Do you have a link for that thread? – Andrew Thompson Dec 16 '11 at 13:52
  • I doubt that it's possible since I would think the OS would use a click outside the Java frame to give control to whatever else you click. I don't know for sure and I'll wait to see what others might say. – toto2 Dec 16 '11 at 13:55

1 Answers1

2

You can make a transparent, undecorated JFrame on top of everything, and pass the click on with the Robot class.

By the way, the following does not work outside your own window (I had hoped so):

    Toolkit.getDefaultToolkit().addAWTEventListener(new AWTEventListener() {

        @Override
        public void eventDispatched(AWTEvent event) {
            System.out.println("event: " + event);
            if (event.toString().contains("MOUSE_EXITED")) {
                System.out.println("mouse_exited");
            }
        }
    }, AWTEvent.MOUSE_EVENT_MASK);
Joop Eggen
  • 107,315
  • 7
  • 83
  • 138
  • thanks Eggen... but still here,i hope need to click on frame, to get the coordinates. – jcrshankar Dec 17 '11 at 04:25
  • @jcrshankar um... can you repost that comment with proper English, please? – Ky - Dec 17 '11 at 05:43
  • when i have clicked the mouse inside the frame am getting the x,y value,but when i clicked mouse on the window(out of frame) i cant able get it values.. – jcrshankar Dec 17 '11 at 08:09
  • http://stackoverflow.com/questions/2419555/how-to-obtain-mouse-click-coordinates-outside-my-window-in-java – jcrshankar Dec 17 '11 at 08:10
  • the above link meets my need partially but still am looking for best solution.,this is similar to Eggens response. – jcrshankar Dec 17 '11 at 08:14