我正试图在我正在做的事情中看到“鬼打字”的样子。我不得不在角色之间睡觉以获得效果。我这样做的方式是采用大量代码:
set wshell = wscript.CreateObject("wScript.Shell")
wshell.sendkeys "1"
wscript.sleep 100
wshell.sendkeys "2"
wscript.sleep 100
wshell.sendkeys "3"
wscript.sleep 100
wshell.sendkeys "4"
wscript.sleep 100
是否有更简单的方法来执行此操作,而不必为每个角色执行此操作?
答案 0 :(得分:1)
您可以将密钥放在一个数组中,然后将其传递给子程序进行输入:
Set wshell = CreateObject("wScript.Shell")
GhostType Array("1", "2", "3", "4"), 100 ' 100ms delay
GhostType Array("5", "{Tab}", "6", "{Enter}"), 200 ' Longer delay for these keys
Sub GhostType(arrKeys, intDelay)
For Each k In arrKeys
wshell.SendKeys k
wshell.Sleep intDelay
Next
End Sub
如果你想只输入没有任何控制键的字符串(tab,enter,alt,control等),你可以编写一个接受字符串的子程序,然后单独发送每个字符,中间有一个暂停:
Set wshell = CreateObject("wScript.Shell")
GhostType "Type this sentence slowly...", 100
GhostType "and this one even slower.", 200
Sub GhostType(strText, intDelay)
For i = 1 To Len(strText)
wshell.SendKeys Mid(strText, i, 1)
wshell.Sleep intDelay
Next
End Sub