今天想做windows上的自动化,所以才有了模拟鼠标点击的需求,先考虑用powershell实现:
首先先安装一个名为“WASP”免费可用的Powershell扩展程序,下载地址:http://wasp.codeplex.com/
下载解压之后放到C:\Windows\System32\WindowsPowerShell\v1.0\Modules\下,如图:
之后,在开始->运行处输入powershell,以管理员方式运行,
执行Import-Module
WASP,则引入了扩展程序,
假如有个已打开的cmd窗口,且名称也为cmd,在其中输入字符123,则执行以下命令:
Select-Window cmd | Send-Keys 123
具体细节可参考:
英文:http://powershell.com/cs/blogs/tips/archive/2014/05/06/automation-via-keystroke-and-mouse-click.aspx
中文:http://www.pstips.net/automation-via-keystroke-and-mouse-click.html
有时使用Select-Window焦点移动到了窗口上,但是send-key时,输入不进去东西,也就意味着输入焦点没有在新窗口上,此时这种失效,考虑用vbs先点击一下要输入的那个窗口,使得输入焦点在新窗口上,之后再执行send-keys操作。
用vbs模拟鼠标点击的操作代码如下:
mouseckick.vbs文件内容如下:
Option Explicit Dim WshShell Dim oExcel, oBook, oModule Dim strRegKey, strCode, x, y Set oExcel = CreateObject("Excel.Application") ‘创建 Excel 对象 set WshShell = CreateObject("wscript.Shell") strRegKey = "HKEY_CURRENT_USER\Software\Microsoft\Office\$\Excel\Security\AccessVBOM" strRegKey = Replace(strRegKey, "$", oExcel.Version) WshShell.RegWrite strRegKey, 1, "REG_DWORD" Set oBook = oExcel.Workbooks.Add ‘添加工作簿 Set oModule = obook.VBProject.VBComponents.Add(1) ‘添加模块 strCode = _ "‘Author: Demon" & vbCrLf & _ "‘Website: http://demon.tw" & vbCrLf & _ "‘Date: 2011/5/10" & vbCrLf & _ "Private Type POINTAPI : X As Long : Y As Long : End Type" & vbCrLf & _ "Private Declare Function SetCursorPos Lib ""user32"" (ByVal x As Long, ByVal y As Long) As Long" & vbCrLf & _ "Private Declare Function GetCursorPos Lib ""user32"" (lpPoint As POINTAPI) As Long" & vbCrLf & _ "Private Declare Sub mouse_event Lib ""user32"" Alias ""mouse_event"" (ByVal dwFlags As Long, ByVal dx As Long, ByVal dy As Long, ByVal cButtons As Long, ByVal dwExtraInfo As Long)" & vbCrLf & _ "Public Function GetXCursorPos() As Long" & vbCrLf & _ "Dim pt As POINTAPI : GetCursorPos pt : GetXCursorPos = pt.X" & vbCrLf & _ "End Function" & vbCrLf & _ "Public Function GetYCursorPos() As Long" & vbCrLf & _ "Dim pt As POINTAPI: GetCursorPos pt : GetYCursorPos = pt.Y" & vbCrLf & _ "End Function" oModule.CodeModule.AddFromString strCode ‘在模块中添加 VBA 代码 ‘Author: Demon ‘Website: http://demon.tw ‘Date: 2011/5/10 ‘x = oExcel.Run("GetXCursorPos") ‘获取鼠标 X 坐标 ‘y = oExcel.Run("GetYCursorPos") ‘获取鼠标 Y 坐标 ‘WScript.Echo x, y ‘以对话框的形式打印出当前鼠标的坐标 oExcel.Run "SetCursorPos", 200, 300 ‘设置鼠标要点击的X Y 坐标 Const MOUSEEVENTF_MOVE = &H1 Const MOUSEEVENTF_LEFTDOWN = &H2 Const MOUSEEVENTF_LEFTUP = &H4 Const MOUSEEVENTF_RIGHTDOWN = &H8 Const MOUSEEVENTF_RIGHTUP = &H10 Const MOUSEEVENTF_MIDDLEDOWN = &H20 Const MOUSEEVENTF_MIDDLEUP = &H40 Const MOUSEEVENTF_ABSOLUTE = &H8000 ‘模拟鼠标左键单击 oExcel.Run "mouse_event", MOUSEEVENTF_LEFTDOWN + MOUSEEVENTF_LEFTUP, 0, 0, 0, 0 ‘模拟鼠标左键双击(即快速的两次单击) ‘oExcel.Run "mouse_event", MOUSEEVENTF_LEFTDOWN + MOUSEEVENTF_LEFTUP, 0, 0, 0, 0 ‘oExcel.Run "mouse_event", MOUSEEVENTF_LEFTDOWN + MOUSEEVENTF_LEFTUP, 0, 0, 0, 0 ‘模拟鼠标右键单击 ‘oExcel.Run "mouse_event", MOUSEEVENTF_RIGHTDOWN + MOUSEEVENTF_RIGHTUP, 0, 0, 200, 200 ‘模拟鼠标中键单击 ‘oExcel.Run "mouse_event", MOUSEEVENTF_MIDDLEDOWN + MOUSEEVENTF_MIDDLEUP, 0, 0, 0, 0 ‘关闭 Excel oExcel.DisplayAlerts = False oBook.Close oExcel.Quit
以上代码实现的是鼠标左键单击电脑屏幕的(200,300)坐标处。
参考博客:http://demon.tw/programming/vbs-control-mouse.html
时间: 2024-11-15 13:15:01