Mouse position relative to screen

M

MAB

How do I get the position of the mouse relative to the top left of the
screen. The MouseMotionListener works relative to some component of the app
but what if I have no visual component and I want the absolute position of
the mouse?

thx
 
V

VisionSet

MAB said:
How do I get the position of the mouse relative to the top left of the
screen. The MouseMotionListener works relative to some component of the app
but what if I have no visual component and I want the absolute position of
the mouse?

SwingUtilities.convertPointToScreen(Point p, Component c)
 
M

Matt Humphrey

MAB said:
How do I get the position of the mouse relative to the top left of the
screen. The MouseMotionListener works relative to some component of the app
but what if I have no visual component and I want the absolute position of
the mouse?

How are you getting mouse events if they're not in the context of some
Component? Are you really asking how to poll the position of the cursor on
the screen? Check out the full-screen mode--that will give you a Graphics
Context that covers the entire screen.

Cheers,
Matt Humphrey (e-mail address removed) http://www.iviz.com/
 
P

Paul Lutus

MAB said:
How do I get the position of the mouse relative to the top left of the
screen. The MouseMotionListener works relative to some component of the
app but what if I have no visual component and I want the absolute
position of the mouse?

Basically, what the other posters are telling you is, to get the mouse
position without a visual component, first create a visual component.
 
T

Tor Iver Wilhelmsen

MAB said:
The MouseMotionListener works relative to some component of the app
but what if I have no visual component and I want the absolute
position of the mouse?

Use java.awt.Robot to force it to be where you want it to be. :)
 
M

MAB

Paul Lutus said:
Basically, what the other posters are telling you is, to get the mouse
position without a visual component, first create a visual component.

I want the absolute position of the mouse on screen. How would creating a
visual component help?
 
P

Paul Lutus

MAB said:
I want the absolute position of the mouse on screen. How would creating a
visual component help?

For your answer, post your commmand-line (non-GUI) application that provides
the mouse's position on the screen.
 
M

MAB

Paul Lutus said:
For your answer, post your commmand-line (non-GUI) application that provides
the mouse's position on the screen.


I wrote a program (with no interface) that is able to click anywhere on the
screen repeatedly thus serving the needs I expressed in the previous thread
i.e. to click the OK button of the message that pops up every minute in the
game (actually my 3 year old kid plays the game and i'm doing this for him
and for myself (learning java)). The location (x,y) and the time interval
(d) between each click is supplied thru command line arguments. Here is the
program

import java.awt.* ;
import java.util.* ;

public final class ClickMouse extends TimerTask {
public static int x, y, d ;

public static void main(String[] args) {

TimerTask clikMouse = new ClickMouse();
Timer t = new Timer();

x = Integer.parseInt(args[0]) ;
y = Integer.parseInt(args[1]) ;
d = Integer.parseInt(args[2]) ;

t.schedule(clikMouse,1000,d*1000);
}

public void run() {

try {
Robot bot = new Robot();

bot.mouseMove(x,y);
bot.mousePress(java.awt.event.InputEvent.BUTTON1_MASK );
bot.mouseRelease(java.awt.event.InputEvent.BUTTON1_MASK);
}
catch (Exception e)
{
System.out.println("Exception occured :" + e.getMessage());
}

}


Now ideally I would like the program to end when my kid is finished with the
game so that the mouse is released and he can play any other game he wants.
The best way that comes to my mind (any other ideas are welcome but I just
dont want to make him click the mouse or press any key) is that when the
mouse is moved more than certain distance from the point (x,y) the program
should end i.e to release the mouse just move it far enough. For this my
program needs to determine the mouse position every time before clicking at
x,y ( I could put a flag to make sure it doesnt quit the first time it
clicks etc). So here is how things will go

1. My son starts the (keyboard based) game and this java app at the same
time
2. Every time the message box of the game pops up it will be closed by my
program within 2 seconds ( the position of the OK button is always the same
so my app would start always with the same arguments e.g javaw ClickMouse
"400" "500" "2"
I've checked this functionality and it works, now only the last part. When
he moves the mouse (which he will naturally) I want the program to end thus
releasing the mouse. for this, the app needs to know the absolute position
of the mouse so that it can compare it with x,y or just the relative
distance from x,y.
 
K

KC Wong

1. My son starts the (keyboard based) game and this java app at the same
time
2. Every time the message box of the game pops up it will be closed by my
program within 2 seconds ( the position of the OK button is always the same
so my app would start always with the same arguments e.g javaw ClickMouse
"400" "500" "2"
I've checked this functionality and it works, now only the last part. When
he moves the mouse (which he will naturally) I want the program to end thus
releasing the mouse. for this, the app needs to know the absolute position
of the mouse so that it can compare it with x,y or just the relative
distance from x,y.

Check out this thread I found by Googling "Java mouse absolute position":
(Pls concat the long link if broken)
http://groups.google.com/groups?hl=...&selm=385F336B.D8443128%40fh-aachen.de&rnum=5

It has the answer - but only if the program has any GUI components. In your
case, your application has no GUI - thus no component to catch the mouse
event.

I'm watching this thread, waiting for an answer too...
 
A

Adam

MAB said:
How do I get the position of the mouse relative to the top left of the
screen. The MouseMotionListener works relative to some component of the app
but what if I have no visual component and I want the absolute position of
the mouse?

AFAIK MouseEvents are generated by AWT,
when Event Dispatch Thread is running.
I don't know a way of starting the EDT
without having any visual component created.

When I was researching this issue - I failed
to get a solution (capturing mouse events
in a non-GUI app).

So my answer would be : no way in pure Java
to capture mouse events happening outside of
a Java visual components.

Probably could be written in C++, using WinAPI
and then used in Java with JNI.

Adam
 
P

Paul Lutus

MAB said:
I wrote a program (with no interface) that is able to click anywhere on
the screen repeatedly

And you have just left the stated topic, which is to locate the mouse
cursor's current position on the screen, a passive monitoring function.
Your application puts the mouse cursor at a determined location, it doesn't
read its current position.
thus serving the needs I expressed in the previous
thread i.e. to click the OK button

A total departure from this thread's topic. Here is the first line of the
first post:
How do I get the position of the mouse relative to the top left of the
screen

It is really a good idea to read a thread before posting to it.
 
P

Paul Lutus

KC Wong wrote:

/ ...
I'm watching this thread, waiting for an answer too...

The answer is that you cannot read the mouse's position without a visual
component. The poster identified as MAB is confusing writing a mouse
position with reading it.
 
M

MAB

Paul Lutus said:
And you have just left the stated topic, which is to locate the mouse
cursor's current position on the screen, a passive monitoring function.
Your application puts the mouse cursor at a determined location, it doesn't
read its current position.


A total departure from this thread's topic. Here is the first line of the
first post:


It is really a good idea to read a thread before posting to it.

its even a better idea to read a post before replying to it. Read the full
post. I explained what I had accomplished and what I wanted to do now and
why I wanted to do it ( locate the mouse position )
 
M

MAB

Paul Lutus said:
KC Wong wrote:

/ ...


The answer is that you cannot read the mouse's position without a visual
component. The poster identified as MAB is confusing writing a mouse
position with reading it.

I'm not confusing anything. I wrote a program that could write a mouse
position and now I want to add the capability to read also. Make it a habit
to read a post carefully before replying to it.
 
P

Paul Lutus

[ clearly a question about READING the mouse cursor position ]

[ essential text snipped here by MAB ]

[ a challenge to READ the mouse cursor position without a visual component ]

[ an abrupt change of topic from READING the mouse cursor potion to SETTING
the mouse cursor position ]
its even a better idea to read a post before replying to it. Read the full
post. I explained what I had accomplished and what I wanted to do now and
why I wanted to do it ( locate the mouse position )

You clearly cannot grasp the meaning of your own posts.

The original request:

This is a request for a method to READ the mouse cursor position.

Your later post:

This is a statement that one can SET the mouse cursor position.

This is not the present topic.

Again, for those readers who might now be getting confused, one cannot read
the mouse cursor position without creating a visual component.
 
P

Paul Lutus

MAB said:
I'm not confusing anything.

You most certainly are. Your original request:

This is very clearly a request to READ the mouse cursor position. We replied
that you cannot do this without creating a visual component. You disagreed
with this statement. We then asked that you post yoru code that can READ
the mouse cursor position without a visual component. You replied that you
ccould WRITE the mouse cursor position without a visual component, thus
confirming your total confusion.

Here it is again:
I wrote a program that could write a mouse
position and now I want to add the capability to read also.

As we have repeatedly pointed out, you need to have a visual component to do
this.
Make it a
habit to read a post carefully before replying to it.

It is your turn to try to read and understand the thread you started.
 
C

Carl Howells

MAB said:
its even a better idea to read a post before replying to it. Read the full
post. I explained what I had accomplished and what I wanted to do now and
why I wanted to do it ( locate the mouse position )

Don't bother arguing with Paul. It's pointless. Once he's made up his
mind, no amount of evidence, logic, or yelling can change it.

But, his advice is usually correct. And while he's doing more yelling
at you than pointing out the issue, his advice is correct in this case,
too. There's no API to get the mouse position without creating a GUI
component. So, you're going to have to create a GUI component.

And if you're doing that, you might as well just put a "Quit" button on
it, and forget about reading the mouse position.
 
T

Thomas G. Marshall

Adam said:
AFAIK MouseEvents are generated by AWT,
when Event Dispatch Thread is running.
I don't know a way of starting the EDT
without having any visual component created.

When I was researching this issue - I failed
to get a solution (capturing mouse events
in a non-GUI app).

So my answer would be : no way in pure Java
to capture mouse events happening outside of
a Java visual components.

Probably could be written in C++, using WinAPI
and then used in Java with JNI.

So long as you keep it to a method call(s). JNI doesn't let you peer into
C++ objects.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top