VBScript - SendKeys Method

本文介绍了 VBScript 中 SendKeys 方法的基本用法,包括如何发送单个字符、特殊按键及组合键到当前激活窗口,就像从键盘输入一样。此外还提供了一个使用 WScript Shell 对象的示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

VBScript - SendKeys Method

The SendKeys method is used to send keystrokes to the currently active window as if they where typed from the keyboard.

object.SendKeys Keystrokes

Single alphanumeric keystrokes can simply be specified using a string representation of the character required. For example, to send the letter S the command would be object.SendKeys “S”.

To send multiple characters combine them into one string. For example, to send A, B and C the command would be object.SendKeys “ABC”.

The plus sign “+”, caret “^”, percent sign “%”, tilde “~”, and parentheses “()” all have special meanings and must be enclosed within braces “{}”. Square brackets “[]” must also be enclosed within braces although they have no special meaning. To specify brace characters themselves, use “{{}” and “{}}”.

Below is a table of characters that can not be directly represent by a keyboard character to use one of these, specify the appropriate Code.

KeyCode
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}

To specify characters combinations use the following codes:

KeyCode
Alt%
Ctrl^
Shift Lock+

For example to specify CTRL and C, the code would be object.SendKeys “^C” and for SHIFT F5 object.SendKeys “+{F5}”. To specify multiple combination sets such as ALT A Z, you use parentheses, for example, 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"
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值