什麼是VBscript 呢?html
VBscrīpt 的全稱是:Microsoft Visual Basic scrīpt Editon.(微軟公司可視化BASIC 腳本shell
版). VBS(VBscrīpt 的進一步簡寫)是基於Visual Basic 的腳本語言,其不編譯成二進制文件, 而是直接由app
宿主(host)解釋源代碼並執行, 簡單點說就是你寫的程序不須要編譯成.exe, 而是直接給用戶發送.vbs 的ide
源程序, 用戶就能執行了.ui
SendKeys 方法spa
模擬鍵盤操做,將一個或多個按鍵指令發送到指定Windows 窗口來控制應用程序運行,orm
其使用格式爲:object.SendKeys(string)htm
「object」:表示WshShell 對象對象
「string」:表示要發送的按鍵指令字符串,須要放在英文雙引號中。ip
-----------------------------------
1.基本鍵
通常來講,要發送的按鍵指令均可以直接用該按鍵字符自己來表示,例如要發送字母「x」,使用
「WshShell.SendKeys "x"」便可。固然,也可直接發送多個按鍵指令,只須要將按鍵字符按順序排列在一
起便可,例如,要發送按鍵「happy」,能夠使用「 WshShell.SendKeys "happy" 」。
2.特殊功能鍵
對於須要與Shift、Ctrl、Alt 三個控制鍵組合的按鍵,SendKeys 使用特殊字符來表示:
Shift---------WshShell.SendKeys "+"
Ctrl---------WshShell.SendKeys "^"
Alt---------WshShell.SendKeys "%" (注意這樣使用時,不用大括號括起這些特殊字符。)
因爲「+」、「^」這些字符用來表示特殊的控制按鍵了,如何表示這些按鍵呢? 只要用大括號括住這
些字符便可。例如: 要發送加號「+」,可以使用「WshShell.SendKeys "{+}"」
另外對於一些不會生成字符的控制功能按鍵,也一樣須要使用大括號括起來按鍵的名稱。
例如要發送回車鍵,須要用「 WshShell.SendKeys "{ENTER}" 」表示;
發送向下的方向鍵用「 Wshell.SendKeys "{DOWN}" 」表示。
Space---------WshShell.SendKeys " "
Enter---------WshShell.SendKeys "{ENTER}"
←---------WshShell.SendKeys "{RIGHT}"
↑---------WshShell.SendKeys "{UP}"
F1---------WshShell.SendKeys "{F1}"
按鍵代碼
BACKSPACE {BACKSPACE}, {BS}, 或{BKSP}
BREAK {BREAK}
CAPS LOCK {CAPSLOCK}
DEL or Delete {Delete} 或{DEL}
DOWN ARROW {DOWN}
END {END}
ENTER {ENTER}或~
ESC {ESC}
HELP {HELP}
HOME {HOME}
INS or Insert {Insert} 或{INS}
LEFT ARROW {LEFT}
NUM LOCK {NUMLOCK}
PAGE DOWN {PGDN}
PAGE UP {PGUP}
PRINT SCREEN {PRTSC}
RIGHT ARROW {RIGHT}
SCROLL LOCK {SCROLLLOCK}
TAB {TAB}
UP ARROW {UP}
F1 {F1}
F2 {F2}
F3 {F3}
F4 {F4}
F5 {F5}
F6 {F6}
F7 {F7}
F8 {F8}
F9 {F9}
F10 {F10}
Tips:若是須要發送多個重複的單字母按鍵,沒必要重複輸入該字母,SendKeys 容許使用簡化格式進行描述,
使用格式爲「{按鍵數字}」。例如要發送10 個字母「x」,則輸入「WshShell.SendKeys "{x 10}"」即
可。
-----------------------------------
按下F5 刷新桌面
Dim WshShell,Path,i
Set WshShell = Wscrīpt.CreateObject("Wscrīpt.Shell")
WshShell.SendKeys "{F5}"
----------------------------------------------------
電腦的自動重啓
set WshShell = CreateObject("Wscrīpt.Shell")
WshShell.SendKeys "^{ESC}u"
WshShell.SendKeys "R"
----------------------------------------------------
啓動任務管理器
set WshShell = CreateObject("Wscrīpt.Shell")
WshShell.SendKeys "^+{ESC}"
----------------------------------------------------
QQ 消息連發
Dim WshShell
Set WshShell= Wscrīpt.createObject("Wscrīpt.Shell")
WshShell.AppActivate "bomb"
for i=1 to 60
Wscrīpt.Sleep 800
WshShell.SendKeys "What do you say"
WshShell.SendKeys i
WshShell.SendKeys "%s"
next
----------------------------------------------------
自動到百度搜索歌曲:white flag
Dim WshShell,Path,i
Set WshShell = Wscrīpt.CreateObject("Wscrīpt.Shell")
WshShell.Run("IEXPLORE.EXE")
Wscrīpt.Sleep 2000
WshShell.AppActivate "about:blank-Microsoft Internet Explorer"
WshShell.SendKeys "+{TAB}"
WshShell.SendKeys "http://mp3.baidu.com"
Wscrīpt.Sleep 800
WshShell.SendKeys "{ENTER}"
Wscrīpt.Sleep 3000
WshShell.SendKeys "white flag"
Wscrīpt.Sleep 800
WshShell.SendKeys "{ENTER}"
----------------------------------------------------
自動關機
Dim WshShell
Set WshShell=Wscrīpt.CreateObject("Wscrīpt.Shell")
Wscrīpt.Sleep 2000
WshShell.Run "shutdown -r -t 120"
wscrīpt.sleep 6000
WshShell.Run "shutdown -a"
----------------------------------------------------
在記事本中輸入Happy Birthday!並保存爲birth.txt
Dim WshShell
Set WshShell=Wscrīpt.CreateObject("Wscrīpt.Shell")
WshShell.Run "notepad"
Wscrīpt.Sleep 1500
WshShell.AppActivate "無標題- 記事本"
WshShell.SendKeys "H"
Wscrīpt.Sleep 500
WshShell.SendKeys "a"
Wscrīpt.Sleep 500
WshShell.SendKeys "p"
Wscrīpt.Sleep 500
WshShell.SendKeys "p"
Wscrīpt.Sleep 500
WshShell.SendKeys "y"
Wscrīpt.Sleep 500
WshShell.SendKeys " "
Wscrīpt.Sleep 500
WshShell.SendKeys "B"
Wscrīpt.Sleep 500
WshShell.SendKeys "i"
Wscrīpt.Sleep 500
WshShell.SendKeys "r"
Wscrīpt.Sleep 500
WshShell.SendKeys "t"
Wscrīpt.Sleep 500
WshShell.SendKeys "h"
Wscrīpt.Sleep 500
WshShell.SendKeys "d"
Wscrīpt.Sleep 500
WshShell.SendKeys "a"
Wscrīpt.Sleep 500
WshShell.SendKeys "y"
Wscrīpt.Sleep 500
WshShell.SendKeys "!"
Wscrīpt.Sleep 500
WshShell.SendKeys "%FS"
Wscrīpt.Sleep 500
WshShell.SendKeys "b"
Wscrīpt.Sleep 500
WshShell.SendKeys "i"
Wscrīpt.Sleep 500
WshShell.SendKeys "r"
Wscrīpt.Sleep 500
WshShell.SendKeys "t"
Wscrīpt.Sleep 500
WshShell.SendKeys "h"
Wscrīpt.Sleep 500
WshShell.SendKeys "%S"
Wscrīpt.Sleep 500
WshShell.SendKeys "%FX"
----------------------------------------------------
製做能自動定時存盤的記事本
第一部分:定義變量和對象
Dim WshShell, AutoSaveTime, TXTFileName
AutoSaveTime=300000
Set WshShell=Wscrīpt.CreateObject("Wscrīpt.Shell")
TXTFileName=InputBox("請輸入你要建立的文件名(不能用中文和純數字):")
第二部分:打開並激活記事本
WshShell.Run "notepad"
Wscrīpt.Sleep 200
WshShell.AppActivate "無標題- 記事本"
'第三部分:用輸入的文件名存盤
WshShell.SendKeys "^s"
Wscrīpt.Sleep 300
WshShell.SendKeys TXTFileName
Wscrīpt.Sleep 300
WshShell.SendKeys "%s"
Wscrīpt.Sleep AutoSaveTime
'第四部分:自動定時存盤
While WshShell.AppActivate (TXTFileName)=True
WshShell.SendKeys "^s"
Wscrīpt.Sleep AutoSaveTime
Wend
Wscrīpt.Quit
----------------------------------------------------
DIM WSHSHELL
SET WSHSHELL=Wscrīpt.CreateOBJECT("Wscrīpt.SHELL")
'WSHSHELL.RUN " "
'Wscrīpt.SLEEP 1000
WSHSHELL.SENDKEYS "{ENTER}"
'Wscrīpt.SLEEP 1000
WSHSHELL.SENDKEYS "{ENTER}"
'Wscrīpt.SLEEP 1000
WSHSHELL.SENDKEYS "{ENTER}"
'Wscrīpt.SLEEP 1000
WSHSHELL.SENDKEYS "{ENTER}"
'Wscrīpt.SLEEP 1000
WSHSHELL.SENDKEYS "{ENTER}"
來源:http://www.jacktesting.com/testing_script/vbscript/91.html