Last active
December 11, 2022 19:15
-
-
Save seakintruth/ab55e008d6bdda78c97d7d1a124d93b0 to your computer and use it in GitHub Desktop.
Powershell Keep Alive Smash Lock Keys
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This can be found/tracked at this public GIST: | |
# https://gist.github.com/seakintruth/ab55e008d6bdda78c97d7d1a124d93b0 | |
# Modified from These: | |
# https://gist.github.com/MatthewSteeples/ce7114b4d3488fc49b6a?permalink_comment_id=3682064 | |
# https://stackoverflow.com/a/15846912 | |
$defaultNumLockStateOff = $false | |
$defaultScrollLockStateOff = $true | |
$defaultCapsLockStateOff = $true | |
Add-Type -AssemblyName System.Windows.Forms | |
Add-Type -Name ConsoleUtils -Namespace WPIA -MemberDefinition @' | |
[DllImport("Kernel32.dll")] | |
public static extern IntPtr GetConsoleWindow(); | |
[DllImport("user32.dll")] | |
public static extern bool ShowWindow(IntPtr hWnd, Int32 nCmdShow); | |
'@ | |
Add-Type @' | |
using System; | |
using System.Diagnostics; | |
using System.Runtime.InteropServices; | |
namespace PInvoke.Win32 { | |
public static class UserInput { | |
[DllImport("user32.dll", SetLastError=false)] | |
private static extern bool GetLastInputInfo(ref LASTINPUTINFO plii); | |
[StructLayout(LayoutKind.Sequential)] | |
private struct LASTINPUTINFO { | |
public uint cbSize; | |
public int dwTime; | |
} | |
public static DateTime LastInput { | |
get { | |
DateTime bootTime = DateTime.UtcNow.AddMilliseconds(-Environment.TickCount); | |
DateTime lastInput = bootTime.AddMilliseconds(LastInputTicks); | |
return lastInput; | |
} | |
} | |
public static TimeSpan IdleTime { | |
get { | |
return DateTime.UtcNow.Subtract(LastInput); | |
} | |
} | |
public static int LastInputTicks { | |
get { | |
LASTINPUTINFO lii = new LASTINPUTINFO(); | |
lii.cbSize = (uint)Marshal.SizeOf(typeof(LASTINPUTINFO)); | |
GetLastInputInfo(ref lii); | |
return lii.dwTime; | |
} | |
} | |
} | |
} | |
'@ | |
## Hide Powershell window (Optionally) | |
# $hWnd = [WPIA.ConsoleUtils]::GetConsoleWindow() | |
# [WPIA.ConsoleUtils]::ShowWindow($hWnd, 0) | |
#if ($hWnd -ne 0){ | |
# [WPIA.ConsoleUtils]::ShowWindow($hWnd, 0) | |
#} | |
Clear-Host | |
#set intial values for variables | |
$WShell = New-Object -com "Wscript.Shell" | |
$p = [System.Windows.Forms.Cursor]::Position | |
Write-Host ("Last Mouse Location " + $($p.X) + ", " + $($p.Y)) | |
$x = $p.X | |
$y = $p.Y | |
$totalCycleCount = 0 | |
$waitCycleCount = 0 | |
while ($true) | |
{ | |
$totalCycleCount += 1 | |
$p = [System.Windows.Forms.Cursor]::Position | |
if ( ` | |
($x -gt $($p.X - 10)) ` | |
-and ($x -lt $($p.X + 10)) ` | |
-and ($y -gt $($p.y -10)) ` | |
-and ($y -lt $($p.y +10)) ` | |
) | |
{ | |
$status = "Smashing locks; Cycles=" + $totalCycleCount + "; " | |
# Randomly smash the numlock, scrollock, or capslock buttons | |
if ($([PInvoke.Win32.UserInput]::IdleTime) -gt 1) | |
{ | |
Switch ( $(Get-Random(3))) | |
{ | |
0 {$WShell.sendkeys("{NUMLOCK}")} | |
1 {$WShell.sendkeys("{SCROLLLOCK}")} | |
2 {$WShell.sendkeys("{CAPSLOCK}")} | |
} | |
} | |
} else { | |
Write-Host ("Stop smashing lock buttons until mouse idle 1 minute; Last Mouse Location" + ` | |
$x + ", " + $y + " New location" + $($p.X) + ", " + $($p.Y)) | |
# Reset the lock keys | |
Start-Sleep -Milliseconds $(Get-Random -Minimum 100 -Maximum 150) | |
if ($([console]::NumberLock) -eq $defaultNumLockState) | |
{ | |
$WShell.sendkeys("{NUMLOCK}") | |
} | |
if ($([System.Windows.Forms.Control]::IsKeyLocked('Scroll')) -eq $defaultScrollLockStateOff) | |
{ | |
$WShell.sendkeys("{SCROLLLOCK}") | |
} | |
if ($([console]::CapsLock) -eq $defaultCapsLockStateOff) | |
{ | |
$WShell.sendkeys("{CAPSLOCK}") | |
} | |
$p = [System.Windows.Forms.Cursor]::Position | |
$x = $p.X | |
$y = $p.Y | |
do | |
{ | |
$status = "Waiting on smashing lock buttons, until timout; Cycles=" + $totalCycleCount + ` | |
"; Wait for Idle Cycles=" + $waitCycleCount + ";" | |
Write-Host -NoNewLine ("`r" + $status) | |
Start-Sleep -Seconds 1 | |
if ( ` | |
($x -gt $($p.X - 10)) ` | |
-and ($x -lt $($p.X + 10)) ` | |
-and ($y -gt $($p.y -10)) ` | |
-and ($y -lt $($p.y +10)) ` | |
) | |
{ | |
# If the mouse has NOT moved more than 10 pixels, x or y increment waitCycleCount | |
$waitCycleCount += 1 | |
} else { | |
# If the mouse has moved more than 10 pixels, x or y then reset waitCycleCount and new x y values | |
$waitCycleCount = 1 | |
$p = [System.Windows.Forms.Cursor]::Position | |
$x = $p.X | |
$y = $p.Y | |
} | |
} Until ($waitCycleCount -gt 60) | |
Write-Host ("Resume smashing lock buttons") | |
} | |
Start-Sleep -Milliseconds $(Get-Random -Minimum 50 -Maximum 100) | |
$x = $p.X | |
$y = $p.Y | |
$idle = [PInvoke.Win32.UserInput]::IdleTime | |
Write-Host -NoNewLine ("`r" + $status + "Idle for " + $idle.Minutes + ":" + $idle.Seconds + "; " + ` | |
"Last Mouse Location " + $($p.X) + ", " + $($p.Y)) | |
} | |
# The following is not incorperated in this implementation, it's a demonstration of how to move the mouse with powershell. | |
# $checkIndex = $plusOrMinusX | |
# while ($checkIndex -gt 0) | |
# { | |
# | |
# # Move mouse | |
# $p = [System.Windows.Forms.Cursor]::Position | |
# $x = $p.X - 1 | |
# $y = $p.Y - 1 | |
# [System.Windows.Forms.Cursor]::Position = New-Object System.Drawing.Point($x, $y) | |
# Start-Sleep -Milliseconds 50 | |
# $checkIndex -=1 | |
# } | |
# # Move mouse | |
# $p = [System.Windows.Forms.Cursor]::Position | |
# $x = $p.X + $plusOrMinusX | |
# $y = $p.Y + $plusOrMinusY | |
# [System.Windows.Forms.Cursor]::Position = New-Object System.Drawing.Point($x, $y) | |
# Start-Sleep -Milliseconds 200 | |
## A example loop that runs anywhere from 10 to 20 times. | |
#for ( $i = 0; $i -lt $(Get-Random -Minimum 10 -Maximum 20); $i++ ) | |
#{ | |
# ACTIONS | |
#} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
REM This can be found/tracked at this public GIST: | |
REM https://gist.github.com/seakintruth/ab55e008d6bdda78c97d7d1a124d93b0 | |
REM This batch file starts the PressLocks.ps1 file found in the same directory | |
@echo off | |
setlocal EnableDelayedExpansion | |
cls | |
SET ThisScriptsDirectory=%~dp0 | |
SET PowerShellScriptPath=%ThisScriptsDirectory%PressLocks.ps1 | |
PowerShell -NoProfile -ExecutionPolicy Bypass -Command "& '%PowerShellScriptPath%'"; | |
exit |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment