在测试的过程中,经常会遇到这样的情况:
1. 需要使用到一些按键的操作,如按Enter,F1什么的情况;
2. 需要模拟组合键的情况,如按Ctrl+A什么的;
3. 需要无法识别的控件中输入一些东西。
在这时候就可以使用SendKeys来进行模拟按键的操作。
先上一个例子:
Dim WshShell
Set WshShell = CreateObject("WScript.Shell")
WshShell.SendKeys "{ENTER}"
如果要发送的是一个字符串,就可以:
WshShell.SendKeys "hello"
如果是一些组合按钮,如:Shift、Ctrl、Alt,可用特殊字符代替:
WshShell.SendKeys "+" '按Shift
WshShell.SendKeys "^" '按Ctrl
WshShell.SendKeys "%" '按Alt
WshShell.SendKeys "^s" '按Ctrl+S进行保存的操作
如果是一些特殊的按键,如空格什么的:
WshShell.SendKeys "{ENTER}" '按回车
WshShell.SendKeys "{UP}" '按光标向上
WshShell.SendKeys "{LEFT}" '按光标向左
WshShell.SendKeys "{F1}" '按F1
用这个方法就能模拟一些按键的操作。
但是这个方法好像无法模拟出输入中文,只能模拟键盘按英文键。