VBS模拟键盘输入示例

Set objShell = CreateObject("Wscript.Shell")

strCommandLine = "ttpsetup.exe" '启动安装程序

objShell.Run(strCommandLine)

set WshShell = CreateObject("WScript.Shell")

WScript.Sleep 3000 '延时3秒

WshShell.SendKeys "s" '模拟按下 S 键开始安装

WScript.Sleep 300 '延时0.3秒

WshShell.SendKeys "a" '模拟按下 A 键同意协议

WScript.Sleep 300

WshShell.SendKeys "{ENTER}" '模拟按下回车键执行下一步

WScript.Sleep 300

WshShell.SendKeys "{ENTER}" '模拟按下回车键执行下一步

WScript.Sleep 300

WshShell.SendKeys "{ENTER}" '模拟按下回车键执行下一步

WScript.Sleep 2000

WshShell.SendKeys "{TAB}" '模拟按下 Tab 键选择安装项目

WScript.Sleep 300

WshShell.SendKeys "{TAB}"

WScript.Sleep 300

WshShell.SendKeys " " '模拟按下空格键去除安装百度超级搜霸一行前的√号

WScript.Sleep 300

WshShell.SendKeys "{ENTER}" '模拟按下回车键完成最后一步

 

把以上代码复制到记事本中(右边的文字是说明,一起复制也不影响),再另存为 AutoSetup.vbs ,把此文件存放在千千静听安装执行程序(ttpsetup.exe)同一目录下,双击 AutoSetup.vbs 即可自动完成千千静听的安装。成功的关键是,你必须先安装一次千千静听,然后记住每一步的按键是什么,这个文件就可以拿去别的机上执行了。

 

SendKeys 的参数说明:

 

使用 SendKeys 方法可以将键击发送到无自动化界面的应用程序中。多数键盘字符都可用一个键击表示。某些键盘字符由多个键击组合而成(例如,CTRL+SHIFT+HOME)。要发送单个键盘字符,请将字符本身作为 string 参数发送。例如,要发送字母 x,则请发送 string 参数 "x"。

 

可使用 SendKeys 同时发送多个键击。为此,可将每个键击按顺序排列在一起,以此来创建表示一系列键击的复合字符串参数。例如,要发送键击 a、b 和 c,则需要发送字符串参数 "abc" 。SendKeys 方法将某些字符用作字符的修饰符(而不使用其本身的含义)。这组特殊的字符可包括圆括号、中括号、大括号,以及:

 

加号 "+"

插入记号 "^"

百分号 "%"

和“非”符号 "~"

 

用大括号 "{}" 括起这些字符可以发送它们。例如,要发送加号,请使用字符串参数 "{+}"。SendKeys中使用的中括号 "[ ]" 无任何特殊含义,但是必须把它们括在大括号中,以便容纳确实要赋予其特殊含义的应用程序(例如,对于动态数据交换 (DDE) 就是这样)。

 

要发送左中括号字符,请发送字符串参数 "{[}";要发送右中括号字符,请发送字符串参数 "{]}"。

要发送左大括号字符,请发送字符串参数 "{{}";要发送右大括号字符,请发送字符串参数 "{}}"。

 

某些键击不生成字符(如 ENTER 和 TAB)。某些键击表示操作(如 BACKSPACE 和 BREAK)。要发送这些类型的键击,请发送下表中列出的参数:

 

空格键 " "

退格键 {BACKSPACE} 或 {BS} 或 {BKSP}

BREAK {BREAK}

DELETE {DELETE} 或 {DEL}

向下键 {DOWN}

向上键 {UP}

向左键 {LEFT}

向右键 {RIGHT}

END {END}

ENTER {ENTER} 或 ~

ESC {ESC}

TAB {TAB}

CAPS LOCK {CAPSLOCK}

HELP {HELP}

HOME {HOME}

INSERT {INSERT} 或 {INS}

NUM LOCK {NUMLOCK}

PAGE DOWN {PGDN}

PAGE UP {PGUP}

PRINT SCREEN {PRTSC}

SCROLL LOCK {SCROLLLOCK}

 

F1 {F1}

F2 {F2}

F3 {F3}

F4 {F4}

F5 {F5}

F6 {F6}

F7 {F7}

F8 {F8}

F9 {F9}

F10 {F10}

F11 {F11}

F12 {F12}

 

要发送由常规键击和 SHIFT、CTRL 或 ALT 组合而成的键盘字符,请创建表示该键击组合的复合字符串参数。可通过在常规键击之前添加一个或多个以下特殊字符来完成上述操作:

 

SHIFT +

CTRL ^

ALT %

 

注意:这样使用时,不用大括号括起这些特殊字符。

要指定在按下多个其他键时,按下 SHIFT、CTRL 和 ALT 的组合,请创建复合字符串参数,用括号括起其中的组合键。例如,要发送的组合键指定:

 

如果在按 e 和 c 的同时按 SHIFT 键,则发送字符串参数 "+(ec)"。

如果在按 e 时只按 c(而不按 SHIFT),则发送字符串参数 "+ec"。

可使用 SendKeys 方法发送一种在一行内重复按键的键击。为此,要创建复合字符串参数,以指定要重复的键击,并在其后指定重复次数。可使用 {键击 数字} 形式的复合字符串参数来完成上述操作。例如,如果要发送 10 次 "x",则需要发送字符串参数 "{x 10}"。请确保在键击和数字之间有一个空格。

 

注意:只可以发送重复按一个键的键击。例如,可以发送 10 次 "x",但不可发送 10 次 "Ctrl+x"。

注意:不能向应用程序发送 PRINT SCREEN 键 {PRTSC}。

 

 

例子1:关机脚本 (模拟按键为:Ctrl + Esc、u、u )

 

把以下代码复制至记事本,另存为 off.vbs

 

set WshShell = CreateObject("WScript.Shell")

WshShell.SendKeys "^{ESC}uu"

 

 

例子2:重启脚本 (模拟按键为:Ctrl + Esc、u、r )

 

把以下代码复制至记事本,另存为 reboot.vbs

 

set WshShell = CreateObject("WScript.Shell")

WshShell.SendKeys "^{ESC}ur"

 

 

例子3:切换输入法 (模拟同时按下:Shift Ctrl )

 

set WshShell = CreateObject("WScript.Shell")

WshShell.SendKeys "+(^)"

 

 

例子4:输入 cn-dos.net! 至记事本并保存为 CnDos.txt

 

Dim WshShell

Set WshShell=WScript.CreateObject("WScript.Shell")

WshShell.Run "notepad"

WScript.Sleep 1500

WshShell.AppActivate "无标题 - 记事本"

WshShell.SendKeys "c"

WScript.Sleep 200

WshShell.SendKeys "n"

WScript.Sleep 200

WshShell.SendKeys "-"

WScript.Sleep 200

WshShell.SendKeys "d"

WScript.Sleep 200

WshShell.SendKeys "o"

WScript.Sleep 200

WshShell.SendKeys "s"

WScript.Sleep 200

WshShell.SendKeys "."

WScript.Sleep 200

WshShell.SendKeys "n"

WScript.Sleep 200

WshShell.SendKeys "e"

WScript.Sleep 200

WshShell.SendKeys "t"

WScript.Sleep 200

WshShell.SendKeys "!"

WScript.Sleep 200

WshShell.SendKeys "%FS"

WScript.Sleep 500

WshShell.SendKeys "C"

WScript.Sleep 200

WshShell.SendKeys "n"

WScript.Sleep 200

WshShell.SendKeys "D"

WScript.Sleep 200

WshShell.SendKeys "o"

WScript.Sleep 200

WshShell.SendKeys "s"

WScript.Sleep 200

WshShell.SendKeys "%S"

WScript.Sleep 500

WshShell.SendKeys "%FX"

 

 

例子5:自动定时存盘的记事本(此脚本来自网上)

 

'第一部分:定义变量和对象

Dim WshShell, AutoSaveTime, TXTFileName

AutoSaveTime=(1000*60*1) '自动存盘时间为1分钟

Set WshShell=WScript.CreateObject("WScript.Shell")

TXTFileName=InputBox("给文本起名(不能用中文和纯数字):")

 

'第二部分:打开并激活记事本

WshShell.Run "notepad"

WScript.Sleep 200

WshShell.AppActivate "无标题 - 记事本"

 

'第三部分:用输入的文件名存盘

WshShell.SendKeys "^s"

WScript.Sleep 300

WshShell.SendKeys TXTFileName

WScript.Sleep 300

WshShell.SendKeys "%s"

WScript.Sleep AutoSaveTime

 

'第四部分:自动定时存盘

While WshShell.AppActivate (TXTFileName)=True

WshShell.SendKeys "^s"

WScript.Sleep AutoSaveTime

Wend

WScript.Quit

 

 

例子:

 

set objShell = CreateObject("Wscript.Shell")

strCommandLine = "psetup755281.exe" '启动安装程序

objShell.Run(strCommandLine)

set WshShell = CreateObject("WScript.Shell")

WScript.Sleep 2000 '延时3秒

WshShell.SendKeys "{ENTER}" '模拟按下回车键执行下一步

WScript.Sleep 300 '延时3秒

WshShell.SendKeys "{ENTER}" '模拟按下回车键执行下一步

WScript.Sleep 300 '延时3秒

WshShell.SendKeys "{ENTER}" '模拟按下回车键执行下一步

WScript.Sleep 5200 '延时3秒

WshShell.SendKeys "{ENTER}" '模拟按下回车键执行下一步

WScript.Sleep 300 '延时3秒

WshShell.SendKeys "{ENTER}" '模拟按下回车键执行下一步

WScript.Sleep 300 '延时3秒

WshShell.SendKeys "{ENTER}" '模拟按下回车键执行下一步

WScript.Sleep 200

WshShell.SendKeys " " '模拟按下空格键去除安装百度超级搜霸一行前的√号

WScript.Sleep 100

WshShell.SendKeys "{TAB}" '模拟按下 Tab 键选择安装项目

WScript.Sleep 200

WshShell.SendKeys " " '模拟按下空格键去除安装百度超级搜霸一行前的√号

WScript.Sleep 200 '延时0.3秒

WshShell.SendKeys "f" '模拟按下 F键同意协议

 

 

 

 

PS:

 

Set WShell=WScript.CreateObject("WScript.Shell")

 

'等候2分钟,1000=1秒,自己改

WScript.Sleep 120000

 

'组合键:Ctrl+Alt+S

WShell.SendKeys "^%S"

 

'等候10秒钟,等密码锁完全启动,密码

WScript.Sleep 10000

WShell.SendKeys "0000"

 

'回车

WScript.Sleep 200

WShell.SendKeys "{ENTER}"

 

'密码

WScript.Sleep 200

WShell.SendKeys "0000"

 

'回车

WScript.Sleep 200

WShell.SendKeys "{ENTER}"

 

'回车

WScript.Sleep 200

WShell.SendKeys "{ENTER}"

 

 

PS2:在批处理中使用vbs

 

@echo offecho wscript.sleep 1000>%temp%.vbsecho set wshell=WScript.CreateObject("WScript.Shell")>>%temp%.vbsecho wshell.SendKeys "%%1">>%temp%.vbsstart %temp%.vbsexit

 

 

 

按下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}"

----------------------------------------------------

QQ消息群发

 

Dim WshShell

Set WshShell= WScript.createObject("WScript.Shell")

WshShell.AppActivate "bomb"

for i=1 to 60

WScript.Sleep 800

WshShell.SendKeys "Number0"

WshShell.SendKeys i

WshShell.SendKeys "%s"

next

----------------------------------------------------

自动到百度搜索歌曲:white flag

 

Dim WshShell,Path,i

Set WshShell = WScript.CreateObject("WScript.Shell")

WshShell.Run("IEXPLORE.EXE")

WScript.Sleep 2000

WshShell.AppActivate "about:blank-Microsoft Internet Explorer"

WshShell.SendKeys "+{TAB}"

WshShell.SendKeys "http://mp3.baidu.com"

WScript.Sleep 800

WshShell.SendKeys "{ENTER}"

WScript.Sleep 3000

WshShell.SendKeys "white flag"

WScript.Sleep 800

WshShell.SendKeys "{ENTER}"

----------------------------------------------------

在记事本中输入Happy Birthday!并保存为birth.txt

 

Dim WshShell

Set WshShell=WScript.CreateObject("WScript.Shell")

WshShell.Run "notepad"

WScript.Sleep 1500

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"

 

VBS在隐藏窗口中创建进程

描述

在本地计算机中(但是在隐藏窗口中)启动 Notepad.exe。

脚本代码复制内容到剪贴板代码: Const HIDDEN_WINDOW = 12 strComputer = "." Set objWMIService = GetObject("winmgmts:" _     & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") Set objStartup = objWMIService.Get("Win32_ProcessStartup") Set objConfig = objStartup.SpawnInstance_ objConfig.ShowWindow = HIDDEN_WINDOW Set objProcess = GetObject("winmgmts:root\cimv2:Win32_Process") errReturn = objProcess.Create("Notepad.exe", null, objConfig, intProcessID)