4

パワーシェルスクリプトで中マウスクリックを送信するにはどうすればよいですか? 私はこのようなものが欲しい:

Add-Type -AssemblyName System.Windows.Forms
[Windows.Forms.SendKeys]::SendWait('{MButton}')
4

2 に答える 2

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

Function : Click-MouseButton
By       : John Bartels
Date     : 12/16/2012 
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);
    }

}


}

この関数をプロファイルまたは他のスクリプトに追加すると、次のように呼び出すことができます。

Click-MouseButton "中"

于 2012-12-31T01:09:51.840 に答える
3

SendKeysは、マウス入力ではなく、キーボード入力をシミュレートするためのものです。関数を呼び出すためのキーボードメカニズムがある場合は、SendKeysを使用できます。たとえば、コントロール用に設定されたキーボードアクセラレータがある場合は、Alt+«char»を送信できます。タブキーを送信してからスペースキーを送信してボタンをクリックすることができます。

PowerShellを使用して実際にキーを送信する場合は、最初に、キーストロークを送信するウィンドウへのハンドルを取得する必要があります。そのウィンドウハンドルを取得する方法は、ウィンドウの作成方法によって異なります。1つの方法は、Win32APIを使用することFindWindowです。ウィンドウを取得したら、それがフォアグラウンドウィンドウであることを確認する必要があります(別のWin32 API-SetForegroundWindowそれを行います)。その後、キーストロークの送信を開始できます。PowerShell V2以降では、コマンドレットを介して.NETPInvokeメカニズムを介してWin32APIにアクセスできますAdd-Type。例:

Start-Process calc.exe

$pinvokes = @'
  [DllImport("user32.dll", CharSet=CharSet.Auto)]
  public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

  [DllImport("user32.dll")]
  [return: MarshalAs(UnmanagedType.Bool)]
  public static extern bool SetForegroundWindow(IntPtr hWnd);
'@

Add-Type -AssemblyName System.Windows.Forms
Add-Type -MemberDefinition $pinvokes -Name NativeMethods -Namespace MyUtils

$hwnd = [MyUtils.NativeMethods]::FindWindow("CalcFrame", "Calculator")
if ($hwnd)
{
    [MyUtils.NativeMethods]::SetForegroundWindow($hwnd)
    [System.Windows.Forms.SendKeys]::SendWait("6")
    [System.Windows.Forms.SendKeys]::SendWait("*")
    [System.Windows.Forms.SendKeys]::SendWait("7")
    [System.Windows.Forms.SendKeys]::SendWait("=")
}

ウィンドウがWinForms(またはWPF)ベースであり、PowerShellプロセスで実行されている場合、別の可能な方法は、Reflectionを使用して、MButtonClickを送信するコントロールに到達し、OnMouseClickを自分で呼び出すことです。コントロールへの参照が必要ですが、おそらくPowerShellプロセスで実行されている場合は、コントロールを作成したため、コントロールへの参照があります。

WindowsPowerShellを使用したUIオートメーションに関するこのMSDNの記事が役立つ場合があります。マウスとキーボードの入力のシミュレーションに関するMSDNトピックも役立つ可能性があります。

于 2012-08-26T19:49:22.183 に答える