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

I'm sorry if this is fairly basic, but I'm new and desperate for help!
I'm trying to use Perl and Win32-GuiTest to manipulate another application. For the most part, I can get by doing rudimentary keystrokes. However, in a couple of steps in the program, I need to leftclick in a menubox.
I used GetCursorPos() to find the coordinates (or at least I think I did?), then used those coordinates with SendMouse but did not achieve the desired result.
Am I doing something wrong? Any suggestions?
use Win32::GuiTest qw(GetCursorPos SendKeys SendMouse);
system("start programname.exe");
sleep 3;
SendKeys("{ENTER}{TAB 2}{ENTER}");
SendKeys("{TAB 6}");
SendMouse("{ABS125,221}");
SendMouse( "{LeftClick}");

Replies are listed 'Best First'.
Re: Mouseclick in Application
by BrowserUk (Patriarch) on Oct 20, 2010 at 22:54 UTC

    Let me say up front. It will be very difficult to help you with this as we cannot see or test the application you are using. And even if we could, we probably have different resolution screens etc. which makes comparing absolute or relative coordinates impossible.

    That said, Are you sure you want SendMouse("{ABS125,221}");? There are couple of reasons why this may be wrong:

    • ABS means "absolute coordinates". Ie. relative to the screen, not the application window.

      Windows and dialogs tend to pop-up in different positions on the screen each time they run. You might find that you need to use {RELx,y}--and adjust the numbers to suite--in order to account for this.

    • Windows tends to use Device Independent Pixels(DIPs) (also sometimes known as "Window Coordinates") for mouse messages.

      If the tool you are using to measure the position, is operating in display pixel units, that might be why you are being unsuccessful. You might need to use MouseMoveAbsPix() to account for this.

      (But that doesn't allow you to compensate for variable window/dialog positions!)

    Another possibility that might work for you, is to avoid using SendMouse() at all. Most mouse actions in most GUIs have a keyboard shortcut that is "position independant". Using them, where available, can make automation much simpler.

    You appear to have taken great pains to manoeuvre the input caret to the control or field of interest. Having done so, you will often find that hitting the spacebar will have the same affect as clicking with the mouse. Hence replacing

    SendMouse('{ABSx,y}'); SendMouse('LeftClick');

    with the simpler, position independent

    SendKey('SPC');

    Might achieve the desired affect.

    Another useful one is replacing SendMouse('{RightClick'}; with SendKey('APP'}; to invoke context menus.


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
      Thank you for your response! Lucky for me, my window opens in the same spot every time (I ran through it several times to make sure). Using the spacebar in lieu of a mouse click did not work in my situation - but MouseMoveAbsPix() did the trick. I deeply appreciate you taking the time to answer so thoroughly even though I couldn't give you the actual application I am using.
      Keep up the great work!