简体   繁体   English

Powershell,鼠标点击

[英]Powershell, mouse click

How would I perform mouse click using PowerShell?我将如何使用 PowerShell 执行鼠标单击?

I created $KeyBoard and $mouse , but not sure how to make the mouse click the left button once the cursor is in position.我创建了$KeyBoard$mouse ,但不确定如何在光标就位后让鼠标单击左键。

$KeyBoard = New-Object -ComObject wscript.shell
$mouse = [System.Windows.Forms.Cursor]::Position = New-Object System.Drawing.Point(675,150)

Check here: http://www.daveamenta.com/2013-08/use-powershell-to-automatically-click-a-point-on-the-screen-reapeatedly-auto-clicker/ 在此处查看: http : //www.daveamenta.com/2013-08/use-powershell-to-automatically-click-a-point-on-the-screen-reapeatedly-auto-clicker/

The author provides a relatively concise function for repeated mouse clicks. 作者为重复单击鼠标提供了一个相对简洁的功能。 I don't want to copy-paste his work here without his permission, but it would be easy to re-purpose the custom type he creates and use the static click methods. 我不想在未经他许可的情况下在此处复制粘贴他的作品,但是很容易重新利用他创建的自定义类型并使用静态点击方法。

For reference, I'm adding the information from the deleted website (archive.org):作为参考,我添加了已删除网站 (archive.org) 中的信息:

To use it, move the mouse to the point on the screen where the click is desired, and then use the keyboard to start the script in a PowerShell window.要使用它,请将鼠标移动到屏幕上需要单击的位置,然后使用键盘在 PowerShell 窗口中启动脚本。 To stop the clicking, press the Windows key on the keyboard.要停止单击,请按键盘上的 Windows 键。 Note that this requires Windows 8 and will not work on Windows 7 or below.请注意,这需要 Windows 8,并且不能在 Windows 7 或更低版本上运行。

Interval between clicks, Left/Right button and the ability to set “move to point before clicking” are supported.支持点击间隔、左/右按钮和设置“点击前移动到点”的功能。

[CmdletBinding()]
param($Interval = 5000, [switch]$RightClick, [switch]$NoMove)
 
[Reflection.Assembly]::LoadWithPartialName("System.Drawing") | Out-Null
$DebugViewWindow_TypeDef = @'
[DllImport("user32.dll")]
public static extern IntPtr FindWindow(string ClassName, string Title);
[DllImport("user32.dll")]
public static extern IntPtr GetForegroundWindow();
[DllImport("user32.dll")]
public static extern bool SetCursorPos(int X, int Y);
[DllImport("user32.dll")]
public static extern bool GetCursorPos(out System.Drawing.Point pt);
 
[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);
 
private const int MOUSEEVENTF_LEFTDOWN = 0x02;
private const int MOUSEEVENTF_LEFTUP = 0x04;
private const int MOUSEEVENTF_RIGHTDOWN = 0x08;
private const int MOUSEEVENTF_RIGHTUP = 0x10;
 
public static void LeftClick(){
    mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
}
 
public static void RightClick(){
    mouse_event(MOUSEEVENTF_RIGHTDOWN | MOUSEEVENTF_RIGHTUP, 0, 0, 0, 0);
}
'@
Add-Type -MemberDefinition $DebugViewWindow_TypeDef -Namespace AutoClicker -Name Temp -ReferencedAssemblies System.Drawing
 
$pt = New-Object System.Drawing.Point
if ([AutoClicker.Temp]::GetCursorPos([ref]$pt)) {
    Write-host "Clicking at $($pt.X), $($pt.Y) every ${Interval}ms until Ctrl^C or " -NoNewline
    Write-Host -ForegroundColor Cyan "Start " -NoNewline
    Write-Host "is open."
    while($true) {
        $start = [AutoClicker.Temp]::FindWindow("ImmersiveLauncher", "Start menu")
        $fg = [AutoClicker.Temp]::GetForegroundWindow()
 
        if ($start -eq $fg) { 
            Write-Host "Start opened. Exiting"
            return 
        }
 
        if (!$NoMove) {
            [AutoClicker.Temp]::SetCursorPos($pt.X, $pt.Y) | Out-Null
        }
 
        if ($RightClick) {
            [AutoClicker.Temp]::RightClick()
        } else {
            [AutoClicker.Temp]::LeftClick()
        }
        sleep -Milliseconds $Interval
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM