Use VBS to control the implementation code of the mouse (get mouse coordinates, mouse movement, mouse click, mouse double click)

Source: Internet
Author: User

The answer commonly found on the web is that VBS cannot be implemented, or that a third-party COM (ActiveX? Components I am disgusted with third-party components, and using third-party components is not portable, because this component is not necessarily registered in someone else's system. My advice is to try not to call third-party components in the VBS code unless your program just writes for your own use. (By the way, also try not to use the SendKeys method, the reason does not explain)

Okay, so much nonsense, now talk about how to control the mouse with VBS. I used to write a "VBS call Windows API function", I thought since all can invoke API, with VBS control Mouse is not very simple? It turns out that I was wrong, the students who do not know the truth are always the majority, not knowing what the API is Vbser. Do not post real code, they do not write their own!

The premise of using this code is that Excel is installed on your system because you want to use the Excel.Application object (if you want to think of this as 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 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:" & 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 A s 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 ' adding VBA code in the module
' Author:demon
' website:
' DATE:2011/5/10
x = ("Getxcursorpos") ' Get mouse x coordinate
y = ("Getycursorpos") ' Get mouse Y coordinate

WScript.Echo X, Y "Setcursorpos", 30, 30 ' Set 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 button click "Mouse_event", Mouseeventf_leftdown + mouseeventf_leftup, 0, 0, 0, 0

' Simulate the left mouse button double-click (ie quick two clicks) "Mouse_event", Mouseeventf_leftdown + mouseeventf_leftup, 0, 0, 0, 0 "Mouse_event", Mouseeventf_leftdown + mouseeventf_leftup, 0, 0, 0, 0

' Analog Mouse right-click "Mouse_event", Mouseeventf_rightdown + mouseeventf_rightup, 0, 0, 0, 0
' Simulate middle mouse click "Mouse_event", Mouseeventf_middledown + mouseeventf_middleup, 0, 0, 0, 0

' Close Excel
Oexcel.displayalerts = False

Note has enough detail, to know that I rarely write comments, if still do not understand, indicating that your level needs to be improved!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.