Using VBS to control the implementation code of the mouse (obtain mouse coordinates, mouse movement, mouse click, mouse double click, mouse right click)

Time:2022-6-29

How to use VBS to simulate left click, left double click and right click

The answers found on the Internet are generally that VBS cannot be implemented, or that third-party com (ActiveX?) is used Components. I am very disgusted with third-party components. Using third-party components is not portable, because other people’s systems do not necessarily register this component. My advice is to try not to call third-party components in VBS code unless your program is written for your own use. (by the way, try not to use the unreliable sendkeys method. The reason is not explained.)

All right, that’s all the nonsense, nowsay somethingThe method of using VBS to control the mouse. I wrote an article “VBS calls Windows API functions” before. I thought that since we can call API, it is not easy to control the mouse with VBS? Facts have proved me wrong. Most of the students who don’t know the truth are always the majority. There are many vbser people who don’t know what the API is. If they don’t post real code, they won’t write it by themselves!

The premise of using this code is that excel is installed on your system, because excel is used Application object (if you think this is a third-party component, I have nothing to say):

Copy codeThe codes are as follows:


Option Explicit

Dim WshShell
Dim oExcel, oBook, oModule
Dim strRegKey, strCode, x, y
Set oexcel = CreateObject (“excel.application”) ‘create an excel object

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 ‘add Workbook
Set oModule = obook. VBProject. VBComponents. Add (1) ‘add module
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 ‘add VBA code in the module
‘Author: Demon
‘Website: http://demon.tw
‘Date: 2011/5/10
x = oExcel. Run (“getxcursorpos”) ‘get the mouse X coordinate
y = oExcel. Run (“getycursorpos”) ‘get the mouse Y coordinate

WScript.Echo x, y
oExcel. Run “setcursorpos”, 30, 30 ‘set the X and Y coordinates of the mouse
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
‘simulate left mouse click
oExcel.Run “mouse_event”, MOUSEEVENTF_LEFTDOWN + MOUSEEVENTF_LEFTUP, 0, 0, 0, 0

‘simulate double clicking with the left mouse button (i.e. two quick clicks)
oExcel.Run “mouse_event”, MOUSEEVENTF_LEFTDOWN + MOUSEEVENTF_LEFTUP, 0, 0, 0, 0
oExcel.Run “mouse_event”, MOUSEEVENTF_LEFTDOWN + MOUSEEVENTF_LEFTUP, 0, 0, 0, 0

‘simulate right-click
oExcel.Run “mouse_event”, MOUSEEVENTF_RIGHTDOWN + MOUSEEVENTF_RIGHTUP, 0, 0, 0, 0
‘simulate middle click
oExcel.Run “mouse_event”, MOUSEEVENTF_MIDDLEDOWN + MOUSEEVENTF_MIDDLEUP, 0, 0, 0, 0

‘close Excel
oExcel.DisplayAlerts = False
oBook.Close
oExcel.Quit


The notes are detailed enough. You know, I seldom write notes. If I still don’t understand them, it means that your level needs to be improved!
Original: http://demon.tw/programming/vbs-control-mouse.html