Skip to content

Instantly share code, notes, and snippets.

@MatthewSteeples
Created February 26, 2015 19:09
Show Gist options
  • Save MatthewSteeples/ce7114b4d3488fc49b6a to your computer and use it in GitHub Desktop.
Save MatthewSteeples/ce7114b4d3488fc49b6a to your computer and use it in GitHub Desktop.
Powershell Script to keep the mouse moving
Add-Type -AssemblyName System.Windows.Forms
while ($true)
{
$Pos = [System.Windows.Forms.Cursor]::Position
$x = ($pos.X % 500) + 1
$y = ($pos.Y % 500) + 1
[System.Windows.Forms.Cursor]::Position = New-Object System.Drawing.Point($x, $y)
Start-Sleep -Seconds 10
}
@danielhall95
Copy link

danielhall95 commented Feb 28, 2023

I like the heartbeat version with the indicator light. Used some of the above code and also preferred the CAPSLOCK light to remind me that it's running in the background as my laptop doesn't have a full-size keyboard. Lastly, added the ability to quit based on GetKeyState using quonic's code above, with a reset state in the beginning so you don't have to toggle PrtSc manually.

#
# -- NoSleep --
# Keep your computer awake by programmatically pressing the CAPSLOCK key every X seconds
#
param($sleep = 3) # seconds
$announcementInterval = 10 # loops



Clear-Host
$key = 0x2C
$printScreenState = [int][WeakShell.Keyboard]::GetKeyState($key) -band 0xffff
Write-Output "PrintScreen Key State: $printScreenState"
Write-Output ""
#Start-Sleep -Milliseconds 100

if ($printScreenState -eq 1){
    Write-Output "Resetting PrintScreenState.."
    Start-Sleep -Milliseconds 200
    [System.Windows.Forms.SendKeys]::SendWait("{PRTSC}") #toggle PrintScreen On
    Start-Sleep -Milliseconds 500
    [System.Windows.Forms.SendKeys]::SendWait("{ESC}")
     #toggle PrintScreen On
    [System.Windows.Forms.SendKeys]::SendWait("{ESC}") #toggle PrintScreen Off
    Write-Output "PrintScreenState Reset..";Write-Output ""; $printScreenState = [int][WeakShell.Keyboard]::GetKeyState($key) -band 0xffff
    Write-Output "PrintScreen Key State: $printScreenState";Write-Output ""
}

Write-Output "Starting Show As Active!"
Write-Host "Executing Print Screen-toggle NoSleep routine. Runs every $sleep seconds..."
Write-Host "Start time:" $(Get-Date -Format "dddd MM/dd HH:mm (K)")
Write-Output "Press Print Screen to quit."

$WShell = New-Object -com "Wscript.Shell"
$date = Get-Date -Format "dddd MM/dd HH:mm (K)"
$TimeToQuit = $false
$Signature = @'
    [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true, CallingConvention = CallingConvention.Winapi)] 
    public static extern short GetKeyState(int keyCode);
'@
Add-Type -AssemblyName System.Windows.Forms
#Add-Type -MemberDefinition $Signature -Name Keyboard -Namespace WeakShell



# Some environments don't support invocation of this method.
try {
    $stopwatch = [system.diagnostics.stopwatch]::StartNew()
} catch {
   Write-Host "Couldn't start the stopwatch."
}



#Write-Host "<3" -fore red

$index = 0
do{
    
    $printScreenState = [int][WeakShell.Keyboard]::GetKeyState($key) -band 0xffff
    
    #for Key State: https://learn.microsoft.com/en-us/windows/win32/inputdev/virtual-key-codes 
    if ($printScreenState -eq 0) {
        
        Write-Host "< 3" -fore red      # heartbeat
        $WShell.sendkeys("{CAPSLOCK}")

        Start-Sleep -Milliseconds 200

        $WShell.sendkeys("{CAPSLOCK}")
        Write-Host "<3" -fore red       # heartbeat

        # Move mouse
        #$Pos = [System.Windows.Forms.Cursor]::Position; $Pos
        #$random = Get-Random -Minimum -100 -Maximum 100
        #$x = ($pos.X % 500) + $random
        #$y = ($pos.Y % 500) + $random
        #[System.Windows.Forms.Cursor]::Position = New-Object System.Drawing.Point($x, $y)

        Start-Sleep -Seconds $sleep
    }
    else {
        $TimeToQuit = $true
        Write-Output "Print Screen on, quitting"
    }
    
}while ($TimeToQuit -eq $false)

@IndieKid28
Copy link

Add-Type -AssemblyName System.Windows.Forms

while ($true)
{
$Pos = [System.Windows.Forms.Cursor]::Position
$x = ($pos.X % 500) + 1
$y = ($pos.Y % 500) + 1
[System.Windows.Forms.Cursor]::Position = New-Object System.Drawing.Point($x, $y)
Start-Sleep -Seconds 10
}

I ended up running this script through powershell and now my pc won't sleep/screen won't turn off at all. Is there any way to undo this?

@quonic
Copy link

quonic commented Sep 21, 2023

Add-Type -AssemblyName System.Windows.Forms

while ($true) { $Pos = [System.Windows.Forms.Cursor]::Position $x = ($pos.X % 500) + 1 $y = ($pos.Y % 500) + 1 [System.Windows.Forms.Cursor]::Position = New-Object System.Drawing.Point($x, $y) Start-Sleep -Seconds 10 }

I ended up running this script through powershell and now my pc won't sleep/screen won't turn off at all. Is there any way to undo this?

Close the powershell window. This is why I check for CapsLock, so when I press CapsLock it stops the script with ease.

@IndieKid28
Copy link

Add-Type -AssemblyName System.Windows.Forms
while ($true) { $Pos = [System.Windows.Forms.Cursor]::Position $x = ($pos.X % 500) + 1 $y = ($pos.Y % 500) + 1 [System.Windows.Forms.Cursor]::Position = New-Object System.Drawing.Point($x, $y) Start-Sleep -Seconds 10 }
I ended up running this script through powershell and now my pc won't sleep/screen won't turn off at all. Is there any way to undo this?

Close the powershell window. This is why I check for CapsLock, so when I press CapsLock it stops the script with ease.

Thanks for the reply. In my case, when i run the script (right click and run with powershell) the window opens for a split second and then disappears, and it doesn't even show up in the task manager.

@quonic
Copy link

quonic commented Sep 21, 2023

Thanks for the reply. In my case, when i run the script (right click and run with powershell) the window opens for a split second and then disappears, and it doesn't even show up in the task manager.

Reboot. Then just use my code. When you press Caps Lock it will quit the script.

@IndieKid28
Copy link

IndieKid28 commented Sep 21, 2023

Thanks for the reply. In my case, when i run the script (right click and run with powershell) the window opens for a split second and then disappears, and it doesn't even show up in the task manager.

Reboot. Then just use my code. When you press Caps Lock it will quit the script.

Will this be detected by any monitoring app? Like powershell running or windows desktop runtime etc. ? Also your script doesn't seem to work for me. The screen keeps turning off regardless.

EDIT: strangely enough, the starting piece of code which was working earlier doesn't work anymore either.

@IndieKid28
Copy link

Use my script instead.

Are you sure the script is really what's holding up your shutdown? I do not believe it would.

Furthermore, are you sure the script is really running? Try opening PS as admin and running Set-ExecutionPolicy -ExecutionPolicy RemoteSigned CurrentUser (or LocalMachine). If that runs successfully, close PS and try again.

Will this be detected by any monitoring app? Like powershell running or windows desktop runtime etc. ? Also your script doesn't seem to work for me. The screen keeps turning off regardless.

The purpose of this script is not so you can sit AFK your entire day at work and pretend otherwise. It's so that when you go to the bathroom or whatever your VPN session doesn't lock on you, or when you talk to someone near your computer it doesn't lock on you. If your IT or whomever department "catches" you doing THAT, tell them to go to Hell.

Is this implementation mimicking mouse movement or is it just sending keystrokes? Because the sending keystrokes scripts don't seem to make me not show up as afk. I would need a mouse mover script.

@IndieKid28
Copy link

@AndrewDavis Who asked? Also judging by how you called someone a "retard" for suggesting something, I'm sure you're the last person to be taking life advices from lmao. Grow up clown.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment