• Log in or sign up for an account here: https://www.sythe.org/login

How to get the cursor position in JAVA? (X,Y)

dirtycommando

Newcomer
Joined
Feb 9, 2007
Sythe Gold
0
Messages
8
Location
Santa's Workshop
How to get the cursor position in JAVA? (X,Y)
Hi all,

I have searched all over and simply couldn't find it. I am looking for a way to get the cursor position in X and Y coordinates relative to the screen. I have searched google and sythe for some sort of API but to no avail. I was expecting to find something as simple as getCursorPos(int,int); But i was mistaking...

So what I was wondering is if someone could possibly point me in the right direction or show me some sort of example implementation on how to obtain X and Y screen coordinates.

But I am also wondering at this point, Does an API exist? Or must this be done through JNI?

Thank you =).


-Dirty
 
How to get the cursor position in JAVA? (X,Y)
First you need:

Code:
import java.awt.*

Then use:

Code:
PointerInfo a = MouseInfo.getPointerInfo();
Point b  = a.getLocation();
int x = (int)b.getX();
int y = (int)b.getY();


I think that works, but i have not done any mouse stuff in awhile so i may be mistaken.
 
How to get the cursor position in JAVA? (X,Y)
Hey thank you for your response, but I am getting a symbol not found error on basically every method in the source. Maybe I am just implementing it wrong...Could you show some sample implementation?
 
How to get the cursor position in JAVA? (X,Y)
Code:
import java.awt.event.*;

public class myExample extends Frame implements MouseMotionListener {
	int mouseX=0,mouseY=0;
	myExample() {
		addMouseMotionListener(this);
		reshape(100,100,100,100);
	}
	public void mouseMoved(MouseEvent e) {
		mouseX=e.getX();
		mouseY=e.getY();
		System.out.println("You clicked on "+mouseX+","+mouseY);
	}
	public void mouseEntered(MouseEvent e) {
	}
	public void mouseExited(MouseEvent e) {
	}
	public static void main(String[] args) {
		myExample frame=new myExample();
		frame.setVisible(true);
	}
}

just a basic example. but you should really try google next time!
 
How to get the cursor position in JAVA? (X,Y)
Sorry, I fucked up... the import was wrong... it should be fixxed now... here is an example that constantly prints the position of your mouse (every 1 sec)

Code:
import java.awt.*;

public class GetMouse
{
	public static void main(String[] args)
	{
		while (true)
		{
			PointerInfo a = MouseInfo.getPointerInfo();
			Point b = a.getLocation();
			int x = (int) b.getX();
			int y = (int) b.getY();
			System.out.println(x + ":" + y);
			try
			{
				Thread.sleep(1000);
			}
			catch (InterruptedException e)
			{
				e.printStackTrace();
			}
		}
	}
}
 
Runescape Gamblingrunescape gambling
Back
Top