Thursday, November 27, 2025

One thousand times left click in PowerShell

 Add-Type -AssemblyName System.Windows.Forms

Add-Type -AssemblyName System.Drawing


function Send-MouseClick {

    # Define the necessary Win32 API function for mouse events

    $signature = @'

    [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]

    public static extern void mouse_event(uint dwFlags, uint dx, uint dy, uint cButtons, uint dwExtraInfo);

'@


    $mouseEvents = Add-Type -MemberDefinition $signature -Name "Win32MouseEvents" -Namespace "Windows" -PassThru


    # Mouse event flags

    $MOUSEEVENTF_LEFTDOWN = 0x0002

    $MOUSEEVENTF_LEFTUP = 0x0004


    # Get current cursor position

    $cursorPos = [System.Windows.Forms.Cursor]::Position

    $x = $cursorPos.X

    $y = $cursorPos.Y


    # Perform the left mouse button down and up events

    $mouseEvents::mouse_event($MOUSEEVENTF_LEFTDOWN, $x, $y, 0, 0)

    $mouseEvents::mouse_event($MOUSEEVENTF_LEFTUP, $x, $y, 0, 0)

}


# Loop 1000 times to perform 1000 clicks

for ($i = 0; $i -lt 1000; $i++) {

    Send-MouseClick

    # Optional: Add a small delay (e.g., 10 milliseconds) to prevent overwhelming the system

    Start-Sleep -Milliseconds 10

}


Write-Host "1000 clicks performed."


No comments:

Post a Comment