版权声明:本文为Ye pengyu原创文章,随意转载,但是请注明出处。内容自: https://blog.csdn.net/BjarneCpp/article/details/81000634
简述
SendKeys方法用于将击键发送到当前活动窗口,就好像它们是从键盘输入的一样。
object.SendKeys Keystrokes
单个字母数字击键可以简单地通过字符串表示所需的字符来指定。例如,要发送字母S,命令将是object.SendKeys"S"
。
将多个字符合并到一个字符串中。例如,发送A、B和C命令将是object.SendKeys "ABC"
。
加号“+”,脱字符(caret)“^”,百分号“%”,波浪号“~”和括号“()”都有特殊的含义,必须用花括号“{}”括起来。方括号“[]”也必须用花括号“{}”括起来,尽管它们没有特殊的含义。要指定花括号字符本身,用“{{}”和“{}}”。
字符表
下面是一个字符表,不能用键盘字符直接表示,可以使用其中的一个,指定适当的代码。
Key | Code
-------------------------------------------
Backspace {BACKSPACE}, {BKSP} or {BS}
Break {BREAK}
Caps Lock {CAPSLOCK}
Delete {DELETE} or {DEL}
Down Arrow {DOWN}
End {END}
Enter {ENTER} or ~
Escape {ESC}
Help {HELP}
Home {HOME}
Insert {INSERT} or {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}
F11 {F11}
F12 {F12}
F13 {F13}
F14 {F14}
F15 {F15}
F16 {F16}
要指定字符组合使用下列代码:
Key | Code
---------------------------
Alt %
Ctrl ^
Shift Lock +
例如,要指定CTRL和C,代码是`object.SendKeys "^C"`。
SHIFT F5 代码是`object.SendKeys "+{F5}"`。
若要指定多个组合集,如ALT A Z,则使用圆括号(),代码是`object.SendKeys "%(AZ)"`。
Example
'VBScript Example
Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.Run "%windir%\notepad.exe"
WshShell.AppActivate "Notepad"
WshShell.SendKeys "Hello World!"
WshShell.SendKeys "{ENTER}"
WshShell.SendKeys "abc"
WshShell.SendKeys "{CAPSLOCK}"
WshShell.SendKeys "def"
' Open notepad
Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.Run "notepad.exe", 9
' Give Notepad time to load
WScript.Sleep 500
' Type in Hello World
WshShell.SendKeys "Hello World!"
WshShell.SendKeys "{ENTER}"
' Add the date
WshShell.SendKeys "{F5}"
参考:
VBScript - SendKeys Method
https://ss64.com/vb/sendkeys.html
VBS 控制按键
Vbs 脚本编程简明教程之-—妙用 SendKeys 简化重复操作