Automation via Keystroke and Mouse Click

by May 6, 2014

Occasionally, the only way of automating processes is to send keystrokes or mouse clicks to UI elements. A good and free PowerShell extension is called “WASP” and is available here:

http://wasp.codeplex.com/

Once you install the module (do not forget to unblock the ZIP file before you unpack it, via right-click, Properties, Unblock), the WASP module provides the following cmdlets:

Here is a simple automation example using the Windows calculator:

Import-Module WASP 

# launch Calculator
$process = Start-Process -FilePath calc -PassThru
$id = $process.Id
Start-Sleep -Seconds 2
$window = Select-Window | Where-Object { $_.ProcessID -eq $id }

# send keys
$window | Send-Keys 123
$window | Send-Keys '{+}'
$window | Send-Keys 999
$window | Send-Keys =

# send CTRL+c
$window | Send-Keys '^c'

# Result is now available from clipboard

And here are the caveats:

  • Once you launch a process, allow 1-2 seconds for the window to be created before you can use WASP to find the window
  • Sending keys follows the SendKeys API. Some characters need to be “escaped” by placing braces around them. More details here: http://msdn.microsoft.com/en-us/library/system.windows.forms.sendkeys.send(v=vs.110).aspx/
  • When sending control key sequences such as CTRL+C, make sure you use a lowercase letter. “^c” would send CTRL+c whereas “^C” would send CTRL+SHIFT+C
  • • Access to child controls like specific textboxes or buttons is supported for WinForms windows only (Select-ChildWindow, Select-Control). WPF windows can receive keys, too, but with WPF you have no control over the UI element on the window that receives the input.

Twitter This Tip! ReTweet this Tip!