Make sendKeys faster

别等时光非礼了梦想. 提交于 2019-12-02 04:27:26

Instead of using SendKeys (which you would usually use a last resort for programs that don't have command-line support), you can use WShell to execute commands directly and get the output from StdOut or StdErr. The added advantage is you can check the return status and the output to see whether your commands succeeded or failed.

See my answer here as an example, but I'll include it here for your convenience:

Option Explicit

Const WshRunning = 0
Const WshFinished = 1
Const WshFailed = 2

Dim shell : Set shell = CreateObject("WScript.Shell")
Dim exec : Set exec = shell.Exec("ping.exe 127.0.0.1 -n 1 -w 500")

While exec.Status = WshRunning
    WScript.Sleep 50
Wend

Dim output

If exec.Status = WshFailed Then
    output = exec.StdErr.ReadAll
Else
    output = exec.StdOut.ReadAll
End If

WScript.Echo output

Alternatively, instead of executing multiple WScript shells in succession, you can write your commands into a pre-made batch file and execute that.

There is another method to do this. Not expert with the antivirus software... You may check if this suites you.

Set oShell = CreateObject("WScript.Shell")

Set oExec = oShell.exec("%comspec%")

oExec.StdIn.Write "dir" & vbCrLf

For anyone who wants to remove the delay: Instead of doing

Set Keys = CreateObject("WScript.Shell")
    Keys.sendKeys "a"
    Keys.sendKeys "{ENTER}"
    Keys.sendKeys "b"
    Keys.sendKeys "{ENTER}"
    Keys.sendKeys "c"
    Keys.sendKeys "{ENTER}

do

Set Keys = CreateObject("WScript.Shell")
Keys.sendKey "a{ENTER}b{ENTER}c{ENTER}"

It's messy, but it's WAY faster.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!