jueves, 10 de enero de 2013

Mouse click en PowerShell

Tuve que recorrer media internet para encontrar finalmente como hacer un click del mouse con  Power Shell sin tener que usar otros programas externos. Así que acá finalmente pude armar una función que hace clicks del mouse en la posición indicada de la pantalla...

# Ejecuta un click del mouse
function MouseClick ($Button = "left", $x, $y, $cant = 1){
    $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);
'@
    [Windows.Forms.Cursor]::Position = New-Object System.Drawing.Point($x, $y);
    $SendMouseClick = Add-Type -memberDefinition $signature -name "Win32MouseEventNew" -namespace Win32Functions -passThru 
    for ($i = 1; $i -le $cant; $i++){
        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);
        }
    }
}



No hay comentarios:

Publicar un comentario