代码:
Option Explicit
Dim WshShell
Set WshShell = CreateObject("WScript.shell")
WshShell.Run "notepad"
wait 1.5
WshShell.AppActivate "无标题-记事本" '激活指定窗口
WshShell.SendKeys "{H 5}"
wait 2
WshShell.SendKeys "^s"
wait 1
WshShell.SendKeys "t"
wait 1
WshShell.SendKeys "e"
wait 1
WshShell.SendKeys "s"
wait 1
WshShell.SendKeys "t"
wait 0.5
WshShell.SendKeys "."
wait 0.5
WshShell.SendKeys "t"
wait 0.5
WshShell.SendKeys "x"
wait 0.5
WshShell.SendKeys "t"
wait 0.5
WshShell.SendKeys "%fx" ’
知识点讲解:
1.使用sendkeys命令模拟键盘向windows窗口发送一个或多个按键指令,格式为:
object.sendkeys String
object是指WshShell对象,string是指将要发送的按键指令字符串,需要放在英文双引号中
2.按键指令分为两类
1)基本键:如要发送字母abc,使用WshShell.sendkeys "abc";要发送10个a,使用WshShell.sendkeys "{a 10}”即可
2)特殊功能键:
ctrl: WshShell.sendkeys "^"
shift: WshShell.sendkeys "+"
alt: WshShell.sendkeys "%"
如果要表示这些字符本身,用大括号括起来即可,如WshShell.sendkeys "{+}“表示+本身
3)其他:
WshShell.sendkeys " ”:空格
WshShell.sendkeys "{enter}":回车
WshShell.sendkeys "{down}":向下方向键
WshShell.sendkeys "{up}":向上方向键
WshShell.sendkeys "{F1}”:F1键