这篇文章将为大家详细讲解有关如何在vbscript中使用Sendkeys模拟键盘,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。
模拟键盘操作,将一个或多个按键指令发送到指定窗窗口来控制应用程序运行
其使用格式为:object.SendKeys (string)
对象:表示WshShell对象
字符串:表示要发送的按键指令字符串,需要放在英文双引号中
<>强基本键强>
每个按键由一个或多个字符表示。
为了指定单一键盘字符,必须按字符本身的键,例如,为了表示字母,可以用“A"
为了表示多个字符,就必须在字符后面直接加上另一个字符。例如,要表示,B及C,可用“ABC"作为字符串。
<强>特殊功能键强>
对于需要与转变,Ctrl, Alt三个控制键组合的按键,SendKeys使用特殊字符来表示:
- - - - - - - - - - -WshShell转变。SendKeys“+“
Ctrl - - - - - - - - - - -WshShell。SendKeys“^”
Alt - - - - - - - - - - -WshShell。SendKeys“%”
由于“+”,“^”这些字符用来表示特殊的控制按键了,如何表示这些按键呢?只要用大括号括住这些字符即可。例如:要发送加号“+”,可使WshShell用”。SendKeys“{+}“”
另外对于一些不会生成字符的控制功能按键,也同样需要使用大括号括起来按键的名称。
例如要发送回车键,需要用“WshShell。SendKeys“{进入}“;“表示,
发送向下的方向键用“Wshell。SendKeys“{下来}“;“表示
- - - - - - - - - - -WshShell空间。SendKeys“;“
输入- - - - - - - - - - -WshShell。SendKeys“{}输入“
←- - - - - - - -WshShell。SendKeys“{右}“
↑- - - - - - - - - - -WshShell。SendKeys“, {}“
F1 - - - - - - - - - - -WshShell。F1 SendKeys“{}”
按键
代码
退格
{退格},
打破
{打破}
锁帽
。
或
箭头最终
{结束}
输入{进入}或
ESC
{ESC}
帮助{帮助}
家{回家}
INS
或者
箭NUM锁
页
页
印屏幕
滚动箭头锁
标签
{标签}
到
箭头F1
{F1}
F2 {F2} F3
{F3}
F4
{F4}
F5
{F5}
F6
{F6}
F7
{F7}
F8
{F8}
F9
{F9}
F10
{F10}
如果需要发送多个重复的单字母按键,不必重复输入该字母,SendKeys允许使用简化格式进行描述,使用格式为“{按键数字}”。例如要发送10个字母“x”,则输入“WshShell。SendKeys“{x 10}“”即可
接下来看一下实例:
按下F5刷新桌面
Dim WshShell,路径,我 Set WshShell =, Wscrpt.CreateObject (“Wscrpt.Shell") WshShell.SendKeys “{F5}“
电脑的自动重启
Dim WshShell Set WshShell =, CreateObject (“Wscrpt.Shell") WshShell.SendKeys “^ {ESC} u" WshShell.SendKeys “R"
启动任务管理器
Dim WshShell Set WshShell =, CreateObject (“Wscrpt.Shell") WshShell.SendKeys “^ + {ESC}“
自动关机
Dim WshShell Set WshShell=Wscrpt.CreateObject (“Wscrpt.Shell") Wscrpt.Sleep 2000 WshShell.Run “shutdown  -r -t 120“; wscrpt.sleep 6000 WshShell.Run “shutdown -a"
在记事本中输入生日快乐!并保存为。txt
Dim WshShell Set WshShell=Wscrpt.CreateObject (“Wscrpt.Shell") WshShell.Run “notepad" Wscrpt.Sleep 1500 WshShell.AppActivate “Ξ?,安康;?±“; WshShell.SendKeys “Happy  Birthdy ! ! !“ Wscrpt.Sleep 500 WshShell.SendKeys “% FS" Wscrpt.Sleep 500 WshShell.SendKeys “birth.txt" Wscrpt.Sleep 500 WshShell.SendKeys “% S" Wscrpt.Sleep 500 WshShell.SendKeys “% FX"
简单地说,SendKey这个命令就是模拟键盘操作,将一个或多个按键指令发送到指定窗窗口来控制应用程序运行,其使用格式为:
,,,WshShell。SendKeys String ,,“字符串”表示要发送的按键指令字符串,需要放在英文双引号中。