How to get the position of current mouse pointer in Java

Asked By 0 points N/A Posted on -
qa-featured

I am trying to run an application in Java for a small project. As a start, i have to identify the position of the mouse pointer. how to detect the X location and Y location (X,Y) of the mouse pointer in Java ? This project will be a sub project later for a major project on which my team is working on. The user need not move the mouse, still the location of the mouse has to be identified and sent as a parameter to say a function Current_mouse_loc(). How to get the mouse pointer position? Which is the call back responsible for this? Thank you for replying.

SHARE
Answered By 590495 points N/A #194256

How to get the position of current mouse pointer in Java

qa-featured

If you want to get the mouse position using JavaScript, you can use the MouseEvent property called clientX. It returns the horizontal coordinate of the mouse pointer based on the client area when a mouse event is triggered. The client area refers to the current window. Alternatively, to get the vertical coordinate of the mouse pointer based on the client area, you can use the clientY property.

Note that both of these MouseEvent properties are read-only. The clientX and clientY properties are supported on the following web browsers: Google Chrome, Windows Internet Explorer, Mozilla Firefox, Safari, and Opera. The syntax to use the clientX property is “event.clientX”. See the example below.

var x = event.clientX;   // Get the horizontal coordinate and store it in the variable x
var y = event.clientY;   // Get the vertical coordinate and store it in the variable y
var coor = "X coords: " + x + ", Y coords: " + y;

When executed, the result of the variable coor might be:

X coords: 230, Y coords: 123

Related Questions