模拟键盘输入

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

模拟键盘输入

Dim wshShell

Set wshShell = CreateObject("Wscript.Shell")

WshShell.SendKeys "{DOWN}"

留着,以后也许有用。

vbs经典回顾之[键盘输入方法]wshShell.SendKeys

[键盘输入方法]wshShell.SendKeys 方法探析(2010-09-30 14:35:11)

模拟键盘操作,将一个或多个按键指令发送到指定Windows窗口来控制应用程序运行,

其使用格式为:object.SendKeys(string)

“object”:表示WshShell对象

“string”:表示要发送的按键指令字符串,需要放在英文双引号中。

Dim wshShell

Set wshShell = CreateObject("Wscript.Shell")

wshShell.SendKeys "{ENTER}" '模拟键盘操作回车

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

1.基本键

一般来说,要发送的按键指令都可以直接用该按键字符本身来表示,例如要发送字母“x”,使用“WshS hell.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}"”即可。

实际应用举例:

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

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

Wscrīpt.Sleep 800

WshShell.SendKeys "{ENTER}"

Wscrīpt.Sleep 3000

WshShell.SendKeys "white flag"

Wscrīpt.Sleep 800

WshShell.SendKeys "{ENTER}"

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

自动关机

相关文档
最新文档