Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use VBS to control mouse

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

Editor to share with you how to use VBS to control the mouse, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to understand it!

How to use VBS to simulate left-click, left-click, right-click?

The answer found on the Internet is generally that VBS cannot be implemented, or that a third party COM (ActiveX? ) component. I have an aversion to third-party components, and there is no portability to use third-party components, because this component is not necessarily registered in someone else's system. 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. Now let's talk about how to control the mouse with VBS. I once wrote an article "VBS calls Windows API function". I thought that now that API can be called, it is not very easy to use VBS to control the mouse. Facts have proved me wrong, the students who don't know the truth are always the majority, and there are many VBSer who don't know what API is. If they don't post real code, they won't write it at all!

The prerequisite for using this code is that Excel is installed on your system, because you need to use the Excel.Application object (if you want to think that this is a third-party component, I have nothing to say):

The code is as follows:

Option Explicit

Dim WshShell

Dim oExcel, oBook, oModule

Dim strRegKey, strCode, x, y

Set oExcel = CreateObject ("Excel.Application") 'create 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 to the module

'Author: Demon

'Website: http://demon.tw

Date: 2011-5-10

X = oExcel.Run ("GetXCursorPos") 'get mouse X coordinates

Y = oExcel.Run ("GetYCursorPos") 'get mouse Y coordinates

WScript.Echo x, y

OExcel.Run "SetCursorPos", 30,30 'set mouse X Y coordinates

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 the left mouse button click

OExcel.Run "mouse_event", MOUSEEVENTF_LEFTDOWN + MOUSEEVENTF_LEFTUP, 0,0,0,0

'simulate a double click with 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 mouse click

OExcel.Run "mouse_event", MOUSEEVENTF_RIGHTDOWN + MOUSEEVENTF_RIGHTUP, 0,0,0,0

'simulate a middle mouse button click

OExcel.Run "mouse_event", MOUSEEVENTF_MIDDLEDOWN + MOUSEEVENTF_MIDDLEUP, 0,0,0,0

'close Excel

OExcel.DisplayAlerts = False

OBook.Close

OExcel.Quit

The above is all the contents of the article "how to use VBS to Control the Mouse". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report