Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I have been scratching my head on how to get the mouse position in the desktop. Does anyone have an idea on how to do this?

Replies are listed 'Best First'.
Re: Mouse Position in Desktop
by GrandFather (Saint) on Dec 27, 2010 at 23:41 UTC

    What OS and for what reason?

    Using Win32::GuiTest you can use GetCursorPos() to return the current mouse cursor position under Windows.

    True laziness is hard work
      Thank you. It is indeed in Windows. I tried it and it went well. But if I wanted to move my mouse and show its current position at the same time, how would you do it?

      The code below seems to work for me, but it seems that the script is not fast enough to capture the mouse position.
      while (1) { my ($x, $y) = GetCursorPos(); print "X is at $x, Y is at $y\n"; }

        What's the application? print will slow your loop down lot, but even without the I/O it may well be that Perl is not suitable for fast enough response for interactive tasks.

        True laziness is hard work
        You need to yield to other processes by sleeping some amount of time each loop. Time::HiRes's version of sleep will help here.