当前位置:编程学习 > VB >>

请问如何用VB程序来模拟键盘操作

如题:打开IE浏览器进入百度首页,启动VB程序,然后VB程序最小化,然后,模拟在百度首页里输入VBgood,然后按回车
请各位来解答一下
追问:我的意思是,运行程序后,能找到IE浏览器,然后在百度首面里输入hello,然后按回车键,能否给个例程?

答案:set ws=createobject("wscript.shell") 
ws.sendKeys "^{esc}r"

VBS中SendKeys的基本应用

模拟键盘操作,将一个或多个按键指令发送到指定Windows窗口来控制应用程序运行, 
其使用格式为:object.SendKeys string 
“object”:表示WshShell对象 
“string”:表示要发送的按键指令字符串,需要放在英文双引号中。 
1.基本键 
  一般来说,要发送的按键指令都可以直接用该按键字符本身来表示,例如要发送字母“x”,使用“WshShell.SendKeys "x"”即可。当然,也可直接发送多个按键指令,只需要将按键字符按顺序排列在一起即可,例如,要发送按键“happy”,可以使用“WshShell.SendKeys "happy"”。 
2.特殊功能键 
  对于需要与Shift、Ctrl、Alt三个控制键组合的按键,SendKeys使用特殊字符来表示: 
Shift---------WshShell.SendKeys "+" 
Ctrl---------WshShell.SendKeys "^" 
Alt---------WshShell.SendKeys "%" 
由于“+”、“^”这些字符用来表示特殊的控制按键了,如何表示这些按键呢? 
只要用大括号括住这些字符即可。例如: 
要发送加号“+”,可使用“WshShell.SendKeys "{+}"” 
另外对于一些不会生成字符的控制功能按键,也同样需要使用大括号括起来按键的名称,例如要发送回车键,需要用“WshShell.SendKeys "{ENTER}"”表示,发送向下的方向键用“WshShell.SendKeys "{DOWN}"”表示。 
Space---------WshShell.SendKeys " " 
Enter---------WshShell.SendKeys "{ENTER}" 
←---------WshShell.SendKeys "{RIGHT}" 
↑---------WshShell.SendKeys "{UP}" 
F1---------WshShell.SendKeys "{F1}" 
Tips:如果需要发送多个重复的单字母按键,不必重复输入该字母,SendKeys允许使用简化格式进行描述,使用格式为“{按键 数字}”。例如要发送10个字母“x”,则输入“WshShell.SendKeys "{x 10}"”即可。 
实例: 
---------------------------------------------------- 
按下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}"

sendkey 函数

上一个:VB编程中涉及到的英语
下一个:VB如何设置禁止通过热键启动任何软件?

CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,