1

When handling any MouseEvent it is easy to get the mouse coordinates by invoking methods such as event.getSceneX() (or event.getScreenX() if that is what we want). Hovewer, I have no idea how to get this information outside the mouse event handling procedure, if there are no mouse events currently?

In my case: after pressing a certain hotkey, I want something to happen relative to the current mouse position. But I am unable to acquire the position information. How should I do it?

Radosław Łazarz
  • 950
  • 1
  • 11
  • 25
  • This question is similar to [How to get location of mouse in JavaFX?](http://stackoverflow.com/questions/16635514/how-to-get-location-of-mouse-in-javafx) – jewelsea Aug 06 '15 at 17:12

1 Answers1

-1

Ok, I found at least one solution, utilising java.awt.MouseInfo, here is the outline of the concept:

if (view.isHover()) {
    Point pointerLocation = MouseInfo.getPointerInfo().getLocation();

    int sceneX = pointerLocation.x;
    sceneX -= view.getScene().getWindow().getX();
    sceneX -= view.getScene().getX();

    int sceneY = pointerLocation.y;
    sceneY -= view.getScene().getWindow().getY();
    sceneY -= view.getScene().getY();

    return new Point(sceneX, sceneY);
} else {
    // mouse not in view
}
Radosław Łazarz
  • 950
  • 1
  • 11
  • 25