Get Mouse Position
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.
PointerInfo a = MouseInfo.getPointerInfo();
Point b = a.getLocation();
int x = (int) b.getX();
int y = (int) b.getY();
System.out.print(y + "jjjjjjjjj");
System.out.print(x);
Robot r = new Robot();
r.mouseMove(x, y - 50);
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 the MouseInfo.getPointerInfo()
that others have mentioned, as the latter is implemented in the AWT toolkit that SWT was designed to replace.
上一篇: 你如何获得textarea中的光标位置?
下一篇: 获取鼠标位置