Script using Windows PowerShell ISE to move mouse and control keyboard

Hello,

I am new to PowerShell scripting, and fairly new to scripting in general.

I would like to create a script that would move the mouse to a certain location, double click, enter a number, click tab 3 three times, click spacebar, close window.

I started with: [System.Windows.Forms.Cursor]::Position = New-Object System.Drawing.Point(230,175);

But I couldn't find how to double click.

I would appreciate your help very much.

October 10th, 2012 8:33pm

Maybe something like this.  If the button your trying to push has a hotkey, even better.

[System.Windows.Forms.SendKeys]::SendWait("{ENTER}{ENTER}")		
Free Windows Admin Tool Kit Click here and download it now
October 10th, 2012 8:51pm

That would be useful for the next steps, but it doesn't simulate a mouse double-click.

Thanks.

October 10th, 2012 9:09pm

Hi Wolf,

Have a look at the vb code here

http://www.vbforums.com/showthread.php?620353-Simulate-Mouse-Click

it references an event in a library which I believe you should be able to impliment in powershell...

that should get you started (and may expose other interesting mouse related events that you could use to help you finish your task)

Cheers

Free Windows Admin Tool Kit Click here and download it now
October 10th, 2012 9:37pm

I'm not exactly sure how to integrate that. As I said, I'm new to Powershell...

Isn't there a simple command to left-click?

Another option I  can use is to select a specific window by entering its name into the script.

October 10th, 2012 10:50pm

Hi wolf,

there is no native command that i know of, and if you are strictly looking at automating mouse/kb input, there is a plethora of macro recording programs out there, powershell is not your best choice...

If you are adamant about using powershell, then I believe what I suggested might be your best course of attack

Hope that helps!

Jamie

Free Windows Admin Tool Kit Click here and download it now
October 10th, 2012 10:56pm

Have a look at WASP

http://wasp.codeplex.com/

October 10th, 2012 10:57pm

Thanks. WASP was very useful.

Free Windows Admin Tool Kit Click here and download it now
October 11th, 2012 12:10am

##In order to move the mouse position you could use something like this:

[system.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") | out-null
[System.Windows.Forms.Cursor]::Position = New-Object System.Drawing.Point(500,500)

##In order to simulate a mouse click you could use something like this:

function Click-MouseButton
{
param(
[string]$Button, 
[switch]$help)
$HelpInfo = @'

Function : Click-MouseButton
Purpose  : Clicks the Specified Mouse Button
Usage    : Click-MouseButton [-Help][-Button x]
           where      
                  -Help         displays this help
                  -Button       specify the Button You Wish to Click {left, middle, right}


'@ 

if ($help -or (!$Button))
{
    write-host $HelpInfo
    return
}
else
{
    $signature=@' 
      [DllImport("user32.dll",CharSet=CharSet.Auto, CallingConvention=CallingConvention.StdCall)]
      public static extern void mouse_event(long dwFlags, long dx, long dy, long cButtons, long dwExtraInfo);
'@ 

    $SendMouseClick = Add-Type -memberDefinition $signature -name "Win32MouseEventNew" -namespace Win32Functions -passThru 
    if($Button -eq "left")
    {
        $SendMouseClick::mouse_event(0x00000002, 0, 0, 0, 0);
        $SendMouseClick::mouse_event(0x00000004, 0, 0, 0, 0);
    }
    if($Button -eq "right")
    {
        $SendMouseClick::mouse_event(0x00000008, 0, 0, 0, 0);
        $SendMouseClick::mouse_event(0x00000010, 0, 0, 0, 0);
    }
    if($Button -eq "middle")
    {
        $SendMouseClick::mouse_event(0x00000020, 0, 0, 0, 0);
        $SendMouseClick::mouse_event(0x00000040, 0, 0, 0, 0);
    }
}
}

##To use this function in your script, implement a command like: Click-MouseButton -Button left

##Or you could just use WASP..

June 9th, 2014 7:34am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics