Results 1 to 7 of 7

Thread: Get the Global Mouse Position

  1. #1
    Join Date
    Jul 2007
    Beans
    41

    Get the Global Mouse Position

    Hi,

    is there a simple API-Function that can retrieve the mouse position on the whole screen? I tried an SDL-Function but it only works within an SDL-Window, but I need the Mouse Position for any window.

  2. #2
    Join Date
    Aug 2007
    Location
    Canada
    Beans
    459
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: Get the Global Mouse Position

    Best, Gnusci

    "Never make a calculation until you know the answer." -- Wheeler, Spacetime Physics, pg 60.

  3. #3
    Join Date
    Jul 2007
    Beans
    41

    Re: Get the Global Mouse Position

    Will allegro create new dependencies for my program (in case I want to run it on a foreign machine, will it require allegro to be installed)?

    Still, there must be a straight-forward way!

  4. #4
    Join Date
    Mar 2006
    Location
    Eefde, The Netherlands
    Beans
    432
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: Get the Global Mouse Position

    Quote Originally Posted by Kenobi View Post
    Will allegro create new dependencies for my program (in case I want to run it on a foreign machine, will it require allegro to be installed)?
    Yes, unless you statically link Allegro with your binary which will probably lead to a rather large executable.

    Still, there must be a straight-forward way!
    Perhaps you could look into Xlib? XQueryPointer() seems to do what you want.
    There's a fairly large chance people have Xlib installed when they use a mouse.
    "When in doubt, use brute force."

    -- Ken Thompson

  5. #5
    Join Date
    Aug 2007
    Location
    Canada
    Beans
    459
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: Get the Global Mouse Position

    So use Xlib I also think is good enough if you just only need the mouse coordinates:

    http://users.actcom.co.il/~choo/lupg...ogramming.html
    Best, Gnusci

    "Never make a calculation until you know the answer." -- Wheeler, Spacetime Physics, pg 60.

  6. #6
    Join Date
    Jul 2007
    Beans
    41

    Re: Get the Global Mouse Position

    I found that XLib function. It returns quite a useful bunch of things, but it needs a *display pointer. I tried to create one by using XOpenDisplay, but then XQueryPointer hangs up the program. Is there a way to get the *display of the actual screen I'm looking on?

    I don't understand why they have to make it that hard. Also I think there could be a GTK-way to get the Mouse Coordinates?

  7. #7
    Join Date
    Jan 2008
    Beans
    1

    Re: Get the Global Mouse Position

    Here ya go:


    Display *dsp = XOpenDisplay( NULL );
    if( !dsp ){ return 1; }

    int screenNumber = DefaultScreen(dsp);

    XEvent event;

    /* get info about current pointer position */
    XQueryPointer(dsp, RootWindow(dsp, DefaultScreen(dsp)),
    &event.xbutton.root, &event.xbutton.window,
    &event.xbutton.x_root, &event.xbutton.y_root,
    &event.xbutton.x, &event.xbutton.y,
    &event.xbutton.state);

    printf("Mouse Coordinates: %d %d\n", event.xbutton.x, event.xbutton.y);

    XCloseDisplay( dsp );

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •