Loading ...
Sorry, an error occurred while loading the content.

Semi-automation?

Expand Messages
  • tirian1967
    I m not sure if GuiTest is the right tool for what I m trying to do, so forgive me in advance.... I d like to have a script that manages user input and sends
    Message 1 of 3 , Apr 9, 2003
    • 0 Attachment
      I'm not sure if GuiTest is the right tool for what I'm trying to do,
      so forgive me in advance....

      I'd like to have a script that manages user input and sends
      keypresses to the target app based on that input. To give a simple
      example, I'd like to be able to type "1" into a Perl script and
      have "one" be entered in Notepad, and then enter a 2 and have "two"
      show up. From what I can tell, GuiTest would switch the focus to
      Notepad for the first transaction and I'd get "one2" instead
      of "onetwo".

      Is there a way to send keys to a window that is not active? Failing
      that, how hard is it to switch focus back to the perl script window
      when the SendKeys is completed?

      Thanks,
      Matthew
    • Dennis K. Paulsen
      Hello, Using Notepad and WMSetText instead of SendKeys, a crude example follows: Note: If you actually just had to use the SendKeys function itself instead of
      Message 2 of 3 , Apr 9, 2003
      • 0 Attachment
        Hello,

        Using Notepad and WMSetText instead of SendKeys, a crude example
        follows:

        Note: If you actually just had to use the SendKeys function itself
        instead of WMSetText, you could use IsKeyPressed like below and
        everytime the desired key became depressed you could switch
        foreground/focus to the desired input window, do the SendKeys, then
        revert back ($oldfgwin).
        ---------------

        #!/usr/bin/perl

        use strict;
        use warnings;

        # Tested using Win32::GuiTest v1.49.2 under Windows XP English
        use Win32::GuiTest qw/
        FindWindowLike
        GetForegroundWindow
        IsKeyPressed
        MenuSelect
        SendKeys
        SetForegroundWindow
        WaitWindowLike
        WMSetText
        WMGetText
        /;

        # Get the current foreground window (should be console window for
        Perl script)
        my $oldfgwin = GetForegroundWindow();

        # Start Notepad
        system('start notepad');

        # Wait for the default 'Untitled - Notepad' main window to come up
        my $npmain = WaitWindowLike(0, 'Untitled - Notepad', '');

        # Get Notepad's 'Edit' class child window. Use () because
        FindWindowLike
        # returns an array of windows, so we just worry about the first
        window found.
        my ($npedit) = FindWindowLike($npmain, '', 'Edit');

        # Since starting Notepad makes it become the foreground, revert
        foreground window
        # back to what should be the console window, so we can send it input.
        SetForegroundWindow($oldfgwin);


        print "Press ESC to end this script.\n";

        while (1) {
        # Handle a '1' when pressed
        if (IsKeyPressed('1')) {
        # Wait for key to become unpressed
        while (IsKeyPressed('1')) {
        # NoOp
        }
        # Add to the edit window
        WMAddText($npedit, 'one');
        }
        # Handle a '2' when pressed
        if (IsKeyPressed('2')) {
        # Wait for key to become unpressed
        while (IsKeyPressed('2')) {
        # NoOp
        }
        # Add to the edit window
        WMAddText($npedit, 'two');
        }
        # Allow for script exit
        if (IsKeyPressed('ESC')) {
        # Close Notepad on script exit
        MenuSelect("&File|E&xit", $npmain);
        last;
        }
        # So we don't bog down the system
        select(undef, undef, undef, 0.10);
        }


        # Subroutine: WMAddText
        # Parameters: [WINDOW], [TEXT]
        # Description: Adds [TEXT] into [WINDOW]
        # Returns: undef on error and new window text on success
        sub WMAddText {
        my $win = shift;
        my $text = shift;
        my $temp = '';

        # Handle bad parameters
        if (!$win || !$text) {
        return(undef);
        }

        # Get the current window text
        $temp = WMGetText($win);

        # Add in the new text
        $temp .= $text;

        # Set the new text into the window
        return( WMSetText($win, $temp) );
        }

        ---------------

        Regards,
        Dennis K. Paulsen

        --- In perlguitest@yahoogroups.com, "tirian1967" <mwdaly@f...> wrote:
        > I'm not sure if GuiTest is the right tool for what I'm trying to
        do,
        > so forgive me in advance....
        >
        > I'd like to have a script that manages user input and sends
        > keypresses to the target app based on that input. To give a
        simple
        > example, I'd like to be able to type "1" into a Perl script and
        > have "one" be entered in Notepad, and then enter a 2 and
        have "two"
        > show up. From what I can tell, GuiTest would switch the focus to
        > Notepad for the first transaction and I'd get "one2" instead
        > of "onetwo".
        >
        > Is there a way to send keys to a window that is not active?
        Failing
        > that, how hard is it to switch focus back to the perl script
        window
        > when the SendKeys is completed?
        >
        > Thanks,
        > Matthew
      • tirian1967
        ... [...] WMSetText is just what I needed! Thanks so much. (The IsKeyPressed part didn t work so well on my Win98 system, but that s a much more manageable
        Message 3 of 3 , Apr 12, 2003
        • 0 Attachment
          --- In perlguitest@yahoogroups.com, "Dennis K. Paulsen"
          <ctrondlpaulsden@y...> wrote:
          > Hello,
          >
          > Using Notepad and WMSetText instead of SendKeys, a crude example
          > follows:

          [...]

          WMSetText is just what I needed! Thanks so much. (The IsKeyPressed
          part didn't work so well on my Win98 system, but that's a much more
          manageable challenge for me.)

          FWIW, ActiveState's PPM only has version 1.3 of Win32::GuiTest. You
          may wish to let them know about the latest version.

          -Matthew
        Your message has been successfully submitted and would be delivered to recipients shortly.