VB.NET - Simulate Mouse click using powershell

Asked By andy brown on 12-Jun-11 09:54 AM
Hi all,

Apologies for the novice question, but I have been googling and searching for nearly 3 hours to no avail! I wish to automate the simulation of pressing a mouse button using powershell in Windows 7. I can only get as far as moving the cursor, i.e.:

add-type -AssemblyName microsoft.VisualBasic
add-type -AssemblyName System.Windows.Forms
[Microsoft.VisualBasic.Interaction]::AppActivate("My Program")
[System.Windows.Forms.Cursor]::Position = New-Object System.Drawing.Point(1480,415)

But I cannot find out how to then simulate a left mouse click as the next step. Any help from anyone who could provide an answer for an absolute newbie to powershell would be warmly appreciated.

Andy
Riley K replied to andy brown on 12-Jun-11 10:13 AM
This is
the Powershell code to move the mouse to (0,0):

[System.Windows.http://www.vistax64.com/#.Cursor]::Position = New-Object
System.Drawing.Point(0,0)

Look at this documentation
http://msdn2.microsoft.com/en-us/library/ms171548.aspx

Hope it helps you 

Thanks
andy brown replied to Riley K on 12-Jun-11 10:24 AM
Hi Riley,

'Thanks but no - I can move the mouse easily already, but I cannot simulate a click. I had already seen that documentation, but I could not find how to incorporate it into powershell, having only started using it a couple of days ago. Would you be able to tell me how to do this?

Andy
Kirtan Patel replied to andy brown on 12-Jun-11 11:14 AM

Here's a http://msdn.microsoft.com/en-us/magazine/cc163301.aspx on UI automation with Powershell.  They use user32.dll functions to do things.  You could do the same thing, and then use the http://www.gamedev.net/community/forums/topic.asp?topic_id=321029 function to perform the click.  Of course you would need to write a cmdlet to do this in C# unless someone knows a way to import a non-.net dll and function into Powershell natively.  I don't believe you can perform a mouse click natively with .net.

thanks
Riley K replied to andy brown on 12-Jun-11 11:19 AM
Here's a http://msdn.microsoft.com/en-us/magazine/cc163301.aspx on UI automation with Powershell.  They use user32.dll functions to do things.  You could do the same thing, and then use the http://www.gamedev.net/community/forums/topic.asp?topic_id=321029 function to perform the click.  Of course you would need to write a cmdlet to do this in C# unless someone knows a way to import a non-.net dll and function into Powershell natively.  I don't believe you can perform a mouse click natively with .net.

Refer this link for more insight

http://social.technet.microsoft.com/Forums/en/ITCG/thread/10ec5ca1-2c20-4ba9-a420-e8533ed47d94
andy brown replied to Riley K on 12-Jun-11 12:53 PM
Kirtan : Thanks I'll look into it, but from my limited knowledge I cannot get it working off-the-bat. It seems odd that I cannot get a simple mouse click working in a simpler way :(

Riley: Thanks for looking again - but I had already seen it and the writer refers to a ".click()" command from goodness knows where!


Edit: No I'm still stumped. Having spent (several) hours looking into this, there doesn't seem to be a simple "this is how you get powershell to make the mouse click" anywhere!