How to Use VBS to simulate left-click, double-click, and right-click ?......
The answer found on the internet is generally that VBS cannot be implemented, or a third-party COM (ActiveX?) is used ?) Component. I am very disgusted with third-party components. using third-party components has no portability, because others do not necessarily register this component in their systems. I suggest that you do not call third-party components in VBS code unless your program is only written and used by yourself. (By The Way, try not to use the unreliable Sendkeys method. The reason is not explained)
Okay, so much nonsense. Now let's talk about how to use VBS to control the mouse. I used to write an article "VBS calling Windows API functions". I thought it was not very easy to use VBS to control the mouse? Facts have proved that I am wrong. People who do not know the truth will always be the majority, and VBSer who does not know what the API is. Without posting real code, they won't write it themselves!
The premise of using this code is that you have installed Excel on your system, because you need to use the Excel. Application Object (if you want to think this is a third-party component, I have nothing to say ):Copy codeThe Code is 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 a workbook
Set oModule = obook. VBProject. VBComponents. Add (1) 'Add a 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 to the module
'Author: Demon
'Website: http://demon.tw
'Date: 2011/5/10
X = oExcel. Run ("GetXCursorPos") 'get the X coordinate of the mouse
Y = oExcel. Run ("GetYCursorPos") 'Get the Y coordinate of the mouse
WScript. Echo x, y
OExcel. Run "SetCursorPos", 30, 30' set the mouse x y coordinate
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 clicking
OExcel. Run "mouse_event", MOUSEEVENTF_LEFTDOWN + MOUSEEVENTF_LEFTUP, 0, 0, 0, 0
'Simulate double-clicking the left mouse button (that is, 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 the mouse and click
OExcel. Run "mouse_event", MOUSEEVENTF_MIDDLEDOWN + MOUSEEVENTF_MIDDLEUP, 0, 0, 0, 0
'Close Excel
OExcel. DisplayAlerts = False
OBook. Close
OExcel. Quit
The comments are already detailed enough. You need to know that I rarely write comments. If you still don't understand them, it means your level is to be improved!
Original article: http://demon.tw/programming/vbs-control-mouse.html