Created
July 1, 2018 22:36
-
-
Save 9999years/87dc909fa453111ba10a89cb942a5700 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| <# | |
| .DESCRIPTION | |
| Moves the mouse cursor 1 pixel to the right and then back if the user hasn't | |
| touched the computer in a bit | |
| .PARAM CheckInterval | |
| Interval between checks for idle-ness, in seconds. Default: 9.75 minutes | |
| .PARAM MaxIdleTime | |
| Maximum time since last input to trigger a mouse move, in seconds. Default: 9 | |
| minutes. | |
| #> | |
| [CmdletBinding()] | |
| Param( | |
| [Float]$CheckInterval=585, | |
| [Float]$MaxIdleTime=540 | |
| ) | |
| # garbage from SO https://stackoverflow.com/a/15846912/5719760 | |
| 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; | |
| } | |
| } | |
| } | |
| } | |
| '@ | |
| Add-Type -AssemblyName System.Windows.Forms | |
| # general idea from https://gist.github.com/MatthewSteeples/ce7114b4d3488fc49b6a | |
| # you could also use something like: | |
| # [System.Windows.Forms.SendKeys]::SendWait("{NUMLOCK}{NUMLOCK}") | |
| # to "type" numlock twice | |
| function MoveCursor { | |
| [CmdletBinding()] | |
| Param( | |
| [int]$dx=0, | |
| [int]$dy=0 | |
| ) | |
| Process { | |
| $pos = [System.Windows.Forms.Cursor]::Position | |
| [System.Windows.Forms.Cursor]::Position = | |
| [System.Drawing.Point]::new( | |
| $pos.X + $dx, | |
| $pos.Y + $dy | |
| ) | |
| } | |
| } | |
| Write-Verbose "Assemblies loaded, checking idleness" | |
| while($True) { | |
| $time = [PInvoke.Win32.UserInput]::IdleTime | |
| Write-Verbose "$($time.Seconds) seconds since last user input" | |
| If($time.Seconds -gt $MaxIdleTime) { | |
| # no movement in <X> amount of time | |
| # NOTE: i'm not sure if this mouse movement is too fast to | |
| # actually register as input; might have to put a `sleep 1` in | |
| # the middle | |
| Write-Verbose "Moving mouse to prevent sleep" | |
| MoveCursor 1 | |
| MoveCursor -1 | |
| } | |
| sleep $CheckInterval | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment