Getting Mouse Location on Screen

I have seen many people asking how you can get the mouse location on screen. Before Java SE 6, a MouseEvent can only tell you where the mouse is in coordinates relative to the component that issued the event. Even though you can combine the MouseEvent.getLocation() and Component.getLocationOnScreen() to discover the mouse location in screen coordinates, you are better off using java.awt.MouseInfo. Just invoke getPointerInfo() to receive an instance of java.awt.PointerInfo that will give you not only the location you were looking for but also the screen device on which the mouse cursor is. Extremely valuable to have high quality dual-screen (or more) support.

6 Responses to “Getting Mouse Location on Screen”

  1. Gareth says:

    ty very much :-)

  2. Ender Demirkaya says:

    MouseEvents occurs when mouse on the component, is there any way to get the mouse location when the mouse is outside the component? Or one step further; if there is no component on the screen, is it possible to create mouseEvent?
    I think I was clear, and any help will be appreciated.

  3. Ender Demirkaya says:

    After a few exploration I found the answer:
    See MouseInfo class. MouseInfo.getPointerInfo().getLocation() retrieves the cursor location on the screen without need of any component.

  4. egasimus says:

    there is MouseEvent.getXOnScreen(), too (:

  5. Romain Guy says:

    It’s exactly what I say at the beginning of my post: “Before Java SE 6, a MouseEvent can only…”

  6. sandeep dhankar says:

    This will only give us the datails about the mouse location and the underlying device. But what if we want the cursor state too , means whther the cursor is default , Hour glass, North-south, east-west, or move cursor??