4

如何使用 power shell 脚本发送鼠标中键?我想要这样的东西:

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);
    }

}


}

您可以将此函数添加到您的个人资料或其他脚本中,然后您可以使用以下命令调用它:

单击鼠标按钮“中间”

于 2012-12-31T01:09:51.840 回答
3

好吧,SendKeys 用于模拟键盘输入而不是鼠标输入。如果有调用该函数的键盘机制,那么您可以使用 SendKeys。例如,如果为控件配置了键盘加速器,则可以发送 Alt+«char»。您可以发送 Tab 键,然后发送空格键来单击按钮。

至于实际使用 PowerShell 发送密钥,首先您必须获得要向其发送击键的窗口的句柄。如何获得该窗口句柄取决于窗口的创建方式。一种方法是使用 Win32 API FindWindow。一旦你有了窗口,你需要确保它是前台窗口(另一个 Win32 API -SetForegroundWindow这样做),然后你可以开始向它发送击键。PowerShell V2 及更高版本允许您通过 .NET PInvoke 机制通过Add-Typecmdlet 访问 Win32 API,例如:

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("=")
}

另一种可能的方法是,如果Window 基于 WinForms(或 WPF)并且它在您的 PowerShell 进程中运行,则使用反射进入您要发送 MButtonClick 的控件并自己调用 OnMouseClick。您需要对控件的引用,但如果它在您的 PowerShell 进程中运行,则可能是您创建了该控件,因此您对它有一个引用。

您可能会发现这篇关于使用 Windows PowerShell 实现 UI 自动化的MSDN 文章很有用。还有一个关于模拟鼠标和键盘输入的 MSDN 主题可能会有所帮助。

于 2012-08-26T19:49:22.183 回答