Dim wshShell
Set wshShell = CreateObject("Wscript.Shell")
wshShell.SendKeys "{ENTER}" '模擬鍵盤操做回車shell
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}" 」表示;
發送向下的方向鍵用「 Wshshell.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}app
Tips:若是須要發送多個重複的單字母按鍵,沒必要重複輸入該字母,SendKeys容許使用簡化格式進行描述,使用格式爲「{按鍵 數字}」。例如要發送10個字母「x」,則輸入「WshShell.SendKeys "{x 10}"」便可。ui
爲了說明在按下其它按鍵時應同時按下 SHIFT、CTRL、及 ALT 的任意組合鍵,請把那些按鍵的碼放在括號當中。例如,爲了說明按下 E 與 C 的時候同時按下 SHIFT 鍵,請使用 "+(EC)"。爲了說明在按下 E 的時候同時按下 SHIFT 鍵,但接着按 C 而不按 SHIFT,則使用 "+EC"。spa
實際應用舉例: ----------------------------------- 按下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
----------------------------------------------------
自動死機o(∩_∩)o...
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}" 對象