Sending keystrokes to GUI applications

Ask a Question related to Ruby, Design and Development.

  1. #1

    Default Sending keystrokes to GUI applications

    I'm trying to automate a Windows GUI application that has no command
    line interface and no OLE Automation interface. I only need to send a
    few keystrokes to this application. I thought Ruby would be great for
    this job but I've searched extensively and can't find any information on
    how to do it. Below is an example in Java of what I'd like to do
    (Notepad is not the application I'm trying to automate).

    Can anyone provide any suggestions or code snippets showing how to send
    keystrokes to a Windows GUI application. Any help would be much
    appreciated.

    Regards,
    Ron


    import java.awt.AWTException;
    import java.awt.Robot;
    import java.awt.event.KeyEvent;
    import java.io.IOException;

    public class Automate {
    public static void main(String[] args) {
    try {
    Runtime.getRuntime().exec( "notepad.exe");
    Robot robot = new Robot();
    robot.keyPress(KeyEvent.VK_A);
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (AWTException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }
    }


    Ron Coutts Guest

  2. Similar Questions and Discussions

    1. Keystrokes without wmode=window
      I work for a company that hosts rich-media internet ads. It is a requirement that our flash content have a wmode of either opaque or transparent...
    2. Missing Keystrokes
      I built application in flex 2 and I have a problem when users run it under IE7 on vista(Flash Player version 9.0.28.0). When a user types is some...
    3. Marker Navigation Keystrokes
      Hi all, Another question for you..... I am currently using a simple script to navigate back and forward between markers using the keys 'X' and...
    4. KeyUp - recognising keystrokes?
      Hi - I want to use keyboard input instead of the mouse for a Director project. Does anyone know how to do that? I intend to use the arrow keys...
    5. MIAW and capturing keystrokes
      Hi I'm still playing with my wordwheel. Basically I have it pop up in its own MIAW when somone presses a button. Then, I have an "On KeyDown" in...
  3. #2

    Default Re: Sending keystrokes to GUI applications

    After some more digging at Microsoft's web site, it seems I've answered
    my own question. The following snippet will start Notepad and send it
    some keys.

    Ron

    require 'win32ole'

    wsh = WIN32OLE.new('WScript.Shell')
    wsh.Run("Notepad.exe")
    while not wsh.AppActivate("Notepad")
    sleep .1
    end
    wsh.SendKeys("This is a test.")

    > -----Original Message-----
    > From: Ron Coutts [mailto:rcoutts@envistatech.com]
    > Sent: November 11, 2003 10:15 AM
    > To: ruby-talk ML
    > Subject: Sending keystrokes to GUI applications
    >
    >
    > I'm trying to automate a Windows GUI application that has no command
    > line interface and no OLE Automation interface. I only need to send a
    > few keystrokes to this application. I thought Ruby would be great for
    > this job but I've searched extensively and can't find any
    > information on
    > how to do it. Below is an example in Java of what I'd like to do
    > (Notepad is not the application I'm trying to automate).
    >
    > Can anyone provide any suggestions or code snippets showing
    > how to send
    > keystrokes to a Windows GUI application. Any help would be much
    > appreciated.
    >
    > Regards,
    > Ron
    >
    >
    > import java.awt.AWTException;
    > import java.awt.Robot;
    > import java.awt.event.KeyEvent;
    > import java.io.IOException;
    >
    > public class Automate {
    > public static void main(String[] args) {
    > try {
    > Runtime.getRuntime().exec( "notepad.exe");
    > Robot robot = new Robot();
    > robot.keyPress(KeyEvent.VK_A);
    > } catch (IOException e) {
    > // TODO Auto-generated catch block
    > e.printStackTrace();
    > } catch (AWTException e) {
    > // TODO Auto-generated catch block
    > e.printStackTrace();
    > }
    > }
    > }
    >
    >

    Ron Coutts Guest

Posting Permissions

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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139