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();
}
}
}

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();
}
}
}