wshShell.SendKeys模擬鍵盤操作


Dim wshShell
Set wshShell = CreateObject("Wscript.Shell")
wshShell.SendKeys "{ENTER}"     '模擬鍵盤操作回車

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}

Tips:如果需要發送多個重復的單字母按鍵,不必重復輸入該字母,SendKeys允許使用簡化格式進行描述,使用格式為“{按鍵 數字}”。例如要發送10個字母“x”,則輸入“WshShell.SendKeys "{x 10}"”即可。

為了說明在按下其它按鍵時應同時按下 SHIFT、CTRL、及 ALT 的任意組合鍵,請把那些按鍵的碼放在括號當中。例如,為了說明按下 E 與 C 的時候同時按下 SHIFT 鍵,請使用 "+(EC)"。為了說明在按下 E 的時候同時按下 SHIFT 鍵,但接着按 C 而不按 SHIFT,則使用 "+EC"。

實際應用舉例: ----------------------------------- 按下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}" 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM