vbscript sendkeys實(shí)例代碼大全
2019-10-26 18:01:02
供稿:網(wǎng)友
 
				
其使用格式為:object.SendKeys string  
“object”:表示W(wǎng)shShell對(duì)象  
“string”:表示要發(fā)送的按鍵指令字符串,需要放在英文雙引號(hào)中。  
1.基本鍵  
  一般來(lái)說(shuō),要發(fā)送的按鍵指令都可以直接用該按鍵字符本身來(lái)表示,例如要發(fā)送字母“x”,使用“WshShell.SendKeys "x"”即可。當(dāng)然,也可直接發(fā)送多個(gè)按鍵指令,只需要將按鍵字符按順序排列在一起即可,例如,要發(fā)送按鍵“happy”,可以使用“WshShell.SendKeys "happy"”。  
2.特殊功能鍵  
  對(duì)于需要與Shift、Ctrl、Alt三個(gè)控制鍵組合的按鍵,SendKeys使用特殊字符來(lái)表示:  
Shift---------WshShell.SendKeys "+"  
Ctrl---------WshShell.SendKeys "^"  
Alt---------WshShell.SendKeys "%"  
由于“+”、“^”這些字符用來(lái)表示特殊的控制按鍵了,如何表示這些按鍵呢?  
只要用大括號(hào)括住這些字符即可。例如:  
要發(fā)送加號(hào)“+”,可使用“WshShell.SendKeys "{+}"”  
另外對(duì)于一些不會(huì)生成字符的控制功能按鍵,也同樣需要使用大括號(hào)括起來(lái)按鍵的名稱,例如要發(fā)送回車鍵,需要用“WshShell.SendKeys "{ENTER}"”表示,發(fā)送向下的方向鍵用“WshShell.SendKeys "{DOWN}"”表示。  
Space---------WshShell.SendKeys " "  
Enter---------WshShell.SendKeys "{ENTER}"  
←---------WshShell.SendKeys "{RIGHT}"  
↑---------WshShell.SendKeys "{UP}"  
F1---------WshShell.SendKeys "{F1}"  
Tips:如果需要發(fā)送多個(gè)重復(fù)的單字母按鍵,不必重復(fù)輸入該字母,SendKeys允許使用簡(jiǎn)化格式進(jìn)行描述,使用格式為“{按鍵 數(shù)字}”。例如要發(fā)送10個(gè)字母“x”,則輸入“WshShell.SendKeys "{x 10}"”即可。  
實(shí)例:  
----------------------------------------------------  
按下F5刷新桌面  
Dim WshShell,Path,i  
Set WshShell = WScript.CreateObject("WScript.Shell")  
WshShell.SendKeys "{F5}"  
----------------------------------------------------  
電腦的自動(dòng)重啟  
set WshShell = CreateObject("WScript.Shell")  
WshShell.SendKeys "^{ESC}u"  
WshShell.SendKeys "R"  
----------------------------------------------------  
啟動(dòng)任務(wù)管理器  
set WshShell = CreateObject("WScript.Shell")  
WshShell.SendKeys "^+{ESC}"  
----------------------------------------------------  
QQ消息群發(fā)  
Dim WshShell  
Set WshShell= WScript.createObject("WScript.Shell")  
WshShell.AppActivate "bomb"  
for i=1 to 60  
WScript.Sleep 800