Get Mouse Position
Solution 1:
MouseInfo.getPointerInfo().getLocation() might be helpful. It returns a Point object corresponding to current mouse position.
Solution 2:
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);
Solution 3:
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.
Solution 4:
import java.awt.MouseInfo;
import java.util.concurrent.TimeUnit;
public class Cords {
public static void main(String[] args) throws InterruptedException {
//get cords of mouse code, outputs to console every 1/2 second
//make sure to import and include the "throws in the main method"
while(true == true)
{
TimeUnit.SECONDS.sleep(1/2);
double mouseX = MouseInfo.getPointerInfo().getLocation().getX();
double mouseY = MouseInfo.getPointerInfo().getLocation().getY();
System.out.println("X:" + mouseX);
System.out.println("Y:" + mouseY);
//make sure to import
}
}
}