关于vbs模拟按键的百钱百鸡问题程序vbs

vbs如实现无需激活指定窗口也能对该窗口模拟点击按键 - VBS求助&讨论 -
批处理之家 批处理_BAT_CMD_DOS_VBS_Perl_Python_PowerShell - Powered by Discuz!
帖子14&积分16&技术0 &捐助0 &注册时间&
vbs如实现无需激活指定窗口也能对该窗口模拟点击按键
本帖最后由 pcl_test 于
21:55 编辑
求一个用VBS写的脚本(纯VBS,网上只有按键精灵版的),后台往t.txt文本里写东西,比如,前台是这样的语句:
Set WshShell=CreateObject(&WScript.Shell&)
WshShell.AppActivate &t - 记事本&
crt.Screen.Send &it&
crt.sleep 100
crt.Screen.Send & &
crt.sleep 100
crt.Screen.Send &is&
crt.sleep 100
crt.Screen.Send & 9&
crt.sleep 100
crt.Screen.Send &{Enter}&
这个问题困扰我好久了,麻烦各位老师赐教,谢谢!非常感谢!
帖子410&积分669&技术43 &捐助0 &注册时间&
Set objFSO = CreateObject(&Scripting.FileSystemObject&)
Set objTextStream = objFSO.OpenTextFile(&D:\test.txt&, 8, True)
'这里的8代表追加文件内容,要清空原有文件内容换成2
objTextStream.WriteLine &111&
objTextStream.WriteLine &222&
objTextStream.Close复制代码
帖子410&积分669&技术43 &捐助0 &注册时间&
Set objTextStream = objFSO.OpenTextFile(&D:\test.txt&,1|2|8,True|False,-2|-1|0)
'打开文件,返回 TextStream 对象
'可以读写文件内容,不能使用文件属性,不可以复制、移动、删除文件
'True表示文件不存在时自动新建文件,False为不新建(缺省值)
&&Const ForReading& && && &= 1& &'只读(缺省值)
&&Const ForWriting& && && &= 2& &'清空文件,覆盖写入
&&Const ForAppending& && & = 8& &'在文件末尾追加写入
&&Const TristateUseDefault = -2&&'以系统默认格式打开文件
&&Const TristateTrue& && & = -1&&'以 Unicode 格式打开文件
&&Const TristateFalse& && &= 0& &'以 ASCII 格式打开文件(缺省值)
objTextStream.Skip(3)
'在读取数据时,跳过3个字符,光标位置移到3个字符之后
'每行末尾的回车换行算2个字符
'要求打开方式为只读
objTextStream.SkipLine
'在读取数据时,跳过一行,光标位置移到下一行行首
'要求打开方式为只读
strRead = objTextStream.Read(3)
'从光标位置读取3个字符,光标移到3个字符之后
'要求打开方式为只读
strRead = objTextStream.ReadLine
'从光标位置读取一行,光标移到下一行行首
'要求打开方式为只读
strRead = objTextStream.ReadAll
'从光标位置读取文件所有内容,光标移到文末
'要求打开方式为只读
blnYN = objTextStream.AtEndOfLine
'判断光标位置是否在某一行末尾,返回True|False
'要求打开方式为只读
blnYN = objTextStream.AtEndOfStream
'判断光标位置是否在文末,返回True|False
'要求打开方式为只读
objTextStream.Write &test&
'从光标位置插入字符串
objTextStream.WriteLine &test&
'从光标位置插入字符串,并回车换行
objTextStream.WriteBlankLines 5
'从光标位置插入5个空行
strRead = objTextStream.Line
'返回当前光标所在行号
strRead = objTextStream.Column
'返回当前光标所在列号
objTextStream.Close
'关闭文件对象,立刻保存到磁盘
帖子14&积分16&技术0 &捐助0 &注册时间&
& & 老师,非常感谢!这个能实现向文件追加内容的目的。现在我想的是用SENDKEY的方式,只是那是前台,我想要后台的。我知道老师要问我有您写那种方式为啥还要后台的SENDKEY?可能我一时也解释不清,但我确实需要那种后台的SENDKEY的例子。不知道老师能不能帮帮忙?占用您宝贵时间,十分过意不去,只能万分感谢!谢谢!!!
帖子14&积分16&技术0 &捐助0 &注册时间&
时间我不是用在记事本上,只是把记事本当例子,本是想用在其他软件上,起一个模拟按键操作的作用.谢谢各位老师了,拜托了!
帖子410&积分669&技术43 &捐助0 &注册时间&
本帖最后由 czjt1234 于
10:01 编辑
后台发送按键,SendKeys不行
可以考虑调用api
vbs调用api,一个是用第三方软件dynwrap.dll
一个是用Excel,如果你装了Excel
还有你的后台不知道是什么意思
如果你可以在屏幕上看到该窗口,并且该窗口是活动窗口
那么也可以用SendKeys
objwsh.SendKeys strRead& & '向当前活动窗口发送按键
'下面是操作键的发送代码
退格键& && && && & {BACKSPACE}、{BS} 或 {BKSP}
BREAK& && && && &&&{BREAK}
CAPS LOCK& && && & {CAPSLOCK}
DEL 或 DELETE& && &{DELETE} 或 {DEL}
向下键& && && && & {DOWN}
END& && && && && & {END}
ENTER& && && && &&&{ENTER} 或 ~
ESC& && && && && & {ESC}
HELP& && && && && &{HELP}
HOME& && && && && &{HOME}
INS 或 INSERT& && &{INSERT} 或 {INS}
向左键& && && && & {LEFT}
NUM LOCK& && && &&&{NUMLOCK}
PAGE DOWN& && && & {PGDN}
PAGE UP& && && && &{PGUP}
向右键& && && && & {RIGHT}
SCROLL LOCK& && &&&{SCROLLLOCK}
TAB& && && && && & {TAB}
向上键& && && && & {UP}
F1 至 F12& && && & {F1} 至 {F12}
SHIFT& && && && &&&+
CTRL& && && && && &^
ALT& && && && && & %
+ { 等修饰符& && & {+} {{} {^} {}} {%}
'如果在按 e 和 c 的同时按 SHIFT 键,则发送字符串参数 +(ec)
'可发送一个键的重复键击。如10次x,为{x 10},但不可发送10次组合键,如Ctrl+x
'不能发送 PRINT SCREEN 键 {PRTSC}
'常用 Wscript.Sleep 来延时若干毫秒发送按键,避免程序来不及响应
比如你的代码可以用vbs这么写
Set objWsh = CreateObject(&WScript.Shell&)
objWsh.AppActivate &t - 记事本&
objwsh.SendKeys &it&
Wsctipt.Sleep 100
objwsh.SendKeys & &
Wsctipt.Sleep 100
objwsh.SendKeys &is&
Wsctipt.Sleep 100
objwsh.SendKeys & 9&
Wsctipt.Sleep 100
objwsh.SendKeys &{Enter}&复制代码
帖子14&积分16&技术0 &捐助0 &注册时间&
& & 非常感谢老师!我用的是一个模拟显示的程序,它是集显示和按键一体的,我想对这个程序模拟按键进行设置,比如,我按A3,这个模拟按键程序就进入相应菜单,我能看到这个按键过程,但是用SENDKEY是一个前台命令,必须保证电脑没人动,否则就不知道发送到哪去了,所以我就想把它转换成后台命令,这样,我只要开着模拟按键程序就可以用电脑做其他事情了。可我不会后台,网上找了好久,也没有老师帮我。确实太困惑了。网上找了个按键精灵版的后台向记事本发送,也不懂怎么移植到纯VBS上来。老师说的API,具体我的知识有限,所以我也不懂,恳请老师写个简单的实例,小可不剩感激!非常感谢!!
附件: 您需要才可以下载或查看附件。没有帐号?
帖子14&积分16&技术0 &捐助0 &注册时间&
主界面我按31后变成
附件: 您需要才可以下载或查看附件。没有帐号?
帖子14&积分16&技术0 &捐助0 &注册时间&
这个是按键精灵版的后台向记事本发送按键的例子
'获得符合标题为&t - 记事本&的窗口句柄
HwndEx = Plugin.Window.Find(0, &t - 记事本&)
If HwndEx = 0 Then
MsgBox &没有找到符合的窗口,请检查标题是否正确&
'获得记事本子窗口,类名为&Edit&
Hwnd = Plugin.Window.FindEx(HwndEx, 0, &Edit&, 0)
'向指定窗口输入一个按键,按键码49为1键
Call Plugin.Bkgnd.KeyPress(Hwnd, 49)
'向指定窗口输入一个按键,按键码65为A键
Call Plugin.Bkgnd.KeyPress(Hwnd, 65)
'向指定窗口输入一个按键,按键码13为回车键
Call Plugin.Bkgnd.KeyPress(Hwnd, 13)
'向指定窗口发送一个文本消息
Call Plugin.Bkgnd.SendString(Hwnd, &我是文本内容,OY!&)
帖子410&积分669&技术43 &捐助0 &注册时间&
本帖最后由 czjt1234 于
09:11 编辑
6楼的vbs也是前台的,不行
按键精灵我没用过,不过看9楼的代码,用到了句柄,那就是调用了api
我找了个vbs调用api的例子,你研究下,需要用到dynwrap.dll文件
有2个版本,一个36K,一个44K,都可以用
附件: 您需要才可以下载或查看附件。没有帐号?
帖子410&积分669&技术43 &捐助0 &注册时间&
Declare Function keybd_event Lib &user32& Alias &keybd_event& (ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long)
发送按键用这个api
这里有api原型和vbs调用的格式Dim UserWrap,hWnd
Set UserWrap = CreateObject(&DynamicWrapper&)
'Declare Function FindWindow Lib &user32& Alias &FindWindowA& (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
UserWrap.Register &USER32.DLL&, &FindWindow&, &I=ss&, &f=s&, &R=l&
'Declare Function SetWindowPos Lib &user32& Alias &SetWindowPos& (ByVal hwnd As Long, ByVal hWndInsertAfter As Long, ByVal x As Long, ByVal y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long
UserWrap.Register &USER32.DLL&, &SetWindowPos&, &I=Hllllll&, &f=s&, &R=l&
hWnd = UserWrap.FindWindow(vbNullString, &计算器&)
UserWrap.SetWindowPos hWnd, -1, 0, 0, 0, 0, 3
MsgBox &现在将鼠标移到左上角!&
'Declare Function SetCursorPos Lib &user32& (ByVal X As Long, ByVal Y As Long) As Long
UserWrap.Register &USER32.DLL&, &SetCursorPos&, &I=ll&, &f=s&, &R=l&
UserWrap.SetCursorPos 0,0复制代码
帖子410&积分669&技术43 &捐助0 &注册时间&
REM i: (Argument Type)
REM 'a', sizeof(IDispatch*), VT_DISPATCH}& && &&&// a IDispatch*
REM 'c', sizeof(unsigned char), VT_I4}& && && && && & // c signed char
REM 'd', sizeof(double), VT_R8}& && && && && && && && &// d 8 byte real
REM 'f', sizeof(float), VT_R4}& && && && && && && && &// f 4 byte real
REM 'k', sizeof(IUnknown*), VT_UNKNOWN}& && && && && & // k IUnknown*
REM 'h', sizeof(long), VT_I4}& && && && && && && && &// h HANDLE
REM 'l', sizeof(long), VT_I4}& && && && && && && && &// l long
REM 'p', sizeof(void*), VT_PTR}& && && && && && && && &// p pointer
REM 's', sizeof(BSTR), VT_LPSTR}& && && && && & // s string
REM 't', sizeof(short), VT_I2}& && && && && && && && &// t short
REM 'u', sizeof(UINT), VT_UINT}& && && && && && && && &// u unsigned int
REM 'w', sizeof(BSTR), VT_LPWSTR}& && && && && & // w wide string
REM f: (Call Method)
REM 'm' - DC_MICROSOFT 0x0000, Default
REM 'b' - DC_BORLAND 0x0001, Borland compat
REM 's' - DC_CALL_STD 0x0020, __stdcall
REM 'c' - DC_CALL_CDECL 0x0010, __cdecl
REM '4' - DC_RETVAL_MATH4 0x0100, Return value in ST
REM '8' - DC_RETVAL_MATH8 0x0200, Return value in ST
REM r: (Return Type)
REM Same as i
上面是&I=ss&, &f=s&, &R=l&参数
I是输入,s代表一个String类型的参数,
f=s不用管,照抄
但是这种调用办法有很多局限,比如指针类型的参数就不行,自定义类型的参数也不行
用excel调用,格式方面比较简单
下面是个例子Option Explicit
Dim WshShell, oExcel, strRegKey, strCode, x, y
Set oExcel = CreateObject(&Excel.Application&)
set WshShell = CreateObject(&wscript.Shell&)
strRegKey = &HKEY_CURRENT_USER\Software\Microsoft\Office\$\Excel\Security\AccessVBOM&
strRegKey = Replace(strRegKey, &$&, oExcel.Version)
'生成注册表路径,oExcel.Version 是当前版本号
WshShell.RegWrite strRegKey, 1, &REG_DWORD&
'写如注册表,1表示设置安全级别为低,这样添加宏就不会有安全提示了
strCode = _
&Private Declare Function SetCursorPos Lib &&user32&& (ByVal x As Long, ByVal y As Long) As Long& & vbCrLf & _
& && && && && && && && && && && && && && && && && && && && && && && && && && && && && && && && && & vbCrLf & _
&Private Type POINTAPI&& && && && && && && && && && && && && && && && && && && && && && && && && && vbCrLf & _
& & &X As Long&& && && && && && && && && && && && && && && && && && && && && && && && && && && &&&& vbCrLf & _
& & &Y As Long&& && && && && && && && && && && && && && && && && && && && && && && && && && && &&&& vbCrLf & _
&End Type&& && && && && && && && && && && && && && && && && && && && && && && && && && && && && & & vbCrLf & _
& && && && && && && && && && && && && && && && && && && && && && && && && && && && && && && && && & vbCrLf & _
&Private Declare Function GetCursorPos Lib &&user32&& (lpPoint As POINTAPI) As Long&& && && && &&&& vbCrLf & _
& && && && && && && && && && && && && && && && && && && && && && && && && && && && && && && && && & vbCrLf & _
&Sub SetCursor(x as Long, y as Long)&& && && && && && && && && && && && && && && && && && && && & & vbCrLf & _
& & &SetCursorPos x, y&& && && && && && && && && && && && && && && && && && && && && && && && && && vbCrLf & _
&End Sub&& && && && && && && && && && && && && && && && && && && && && && && && && && && && && &&&& vbCrLf & _
& && && && && && && && && && && && && && && && && && && && && && && && && && && && && && && && && & vbCrLf & _
&Public Function GetXCursorPos() As Long&& && && && && && && && && && && && && && && && && && && && vbCrLf & _
& & &Dim pt As POINTAPI&& && && && && && && && && && && && && && && && && && && && && && && && &&&& vbCrLf & _
& & &GetCursorPos pt&& && && && && && && && && && && && && && && && && && && && && && && && && &&&& vbCrLf & _
& & &GetXCursorPos = pt.X&& && && && && && && && && && && && && && && && && && && && && && && && && vbCrLf & _
&End Function&& && && && && && && && && && && && && && && && && && && && && && && && && && && && && vbCrLf & _
& && && && && && && && && && && && && && && && && && && && && && && && && && && && && && && && && & vbCrLf & _
&Public Function GetYCursorPos() As Long&& && && && && && && && && && && && && && && && && && && && vbCrLf & _
& & &Dim pt As POINTAPI&& && && && && && && && && && && && && && && && && && && && && && && && &&&& vbCrLf & _
& & &GetCursorPos pt&& && && && && && && && && && && && && && && && && && && && && && && && && &&&& vbCrLf & _
& & &GetYCursorPos = pt.Y&& && && && && && && && && && && && && && && && && && && && && && && && && vbCrLf & _
&End Function&
oExcel.Workbooks.Add.VBProject.VBComponents.Add(1).CodeModule.AddFromString strCode
x = oExcel.Run(&GetXCursorPos&)
y = oExcel.Run(&GetYCursorPos&)
WScript.Echo x, y
oExcel.Run &SetCursor&,
oExcel.DisplayAlerts = False
oExcel.Workbooks.Add.Close
oExcel.Quit复制代码
帖子410&积分669&技术43 &捐助0 &注册时间&
本帖最后由 czjt1234 于
09:16 编辑
按键精灵屏蔽了具体的api,给你固定格式直接调用,很方便
看它代码,应该是先获取句柄,再向指定句柄发送按键,不知道是哪个api
正好我也想研究“向指定句柄发送按键”,我们一起交流交流
帖子14&积分16&技术0 &捐助0 &注册时间&
非常感谢老师!给您添麻烦了!我先看看,研究一下再来说说。谢谢!非常感谢!!!!
帖子14&积分16&技术0 &捐助0 &注册时间&
老师,12L那个例子好像是获取鼠标坐标的,实际应用中不知道该怎么用?11L的Set UserWrap = CreateObject(&DynamicWrapper&)报错,说ActiveX部件不能创建对象:'DynamicWrapper'
[通过 QQ、MSN 分享给朋友]3被浏览660分享邀请回答01 条评论分享收藏感谢收起写回答豆丁微信公众号
君,已阅读到文档的结尾了呢~~
面是VBS模拟按键的 不懂的可以问我Q可以,vbs,VBS,模拟按键
扫扫二维码,随身浏览文档
手机或平板扫扫即可继续访问
面是VBS模拟按键的 不懂的可以问我Q
举报该文档为侵权文档。
举报该文档含有违规或不良信息。
反馈该文档无法正常浏览。
举报该文档为重复文档。
推荐理由:
将文档分享至:
分享完整地址
文档地址:
粘贴到BBS或博客
flash地址:
支持嵌入FLASH地址的网站使用
html代码:
&embed src='http://www.docin.com/DocinViewer-4.swf' width='100%' height='600' type=application/x-shockwave-flash ALLOWFULLSCREEN='true' ALLOWSCRIPTACCESS='always'&&/embed&
450px*300px480px*400px650px*490px
支持嵌入HTML代码的网站使用
您的内容已经提交成功
您所提交的内容需要审核后才能发布,请您等待!
3秒自动关闭窗口您所在位置: &
&nbsp&&nbsp&nbsp&&nbsp
VBS模拟按键(VBS simulation key).doc 10页
本文档一共被下载:
次 ,您可全文免费在线阅读后下载本文档。
下载提示
1.本站不保证该用户上传的文档完整性,不预览、不比对内容而直接下载产生的反悔问题本站不予受理。
2.该文档所得收入(下载+内容+预览三)归上传者、原创者。
3.登录后可充值,立即自动返金币,充值渠道很便利
需要金币:200 &&
你可能关注的文档:
··········
··········
VBS模拟按键(VBS simulation key)
VBS SendKey usage
Simply put, this command is to simulate the keyboard operation, sending one or more key instructions to the specified Windows window to control the application run, and the format is:
Object. The SendKeys string
&Object& : represents the WshShell object
&String& : represents the key command string to be sent, which needs to be placed in double quotation marks in English.
Basic key 1.
Generally speaking, the key instructions to send can be represented directly by the key character itself, for example, to send the letter &x&
WshShell. SendKeys &x&.
You can also send multiple key commands directly, just by placing the key characters in sequence. For example, to send the key &cfan&, you can use &WshShell. SendKeys& cfan.
2. Special function keys
For the keypad SendKeys that need to be combined with Shift, Ctrl and Alt, the special characters are used to represent:
Special control key special characters
If you want to send is a combination of keys and press Ctrl + E, need to use &WshShell. SendKeys& ^ E &, &said,
If you want to send a combination of keys is holding down the Ctrl key while pressing the E and C two key, then you should use parentheses around the letters, writing format for &WshShell. The SendKeys& ^ &(ec).& here to pay attention to it and &WshShell. The SendKeys& ^ &ec& distinction, who said combination keys is hold down Ctrl and E keys at the same time, and then loosen the Ctrl key, separate press the &C& letter key
Due to the &+&, &^& these characters used to represent special control buttons, how to represent these buttons? With braces enclose the these characters. For example, to send a plus sign &+&, can use &WshShell SendKeys& {+} &&. In addition to some won't generate control function buttons of the charact
正在加载中,请稍后...VBS模拟按键方法,详细点,每步要讲解,谢了_百度知道
VBS模拟按键方法,详细点,每步要讲解,谢了
比如我先输入123456,在按回车,在输入321654。还有,组合键怎么办,如CTRL+ENTER
我有更好的答案
&#39.SendKeys &123456& Ws.SendKeys &quot。例如,要发送的组合键指定:
'x&,可以发送 10 次 &可使用 SendKeys 方法发送一种在一行内重复按键的键击。为此,则发送字符串参数 &+ec&。
'{enter}& Ws.SendKeys &。
&#39,用括号括起其中的组合键;如果在按 e 时只按 c(而不按 SHIFT);wscript,则发送字符串参数 &+(ec)&如果在按 e 和 c 的同时按 SHIFT 键; &#39。可使用 {键击 数字} 形式的复合字符串参数来完成上述操作;要指定在按下多个其他键时,按下 SHIFTDim Ws set Ws = CreateObject(&quot、CTRL 和 ALT 的组合,请创建复合字符串参数.Shell&) Ws;321654&quot,则需要发送字符串参数 &{x 10}&;x&。例如,如果要发送 10 次 &注意 不能向应用程序发送 PRINT SCREEN 键 {PRTSC},要创建复合字符串参数,以指定要重复的键击,并在其后指定重复次数。请确保在键击和数字之间有一个空格。 '注意 只可以发送重复按一个键的键击。例如,但不可发送 10 次 &Ctrl+x&
采纳率:67%
为您推荐:
换一换
回答问题,赢新手礼包
个人、企业类
违法有害信息,请在下方选择后提交
色情、暴力
我们会通过消息、邮箱等方式尽快将举报结果通知您。

我要回帖

更多关于 vbs模拟按键 的文章

 

随机推荐