powershell mouse move does not prevent idle mode

后端 未结 8 1767
猫巷女王i
猫巷女王i 2021-01-30 09:36

Before I start, here is my very first little code I wrote in PowerShell :)

[System.Windows.Forms.Cursor]::Position = `
    New-Object System.Drawing.Point($pos.X         


        
8条回答
  •  礼貌的吻别
    2021-01-30 09:59

    I've added a notification that you can easily enable / disable just setting its variable to $true or $false. Also the mouse cursor moves 1 px right and then 1 px left so it basically stays in the same place even after several iterations.

    # Lines needed for the notification
    [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
    Add-Type -AssemblyName System.Windows.Forms 
    $isNotificationOn = $true
    
    $secondsBetweenMouseMoves = 6
    $Pos = [System.Windows.Forms.Cursor]::Position
    $PosDelta = 1
    $logFilename = "previousMouseMoverAction.txt"
    $errorLogFilename = "mouseMoverLog.txt"
    
    if (!(Test-Path "$PSScriptRoot\$logFilename")) {
       New-Item -path $PSScriptRoot -name $logFilename -type "file" -value "right"
       Write-Host "Warning: previousMouseMoverAction.txt missing, created a new one."
    }
    
    $previousPositionChangeAction = Get-Content -Path $PSScriptRoot\$logFilename
    
    if ($previousPositionChangeAction -eq "left") {
        $PosDelta = 1
        Set-Content -Path $PSScriptRoot\$logFilename -Value 'right'
    } else {
        $PosDelta = -1
        Set-Content -Path $PSScriptRoot\$logFilename -Value 'left'
    }
    
    for ($i = 0; $i -lt $secondsBetweenMouseMoves; $i++) {
        [System.Windows.Forms.Cursor]::Position = New-Object System.Drawing.Point((($Pos.X) + $PosDelta) , $Pos.Y)
        if ($isNotificationOn) {
            # Sending a notification to the user
            $global:balloon = New-Object System.Windows.Forms.NotifyIcon
            $path = (Get-Process -id $pid).Path
            $balloon.Icon = [System.Drawing.Icon]::ExtractAssociatedIcon($path) 
            $balloon.BalloonTipIcon = [System.Windows.Forms.ToolTipIcon]::Warning 
            $balloon.BalloonTipText = 'I have just moved your cheese...'
            $balloon.BalloonTipTitle = "Attention, $Env:USERNAME" 
            $balloon.Visible = $true 
            $balloon.ShowBalloonTip(3000)
        }
    }
    

提交回复
热议问题