Simulate mouse clicks with Powershell/vbs automation

Source: Internet
Author: User

Today I want to do the automation on windows, so there is the need to simulate the mouse click, first consider using PowerShell implementation:

First install a free PowerShell extension called "WASP", which is available: http://wasp.codeplex.com/

Download the extract and put it under the C:\Windows\System32\WindowsPowerShell\v1.0\Modules\.

After that, enter PowerShell at start-to-run, run as Administrator,

Execution Import-ModuleWASP,则引入了扩展程序,

If you have an open cmd window with the name cmd and enter the character 123, execute the following command:

Select-window cmd | Send-keys 123

For details, refer to:

English: http://powershell.com/cs/blogs/tips/archive/2014/05/06/automation-via-keystroke-and-mouse-click.aspx

English: http://www.pstips.net/automation-via-keystroke-and-mouse-click.html

Sometimes use Select-window focus to move to the window, but send-key, input does not go in, it means that the input focus is not in the new window, this failure, consider using VBS first click on the window to enter, so that the input focus on the new window, Then perform the Send-keys operation.

The operating code for simulating mouse clicks with VBS is as follows:

The contents of the Mouseckick.vbs file are as follows:

Option ExplicitDimWshShellDimoexcel, obook, OmoduleDimstrRegKey, Strcode, x, ySetoexcel =CreateObject("Excel.Application")'Create an Excel objectSetWshShell =CreateObject("WScript. Shell") strRegKey="Hkey_current_user\software\microsoft\office\$\excel\security\accessvbom"strRegKey=Replace(strRegKey,"$", Oexcel.version) WshShell.RegWrite strRegKey,1,"REG_DWORD"Setobook = OExcel.Workbooks.Add'Add a workbookSetOmodule = obook. VBPROJECT.VBCOMPONENTS.ADD (1)'Add ModuleStrcode = _"' 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 D Y 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 coordinate'y = Oexcel.run ("Getycursorpos") ' Get mouse y coordinate'wscript.echo x, y ' prints the current mouse coordinates as a dialog boxOexcel.run"Setcursorpos", $, - 'set the x Y coordinate of the mouse to clickConstMouseeventf_move = &H1ConstMouseeventf_leftdown = &H2ConstMouseeventf_leftup = &H4ConstMouseeventf_rightdown = &H8ConstMouseeventf_rightup = &H10ConstMouseeventf_middledown = &H20ConstMouseeventf_middleup = &H40ConstMouseeventf_absolute = &H8000'Simulate left mouse button clickOexcel.run"mouse_event", Mouseeventf_leftdown + Mouseeventf_leftup,0,0,0,0'simulate the left mouse button double-click (that is, quick two 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 on the right mouse button'Oexcel.run "Mouse_event", Mouseeventf_rightdown + mouseeventf_rightup, 0, 0, $, $'simulate middle mouse click'Oexcel.run "Mouse_event", Mouseeventf_middledown + mouseeventf_middleup, 0, 0, 0, 0'Close ExcelOexcel.displayalerts =FalseOBook.CloseoExcel.Quit

The above code is to achieve the left mouse button click on the computer screen (200,300) at the coordinates.

Reference Blog: http://demon.tw/programming/vbs-control-mouse.html

Simulate mouse clicks with Powershell/vbs automation

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: info-contact@alibabacloud.com 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.