2.特殊功能键。
对于需要与Shift、Ctrl、Alt?3个控制键组合的按键,SendKeys使用特殊字符来表示。
● Shift:WshShell.SendKeys "+"。
● Ctrl:WshShell.SendKeys "^"。
● Alt:WshShell.SendKeys "%"。
由于“+”、“^”这些字符用来表示特殊的控制按键了,那又如何表示这些按键呢? 很简单,只要用大括号括住这些字符即可,例如。
要发送加号“+”,可使用“WshShell.SendKeys "{+}"”
另外,对于一些不会生成字符的控制功能按键,也同样需要使用大括号,把按键的名称“括起来”,例如,要发送回车键,需要用“WshShell.SendKeys "{ENTER}"”表示,发送向下的方向键用“WshShell.SendKeys "{DOWN}"”表示,其他的比如。
● Space:WshShell.SendKeys " "。
● ←:WshShell.SendKeys "{RIGHT}"。
● ↑:WshShell.SendKeys "{UP}"。
● F1:WshShell.SendKeys "{F1}"。
注意:如果需要发送多个重复的单字母按键,不必重复输入该字母,SendKeys允许使用简化格式进行描述,使用格式为“{按键 数字}”。例如,要发送10个字母“q”,则输入“WshShell.SendKeys "{q 10}"”即可。
大致的语法都已经了解了,其实其他的按键都是遵循这些法则的,接下来让我们来看看一些使用Sendkeys的实例。
实例一:按F5键刷新桌面:
Dim WshShell , Path , i Set WshShell = WScript.CreateObject("WScript.Shell") WshShell.SendKeys "{F5}" |
实例二:自动重启电脑:
Set WshShell = CreateObject("WScript.Shell") WshShell.SendKeys "^{ESC}u" WshShell.SendKeys "R" |
实例三:启动任务管理器:
Set WshShell = CreateObject("WScript.Shell") WshShell.SendKeys "^+{ESC}" |
实例四:在记事本中输入“Happy Birthday!”字样并保存为birth.txt:
Dim WshShell Set WshShell = WScript.CreateObject("WScript.Shell") WshShell.Run "notepad" WScript.Sleep 1500 ‘wait 1.5 second WshShell.AppActivate "无标题 - 记事本" WshShell.SendKeys "H" WScript.Sleep 500 WshShell.SendKeys "a" WScript.Sleep 500 WshShell.SendKeys "p" WScript.Sleep 500 WshShell.SendKeys "p" WScript.Sleep 500 WshShell.SendKeys "y" WScript.Sleep 500 WshShell.SendKeys " " WScript.Sleep 500 WshShell.SendKeys "B" WScript.Sleep 500 WshShell.SendKeys "i" WScript.Sleep 500 WshShell.SendKeys "r" WScript.Sleep 500 WshShell.SendKeys "t" WScript.Sleep 500 WshShell.SendKeys "h" WScript.Sleep 500 WshShell.SendKeys "d" WScript.Sleep 500 WshShell.SendKeys "a" WScript.Sleep 500 WshShell.SendKeys "y" WScript.Sleep 500 WshShell.SendKeys "!" WScript.Sleep 500 WshShell.SendKeys "%FS" WScript.Sleep 500 WshShell.SendKeys "b" WScript.Sleep 500 WshShell.SendKeys "i" WScript.Sleep 500 WshShell.SendKeys "r" WScript.Sleep 500 WshShell.SendKeys "t" WScript.Sleep 500 WshShell.SendKeys "h" WScript.Sleep 500 WshShell.SendKeys "%S" WScript.Sleep 500 WshShell.SendKeys "%FX" |