Start-MouseClicker.ps1

function Start-MouseClicker {
    <#
    Performs a left-click at the current mouse location every N seconds.
    Stops automatically when the mouse is moved or any key is pressed.
    #>

    [CmdletBinding()]
    param(
        [Parameter(Position=0)]
        [ValidateRange(5,60)]
        [int]
        $IntervalSeconds = 10,

        [Parameter(Position=1)]
        [ValidateNotNullOrEmpty()]
        [PSCustomObject]
        $TargetPosition
    )

    Write-Output "Starting mouse clicker with interval $IntervalSeconds seconds. Move the mouse or press any key to stop."

    $initial = Get-NativeMousePosition
    $startX = $initial.X; $startY = $initial.Y

    if ($null -ne $TargetPosition) {
        if (-not ($TargetPosition -is [PSCustomObject] -and $TargetPosition.PSObject.Properties['X'] -and $TargetPosition.PSObject.Properties['Y'])) {
            throw "-TargetPosition must be the same object shape returned by Get-MousePosition (has X and Y properties)."
        }
        $targetX = [int]$TargetPosition.X
        $targetY = [int]$TargetPosition.Y
    } else {
        $targetX = $null; $targetY = $null
    }

    while ($true) {
        Start-Sleep -Seconds $IntervalSeconds

        $curr = Get-NativeMousePosition

        if ($curr.X -ne $startX -or $curr.Y -ne $startY) {
            Write-Output "Mouse moved — stopping clicker."
            break
        }

        if (Any-KeyPressed) {
            Write-Output "Key pressed — stopping clicker."
            break
        }

        if ($null -ne $targetX) {
            Click-LeftMouse -X $targetX -Y $targetY
        } else {
            Click-LeftMouse -X $curr.X -Y $curr.Y
        }
    }
}

Export-ModuleMember -Function Start-MouseClicker