Search code examples
batch-filecmdvbscriptautomationactive-window

Batch-VBScript Send specific key(s) specific number of times


As I asked in the Question title, how can I avoid repetitive typing of key(s) when I have to send that key multiple times.

Below is my Batch-VBScript hybrid where in, for sending {DOWN} key 4 times, I tried the below code but it didn't work ?:

FINDSTR /E "'VbsCode" %~f0 > %temp%\~temp.vbs
CSCRIPT //NOLOGO %temp%\~temp.vbs
Sub TestAppAuto 'VbsCode
    On Error Resume Next 'VbsCode
    Set WshShell = WScript.CreateObject("WScript.shell") 'VbsCode
    WshShell.Visible = False 'VbsCode
    WshShell.Run "test.app",0 'VbsCode
    WScript.Sleep 500 : WshShell.AppActivate "Test Window Title" 'VbsCode
    WScript.Sleep 500 : WshShell.sendKeys "{DOWN}{4}" 'VbsCode
    :: Instead of WScript.Sleep 500 : WshShell.sendKeys "{DOWN}{DOWN}{DOWN}{DOWN}" 'VbsCode
End Sub 'VbsCode
TestAppAuto 'VbsCode
WScript.Quit 0 'VbsCode

How can I achieve what I want ?


Solution

  • From Help http://download.microsoft.com/download/winscript56/Install/5.6/W982KMeXP/EN-US/scrdoc56en.exe

    You can use the SendKeys method to send a pattern of keystrokes that consists of a single keystroke pressed several times in a row. To do this, create a compound string argument that specifies the keystroke you want to repeat, followed by the number of times you want it repeated. You do this using a compound string argument of the form {keystroke number}. For example, to send the letter "x" ten times, you would send the string argument "{x 10}". Be sure to include a space between keystroke and number.