I would like to simulate a natural mouse movement in Java (going from here to there pixel by pixel). To do that I need to know the starting coordinates.
I've found the method event.getX() and event.getY() but I need an event...
How can I know the positions without doing anything (or something not visible)?
Thank you
MouseInfo.getPointerInfo().getLocation() might be helpful. It returns a Point object corresponding to current mouse position.
In SWT you need not be in a listener to get at the mouse location. The Display object has the method
getCursorLocation()
.In vanilla SWT/JFace, call
Display.getCurrent().getCursorLocation()
.In an RCP application, call
PlatformUI.getWorkbench().getDisplay().getCursorLocation()
.For SWT applications, it is preferable to use
getCursorLocation()
over theMouseInfo.getPointerInfo()
that others have mentioned, as the latter is implemented in the AWT toolkit that SWT was designed to replace.If you're using Swing as your UI layer, you can use a Mouse-Motion Listener for this.
In my scenario, I was supposed to open a dialog box in the mouse position based on a GUI operation done with the mouse. The following code worked for me:
I am doing something like this to get mouse coordinates using Robot, I use these coordinates further in few of the games I am developing: