SendInput バッチファイルとパワーシェルでマウスとキーボードを操作する powershell

SendInputを使ってC#で関数を作りパワー処理で実行します

バッチファイル

@powershell/c '#'+(Get-Content  \"%~f0\"-raw)^|Invoke-Expression &timeout /t 50&goto:eof
$cSource = @'
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Runtime.InteropServices;
public class Clicker{
  [StructLayout(LayoutKind.Sequential)]
  struct INPUT{
    public int type;
    // 0 = INPUT_MOUSE,
    // 1 = INPUT_KEYBOARD,
    // 2 = INPUT_HARDWARE
    public InputUnion ui;
  }
  [StructLayout(LayoutKind.Explicit)]
  private struct InputUnion{
    [FieldOffset(0)] public MOUSEINPUT    mi;
    [FieldOffset(0)] public KEYBOARDINPUT ki;
    [FieldOffset(0)] public HARDWAREINPUT hi;
  }
    [StructLayout(LayoutKind.Sequential)]
    private struct POINT
    {
        public int x;
        public int y;
    }


  [StructLayout(LayoutKind.Sequential)]
  private struct MOUSEINPUT{
    public int dx;
    public int dy;
    public int mouseData;
    public int dwFlags;
    public int time;
    public IntPtr dwExtraInfo;
  }
  
const int MOUSEEVENTF_MOVED      = 0x0001 ;
const int MOUSEEVENTF_LEFTDOWN   = 0x0002 ;
const int MOUSEEVENTF_LEFTUP     = 0x0004 ;
const int MOUSEEVENTF_RIGHTDOWN  = 0x0008 ;
const int MOUSEEVENTF_RIGHTUP    = 0x0010 ;
const int MOUSEEVENTF_MIDDLEDOWN = 0x0020 ;
const int MOUSEEVENTF_MIDDLEUP   = 0x0040 ;
const int MOUSEEVENTF_WHEEL      = 0x0080 ;
const int MOUSEEVENTF_XDOWN      = 0x0100 ;
const int MOUSEEVENTF_XUP        = 0x0200 ;
const int MOUSEEVENTF_ABSOLUTE   = 0x8000 ;

const int screen_length = 0x10000 ;


  [StructLayout(LayoutKind.Sequential)]
  private struct KEYBOARDINPUT{
    public short wVk;
    public short wScan;
    public int   dwFlags;
    public int   time;
    public IntPtr dwExtraInfo;
  }
  private const int INPUT_MOUSE    = 0x0;
  private const int INPUT_KEYBOARD = 0x1;
  private const int INPUT_HARDWARE = 0x2;
  private const int KEYEVENTF_KEYDOWN = 0x0;
  private const int KEYEVENTF_KEYUP   = 0x2;
  private const int KEYEVENTF_EXTENDEDKEY = 0x1;
    private const int MAPVK_VK_TO_VSC = 0;

  [StructLayout(LayoutKind.Sequential)]
  private struct HARDWAREINPUT{
      public int uMsg;
      public short wParamL;
      public short wParamH;
  };

  [DllImport("user32.dll")]
  private extern static uint SendInput(
    int ninput, INPUT[] pinput, int cbSize);

  [DllImport("user32.dll", EntryPoint = "MapVirtualKeyA")]
  private extern static int MapVirtualKey(
    int wCode, int wMapType);

public static void MousePoint(int x, int y)
{
    INPUT[] input = new INPUT[1];
    input[0].type = INPUT_MOUSE;
    input[0].ui.mi.dx = x*(65535/Screen.PrimaryScreen.Bounds.Width);
    input[0].ui.mi.dy = y*(65535/Screen.PrimaryScreen.Bounds.Height);
    input[0].ui.mi.dwFlags = MOUSEEVENTF_MOVED | MOUSEEVENTF_ABSOLUTE;
    input[0].ui.mi.time = 0;
    input[0].ui.mi.dwExtraInfo = IntPtr.Zero;
    SendInput(input.Length, input, Marshal.SizeOf(input[0]));
}
public static void MouseClick(int Flags)
{
    INPUT[] input = new INPUT[1];
    input[0].type = INPUT_MOUSE;
    input[0].ui.mi.dwFlags = Flags;
    input[0].ui.mi.time = 0;
    input[0].ui.mi.dwExtraInfo = IntPtr.Zero;
    SendInput(input.Length, input, Marshal.SizeOf(input[0]));
}
public static void SendInputKeyPressAndRelease(Keys key)
{
        INPUT[] input = new INPUT[2];
        int vsc = MapVirtualKey((int)key, MAPVK_VK_TO_VSC);

        input[0].type = INPUT_KEYBOARD;
        input[0].ui.ki.wVk = (short)key;
        input[0].ui.ki.wScan = (short)vsc;
        input[0].ui.ki.dwFlags = KEYEVENTF_KEYDOWN;
        input[0].ui.ki.time = 0;
        input[0].ui.ki.dwExtraInfo = IntPtr.Zero;

        input[1].type = INPUT_KEYBOARD;
        input[1].ui.ki.wVk = (short)key;
        input[1].ui.ki.wScan = (short)vsc;
        input[1].ui.ki.dwFlags = KEYEVENTF_KEYUP;
        input[1].ui.ki.time = 0;
        input[1].ui.ki.dwExtraInfo = IntPtr.Zero;

        SendInput(input.Length, input, Marshal.SizeOf(input[0]));
}

public static void SendInputKeyPress(Keys key)
{
        INPUT[] input = new INPUT[1];
        int vsc = MapVirtualKey((int)key, MAPVK_VK_TO_VSC);

        input[0].type = INPUT_KEYBOARD;
        input[0].ui.ki.wVk = (short)key;
        input[0].ui.ki.wScan = (short)vsc;
        input[0].ui.ki.dwFlags = KEYEVENTF_KEYDOWN;
        input[0].ui.ki.time = 0;
        input[0].ui.ki.dwExtraInfo = IntPtr.Zero;

        SendInput(input.Length, input, Marshal.SizeOf(input[0]));
}

public static void SendInputKeyRelease(Keys key)
{
        INPUT[] input = new INPUT[1];
        int vsc = MapVirtualKey((int)key, MAPVK_VK_TO_VSC);

        input[0].type = INPUT_KEYBOARD;
        input[0].ui.ki.wVk = (short)key;
        input[0].ui.ki.wScan = (short)vsc;
        input[0].ui.ki.dwFlags = KEYEVENTF_KEYUP;
        input[0].ui.ki.time = 0;
        input[0].ui.ki.dwExtraInfo = IntPtr.Zero;

        SendInput(input.Length, input, Marshal.SizeOf(input[0]));
}

}
'@
# const definition 
$MOUSEEVENTF_MOVED      = 0x0001 ;
$MOUSEEVENTF_LEFTDOWN   = 0x0002 ;
$MOUSEEVENTF_LEFTUP     = 0x0004 ;
$MOUSEEVENTF_RIGHTDOWN  = 0x0008 ;
$MOUSEEVENTF_RIGHTUP    = 0x0010 ;
$MOUSEEVENTF_MIDDLEDOWN = 0x0020 ;
$MOUSEEVENTF_MIDDLEUP   = 0x0040 ;
$MOUSEEVENTF_WHEEL      = 0x0080 ;
$MOUSEEVENTF_XDOWN      = 0x0100 ;
$MOUSEEVENTF_XUP        = 0x0200 ;
$MOUSEEVENTF_ABSOLUTE   = 0x8000 ;

$Keys = [PSCustomObject]@{
              A = 65
              Shift = 16
}

Add-Type -TypeDefinition $cSource -ReferencedAssemblies System.Windows.Forms,System.Drawing
Start-Sleep -s 11
[Clicker]::MousePoint(300,300)
Start-Sleep -s 11
[Clicker]::MouseClick($MOUSEEVENTF_LEFTDOWN)
[Clicker]::MouseClick($MOUSEEVENTF_LEFTUP)
Start-Sleep -s 11

[Clicker]::SendInputKeyPress($Keys.Shift)
[Clicker]::SendInputKeyPressAndRelease($Keys.A)
[Clicker]::SendInputKeyRelease($Keys.Shift)

Keys 列挙型 (System.Windows.Forms) | Microsoft Learn

bat,PowerShell ,windows

Posted by eightban