SendKeys()? meh. how about SendFood() or SendMoney()?

April 1, 2006 under Computers, Programming, Ruby, SendKeys, VBScript

At work last week, it so happened that I had a very tedious manual task to perform. Due to an NDA, I can’t say which server application this task involved. Let’s just say that there isn’t an easy way to import lots values for a picklist field. I had to add 245 values from an Excel worksheet to 3 picklist fields – that would require me to switch to Excel, select the value, copy, switch to the server application, click “Add”, paste, click “OK” 735 times! There is apparently a way to do this by writing a .NET assembly, but I was assured that finding the neccessary info to accomplish this would take longer that actually doing the manual task. And that’s when the thought of my wrists in firey pain from repetive stress foced me to recall and old friend of mine from shell32.dll – the SendKeys() method.

SendKeys() does what it sounds like…it sends keystrokes. Plain and simple. For that task, I knew what keystrokes I needed to do. I could’ve written something in C# or VB.NET, since there’s a SendKeys() method in the System.Windows.Forms namespace, but time was not on my side. There’s nothing like the cling and static-free scripting languages to save the day 🙂 So using the Windows Scripting Host and 15 minutes of my time, I wrote some JScript code (as an aside, it appears that Microsoft is downplaying VBScript more and more all the time) that read all of the values from the Excel worksheet, stored them, and then switched back to the server application’s window to add values in the manual process. I could then take my hands off of the keyboard and watch my “ghost writer” do the work, saving me plenty of time and preventing a repetitive stress injury 🙂

SendKeys() seems old-school to me, but comes in handy when an application you’re working with doesn’t have any of its functionality exposed via COM or .NET or whatever. Even if it does, sometimes it’s quicker to send keystrokes than read API documentation. As a cheesy example, let’s fire up MSCONFIG and automatically switch to the “Startup” tab to see what’s launching when Windows boots:

// Let's get to the good stuff in shell32.dll.
var objWSHShell = WScript.CreateObject("WScript.Shell");
 
// Launch MSCONFIG.
objWSHShell.Run("msconfig");
 
// Wait a couple of seconds for MSCONFIG to launch.
WScript.Sleep(2000);
 
// Switch the focus to MSCONFIG.
objWSHShell.AppActivate("System Configuration Utility");
 
// Keystroke time!
// SHIFT+TAB sets the keyboard focus to the row of tabs.
objWSHShell.SendKeys("+{TAB}");
 
// Hit the right cursor key 5 times to get to the Startup tab.
for (var i = 0; i < 5; i++)
    objWSHShell.SendKeys("{RIGHT}");

Pretty simple, and you don’t need the Windows Scripting Host to use SendKeys() from a scripting language. You can use non-Microsoft languages too. For example, the scripting language that has my attention lately is Ruby – it’s like Python on steroids. This script will do the same thing as the one above, but this time, I’ll write it in Ruby (without comments):

require 'win32ole'
 
objWSHShell = WIN32OLE.new("WScript.Shell")
objWSHShell.Run("msconfig")
sleep(2)
objWSHShell.AppActivate("System Configuration Utility")
objWSHShell.SendKeys("+{TAB}")
 
5.times do
   objWSHShell.SendKeys("{RIGHT}")
end

Think of the fun you can have automating all sorts of tasks in Windows with the SendKeys() method 😉

Digg This
Reddit This
Stumble Now!
Buzz This
Vote on DZone
Share on Facebook
Bookmark this on Delicious
Kick It on DotNetKicks.com
Shout it
Share on LinkedIn
Bookmark this on Technorati
Post on Twitter
Google Buzz (aka. Google Reader)
comments: 0 »