脚本在 .wsh 中运行良好,但在 HTA 中变得异常

脚本在 .wsh 中运行良好,但在 HTA 中变得异常

我有一些代码可以将密钥发送到 Telnet 来操纵我的 IR 发射器。该脚本作为独立的 WSH 脚本运行良好,但只要我尝试将其放入 HTA 内的 Sub 中,它就会变得很疯狂。它不会将我的密钥发送到 cmd 的第一个实例,而是为每个 .sendkey 打开一个新的实例!

请帮忙!

这是我正在工作的独立脚本:

    <job>
<script language="VBScript">
Option Explicit
On Error Resume Next
Dim WshShell
set WshShell=CreateObject("WScript.Shell")
WshShell.run "cmd.exe"
WScript.Sleep 500


WshShell.SendKeys "telnet 130.160.176.219 4998"

WshShell.SendKeys ("{Enter}")
WScript.Sleep 5000


WshShell.SendKeys "sendir,1:1,1,37764,1,1,340,168,21,22,21,22,21,22,21,22,21,22,21,22,21,22,21,22,21,64,21,64,21,64,21,64,21,64,21,64,21,64,21,64,21,22,21,64,21,22,21,64,21,22,21,22,21,22,21,22,21,64,21,22,21,64,21,22,21,64,21,64,21,64,21,64,21,4833"
WshShell.SendKeys ("{Enter}")
WScript.Sleep 500
WshShell.SendKeys "sendir,1:1,10,37764,1,1,340,168,21,22,21,22,21,22,21,22,21,22,21,22,21,22,21,22,21,64,21,64,21,64,21,64,21,64,21,64,21,64,21,64,21,64,21,64,21,22,21,22,21,22,21,22,21,64,21,22,21,22,21,22,21,64,21,64,21,64,21,64,21,22,21,64,21,4833"
WshShell.SendKeys ("{Enter}")
WScript.Sleep 500
WshShell.SendKeys "sendir,1:1,5,37764,1,1,340,168,21,22,21,22,21,22,21,22,21,22,21,22,21,22,21,22,21,64,21,64,21,64,21,64,21,64,21,64,21,64,21,64,21,64,21,22,21,64,21,22,21,22,21,22,21,22,21,22,21,22,21,64,21,22,21,64,21,64,21,64,21,64,21,64,21,4833"
WshShell.SendKeys ("{Enter}")
WScript.Sleep 500
WshShell.SendKeys "sendir,1:1,11,37764,1,1,340,168,21,22,21,22,21,22,21,22,21,22,21,22,21,22,21,22,21,64,21,64,21,64,21,64,21,64,21,64,21,64,21,64,21,22,21,64,21,64,21,64,21,64,21,22,21,64,21,22,21,64,21,22,21,22,21,22,21,22,21,64,21,22,21,64,21,4833"
WshShell.SendKeys ("{Enter}")
WScript.Sleep 500
WshShell.SendKeys "sendir,1:1,1,37764,1,1,340,168,21,22,21,22,21,22,21,22,21,22,21,22,21,22,21,22,21,64,21,64,21,64,21,64,21,64,21,64,21,64,21,64,21,22,21,64,21,22,21,64,21,22,21,22,21,22,21,22,21,64,21,22,21,64,21,22,21,64,21,64,21,64,21,64,21,4833"
WshShell.SendKeys ("{Enter}")
WScript.Sleep 1000
WshShell.SendKeys ("^{]}q{Enter}exit{Enter}")
WScript.Quit 
</script>
</job>

答案1

WScript.Sleep 500

在 HTA 中不起作用。这是来自 IE 窗口的(HTA 有一个)

setTimeout Method

--------------------------------------------------------------------------------

Evaluates an expression after a specified number of milliseconds has elapsed. 

Syntax

iTimerID = window.setTimeout(vCode, iMilliSeconds [, sLanguage])
Parameters

vCode Required. Variant that specifies the function pointer or string that indicates the code to be executed when the specified interval has elapsed. 
iMilliSeconds Required. Integer that specifies the number of milliseconds. 
sLanguage Optional. String that specifies one of the following values: JScript Language is JScript. 
VBScript Language is VBScript. 
JavaScript Language is JavaScript. 


Return Value

Integer. Returns an identifier that cancels the evaluation with the clearTimeout method. 

Remarks

在发送密钥之前务必使用WshShell.AppActivate,否则您不知道它们要去哪里。

由于您使用的是 TELNET 而不是 CMD,因此启动 cmd 并要求其启动 telnet 是没有意义的。只需直接启动 telnet 即可。

相关内容