Mouse position relative to screen

Discussion in 'Java' started by MAB, Aug 17, 2004.

  1. MAB

    MAB Guest

    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
     
    MAB, Aug 17, 2004
    #1
    1. Advertising

  2. MAB

    VisionSet Guest

    "MAB" <> wrote in message
    news:...
    > 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)

    --
    Mike W
     
    VisionSet, Aug 17, 2004
    #2
    1. Advertising

  3. MAB

    VisionSet Guest

    "VisionSet" <> wrote in message
    news:SBnUc.1429$%%...
    > "MAB" <> wrote in message
    > news:...
    > > 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)
    >


    Ooops, sorry must read more carefully!

    --
    Mike W
     
    VisionSet, Aug 17, 2004
    #3
  4. "MAB" <> wrote in message
    news:...
    > 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 http://www.iviz.com/
     
    Matt Humphrey, Aug 17, 2004
    #4
  5. MAB

    Paul Lutus Guest

    MAB wrote:

    > 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.

    --
    Paul Lutus
    http://www.arachnoid.com
     
    Paul Lutus, Aug 17, 2004
    #5
  6. "MAB" <> writes:

    > 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. :)
     
    Tor Iver Wilhelmsen, Aug 17, 2004
    #6
  7. MAB

    MAB Guest

    "Tor Iver Wilhelmsen" <> wrote in message
    news:...
    > "MAB" <> writes:
    >
    > > 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. :)


    First I want to know where it is :)
     
    MAB, Aug 18, 2004
    #7
  8. MAB

    MAB Guest

    "Paul Lutus" <> wrote in message
    news:...
    > MAB wrote:
    >
    > > 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.
    >


    I want the absolute position of the mouse on screen. How would creating a
    visual component help?
     
    MAB, Aug 18, 2004
    #8
  9. MAB

    Paul Lutus Guest

    MAB wrote:

    >
    > "Paul Lutus" <> wrote in message
    > news:...
    >> MAB wrote:
    >>
    >> > 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.
    >>

    >
    > 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.

    --
    Paul Lutus
    http://www.arachnoid.com
     
    Paul Lutus, Aug 18, 2004
    #9
  10. MAB

    MAB Guest

    "Paul Lutus" <> wrote in message
    news:...
    > MAB wrote:
    >
    > >
    > > "Paul Lutus" <> wrote in message
    > > news:...
    > >> MAB wrote:
    > >>
    > >> > 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.
    > >>

    > >
    > > 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.
    >
    > --
    > Paul Lutus
    > http://www.arachnoid.com



    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.
     
    MAB, Aug 18, 2004
    #10
  11. MAB

    KC Wong Guest

    > 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...
     
    KC Wong, Aug 18, 2004
    #11
  12. MAB

    Adam Guest

    "MAB" <> wrote in message
    news:...
    > 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
     
    Adam, Aug 18, 2004
    #12
  13. MAB

    Paul Lutus Guest

    MAB wrote:

    >> > 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.

    >
    > 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.

    --
    Paul Lutus
    http://www.arachnoid.com
     
    Paul Lutus, Aug 18, 2004
    #13
  14. MAB

    Paul Lutus Guest

    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.

    --
    Paul Lutus
    http://www.arachnoid.com
     
    Paul Lutus, Aug 18, 2004
    #14
  15. MAB

    MAB Guest

    "Paul Lutus" <> wrote in message
    news:...
    > MAB wrote:
    >
    > >> > 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.

    > >
    > > 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.
    >


    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 )
     
    MAB, Aug 18, 2004
    #15
  16. MAB

    MAB Guest

    "Paul Lutus" <> wrote in message
    news:...
    > 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.
    >


    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.
     
    MAB, Aug 18, 2004
    #16
  17. MAB

    Paul Lutus Guest

    MAB wrote:

    >
    > "Paul Lutus" <> wrote in message
    > news:...
    >> MAB wrote:
    >>
    >> >> > I want the absolute position of the mouse on screen. How would

    > creating
    >> > a
    >> >> > visual component help?


    [ clearly a question about READING the mouse cursor position ]

    [ essential text snipped here by MAB ]

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


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

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


    [ an abrupt change of topic from READING the mouse cursor potion to SETTING
    the mouse cursor position ]

    >>
    >> 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.
    >>

    >
    > 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:

    >> How do I get the position of the mouse relative to
    >> the top left of the screen.


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

    Your later post:

    >> 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


    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.

    --
    Paul Lutus
    http://www.arachnoid.com
     
    Paul Lutus, Aug 18, 2004
    #17
  18. MAB

    Paul Lutus Guest

    MAB wrote:

    >
    > "Paul Lutus" <> wrote in message
    > news:...
    >> 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.
    >>

    >
    > I'm not confusing anything.


    You most certainly are. Your original request:

    >> How do I get the position of the mouse relative to
    >> the top left of the screen.


    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.

    --
    Paul Lutus
    http://www.arachnoid.com
     
    Paul Lutus, Aug 18, 2004
    #18
  19. MAB

    Carl Howells Guest

    MAB wrote:

    > 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.
     
    Carl Howells, Aug 18, 2004
    #19
  20. Adam <> coughed up the
    following:
    > "MAB" <> wrote in message
    > news:...
    >> 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.


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


    --
    It'salwaysbeenmygoalinlifetocreateasignaturethatendedwiththeword"blarphoogy"
    ..
     
    Thomas G. Marshall, Aug 18, 2004
    #20
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Max
    Replies:
    7
    Views:
    9,182
  2. tom arnall
    Replies:
    6
    Views:
    1,304
    Ian Shef
    Jan 18, 2007
  3. Amir Kouchekinia

    Zoom relative to mouse position

    Amir Kouchekinia, May 20, 2008, in forum: Java
    Replies:
    6
    Views:
    1,934
    John B. Matthews
    May 23, 2008
  4. madmike

    iframe relative mouse position

    madmike, Jul 16, 2003, in forum: Javascript
    Replies:
    2
    Views:
    172
    madmike
    Jul 17, 2003
  5. Nikolai Prokoschenko

    Mouse position in both fixed and relative positioning

    Nikolai Prokoschenko, May 17, 2011, in forum: Javascript
    Replies:
    2
    Views:
    1,116
    darwinist
    May 18, 2011
Loading...

Share This Page