Detecting Changes in Cursor
Hi everybody!
I am using the robot class to move the cursor across the screen. Is it possible for my java program to detect a change in cursor shape or color as the cursor moves across an open window where the non-java program controlling that window is the source of the change to the cursor?
If it is possible...how do you do it? My research so far tells me that it cannot be done.
Re: Detecting Changes in Cursor
Well, if the Java program has focus then you could always have a thread running in the background that gets the cursor position every so often, say once a second, and performs some action when it sees a change in position. You can use the following at any time in order to determine the cursors position.
Code:
PointerInfo a = MouseInfo.getPointerInfo();
Point b = a.getLocation();
int x = (int)b.getX();
int y = (int)b.getY();
This thread running in the background could have some class variables that hold the current position and then you could compare the X and Y values to see if it has moved.
Re: Detecting Changes in Cursor
AFAIK You can't detect changes in the cursor shape or colour once it is outside of a Java Window, unless you query the OS.
Re: Detecting Changes in Cursor
Thank you very much for the replies to my question. I guess I just have one final question: Is it possible for a java program to query the OS regarding the cursor color or shape? If so, how is it done?
Re: Detecting Changes in Cursor
Yes you can query the OS but it's not straightforward. You need to find out what system calls are available to you and then you can call them via a wrapper using JNI.
There are plenty of tutorials on using JNI, but if your really lucky someone may have already had and solved your problem and posted the code so I'd advise a good search before writing it yourself.