Tuesday, July 26, 2011

Using Windows Script for sending keys to an application

Open the calculator and show the About message for 3 seconds:


Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.Run("calc")
WScript.Sleep(100)
WshShell.AppActivate("Calculator")
WScript.Sleep(100)
WshShell.SendKeys("%H")
WScript.Sleep(500)
WshShell.SendKeys("{DOWN}")
WScript.Sleep(100)
WshShell.SendKeys("~")
WScript.Sleep(3000)
WshShell.SendKeys("{ESC}")

Close the Calculator:


Set WshShell = WScript.CreateObject("WScript.Shell")
Result = WshShell.AppActivate("Calculator")
WScript.Sleep(100)
If Result = True Then
WshShell.SendKeys "%{F4}"
End If

References:
http://technet.microsoft.com/en-us/library/ee156592.aspx
http://msdn.microsoft.com/en-us/library/8c6yea83(v=vs.85).aspx

No comments: