Jake Devenport
Posted Jul 28, 2004 11:07 AM
I use a VBScript that I call "sendkeys.vbs" that allows me to repeat a series of keystrokes. This VBScript can be launched with Hotkeys2000 and will prompt you for the SendKeys() string that you want to perform. In addition, it will let you do something like:
sleep 300 "%fx" sleep 50 "Some text" sleep 100 "some time later"
which is helpful for automating certain repetitive tasks that require some "waiting for the OS to catch up"
.
You can also use the 'repeat num_reps' args to repeat the series of SendString
commands as in:
repeat 14 "{Down}" sleep 50 "%~" sleep 100 "%ts~" sleep 50
--Jake
Here is the VBScript code:
Option Explicit
Dim g_shell, g_fso
Dim vArgs
Set g_shell = CreateObject("WScript.Shell")
Set g_fso = CreateObject("Scripting.FileSystemObject")
Dim g_szLastCommandSent, g_szLastCommandLog
g_szLastCommandLog = g_fso.GetSpecialFolder(2).Path & "\SendKeys_LastCommand.log"
Main
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Sub Main()
Set vArgs = WScript.Arguments
Dim objFile
if vArgs.Count < 1 then
Dim szStringToSend
g_szLastCommandSent = ""
if g_fso.FileExists(g_szLastCommandLog) then
Set objFile = g_fso.OpenTextFile(g_szLastCommandLog)
g_szLastCommandSent = objFile.ReadAll
objFile.Close
Set objFile = Nothing
end if
szStringToSend = InputBox("Please enter the string that you want to use for SendKeys()", "SendKeys VBScript", g_szLastCommandSent)
if trim(szStringToSend) <> "" then
Set objFile = g_fso.OpenTextFile(g_szLastCommandLog, 2, True)
objFile.Write szStringToSend
objFile.Close
Set objFile = Nothing
Dim cmd
cmd = WScript.FullName & " " & chr(34) & WScript.ScriptFullName & chr(34) & " " & szStringToSend
if MsgBox(cmd, vbokcancel) <> vbOK then exit sub
g_shell.Run cmd
exit sub
end if
exit Sub
end if
ProcessArgs vArgs, 1, 0
End Sub
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Function ProcessArgs(vArgs, nStart, nRepeat)
'MsgBox "ProcessArgs"
Dim nIndex
Do
For nIndex = nStart to vArgs.Count
If vArgs(nIndex - 1) = "sleep" then
'MsgBox "Sleeping for " & vArgs(nIndex) & " miliseconds"
WScript.Sleep vArgs(nIndex)
nIndex = nIndex + 1
ElseIf vArgs(nIndex - 1) = "repeat" then
'MsgBox "Repeating for " & vArgs(nIndex) & " times"
' Make sure that we know where to continue
nIndex = ProcessArgs(vArgs, nIndex + 2, vArgs(nIndex))
Else
'MsgBox "Sending keys: " & vArgs (nIndex - 1)
g_shell.SendKeys vArgs(nIndex - 1)
End If
Next
nRepeat = nRepeat - 1
Loop while nRepeat > 0
ProcessArgs = nIndex
End Function