51Testing丛书连载:(三十四)精通QTP——自动化测试技术领航

发表于:2012-1-29 11:32

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:余杰 赵旭斌    来源:51Testing软件测试网

  2.特殊功能键。

  对于需要与Shift、Ctrl、Alt?3个控制键组合的按键,SendKeys使用特殊字符来表示。

  ● Shift:WshShell.SendKeys "+"。

  ● Ctrl:WshShell.SendKeys "^"。

  ● Alt:WshShell.SendKeys "%"。

  由于“+”、“^”这些字符用来表示特殊的控制按键了,那又如何表示这些按键呢? 很简单,只要用大括号括住这些字符即可,例如。

  要发送加号“+”,可使用“WshShell.SendKeys "{+}"”

  另外,对于一些不会生成字符的控制功能按键,也同样需要使用大括号,把按键的名称“括起来”,例如,要发送回车键,需要用“WshShell.SendKeys "{ENTER}"”表示,发送向下的方向键用“WshShell.SendKeys "{DOWN}"”表示,其他的比如。

  ● Space:WshShell.SendKeys " "。

  ● ←:WshShell.SendKeys "{RIGHT}"。

  ● ↑:WshShell.SendKeys "{UP}"。

  ● F1:WshShell.SendKeys "{F1}"。

  注意:如果需要发送多个重复的单字母按键,不必重复输入该字母,SendKeys允许使用简化格式进行描述,使用格式为“{按键  数字}”。例如,要发送10个字母“q”,则输入“WshShell.SendKeys "{q 10}"”即可。

  大致的语法都已经了解了,其实其他的按键都是遵循这些法则的,接下来让我们来看看一些使用Sendkeys的实例。

  实例一:按F5键刷新桌面:

Dim WshShell , Path , i

Set WshShell = WScript.CreateObject("WScript.Shell")

WshShell.SendKeys "{F5}"

  实例二:自动重启电脑:

Set WshShell = CreateObject("WScript.Shell")

WshShell.SendKeys "^{ESC}u"

WshShell.SendKeys "R"

  实例三:启动任务管理器:

Set WshShell = CreateObject("WScript.Shell")

WshShell.SendKeys "^+{ESC}"

  实例四:在记事本中输入“Happy Birthday!”字样并保存为birth.txt:

Dim WshShell

Set WshShell = WScript.CreateObject("WScript.Shell")

WshShell.Run "notepad"

WScript.Sleep 1500    wait 1.5 second

WshShell.AppActivate "无标题 记事本"

WshShell.SendKeys "H"

WScript.Sleep 500

WshShell.SendKeys "a"

WScript.Sleep 500

WshShell.SendKeys "p"

WScript.Sleep 500

WshShell.SendKeys "p"

WScript.Sleep 500

WshShell.SendKeys "y"

WScript.Sleep 500

WshShell.SendKeys " "

WScript.Sleep 500

WshShell.SendKeys "B"

WScript.Sleep 500

WshShell.SendKeys "i"

WScript.Sleep 500

WshShell.SendKeys "r"

WScript.Sleep 500

WshShell.SendKeys "t"

WScript.Sleep 500

WshShell.SendKeys "h"

WScript.Sleep 500

WshShell.SendKeys "d"

WScript.Sleep 500

WshShell.SendKeys "a"

WScript.Sleep 500

WshShell.SendKeys "y"

WScript.Sleep 500

WshShell.SendKeys "!"

WScript.Sleep 500

WshShell.SendKeys "%FS"

WScript.Sleep 500

WshShell.SendKeys "b"

WScript.Sleep 500

WshShell.SendKeys "i"

WScript.Sleep 500

WshShell.SendKeys "r"

WScript.Sleep 500

WshShell.SendKeys "t"

WScript.Sleep 500

WshShell.SendKeys "h"

WScript.Sleep 500

WshShell.SendKeys "%S"

WScript.Sleep 500

WshShell.SendKeys "%FX"

43/4<1234>
《2023软件测试行业现状调查报告》独家发布~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号