使 sendKeys 更快 [英] Make sendKeys faster

查看:21
本文介绍了使 sendKeys 更快的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个程序,该程序将打开命令提示符"并使用sendKeys"打开特定端口.这是我的代码:

I'm trying to create a program that will open "command prompt" and open a specific port using "sendKeys". Here is my code:

Set Keys = CreateObject("WScript.Shell")
oShell.ShellExecute "cmd.exe","runas", 1
Sleep(0.01)
Keys.sendKeys "{ENTER}"
Sleep(0.01)
Keys.sendKeys "rem Open TCP Port 407 inbound and outbound"
Keys.sendKeys "{ENTER}"
Keys.sendKeys "netsh advfirewall firewall add rule name=""EXAMPLE"" dir=out action=allow protocol=TCP localport=407"
Keys.sendKeys "{ENTER}"
Keys.sendKeys "netsh advfirewall firewall add rule name=""EXAMPLE""  protocol=TCP dir=out localport=407 action=""allow"""
Keys.sendKeys "{ENTER}"

我认为按键不够快.

我不想使用 Keys.run "cmd [code]" 因为那样防病毒软件可能会认为该程序是病毒.

I don't want to use Keys.run "cmd [code]" because then an Antivirus might think that the program is a virus.

推荐答案

您可以使用 WShell 来执行命令,而不是使用 SendKeys(对于没有命令行支持的程序,您通常会使用它作为最后的手段)直接从 StdOut 或 StdErr 获取输出.额外的好处是你可以检查返回状态和输出,看看你的命令是成功还是失败.

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

或者,您可以将命令写入预先制作的批处理文件并执行,而不是连续执行多个 WScript shell.

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

这篇关于使 sendKeys 更快的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆