- 1 year ago
Automatic Shut off Script when User is Idle for too long W10/11 AutoIT au3
To create an AutoIt script to automatically shut off the computer when the user is idle for too long, you can use the DllCall
function to call the Windows API function GetLastInputInfo
to determine the last input time. Here's an example script:
perlGlobal Const $LASTINPUTINFO_SIZE = 8
; Get the last input time
Global $lastInputInfo = DllStructCreate("uint;dword")
DllStructSetData($lastInputInfo, 1, $LASTINPUTINFO_SIZE)
DllCall("user32.dll", "bool", "GetLastInputInfo", "ptr", DllStructGetPtr($lastInputInfo))
; Calculate the idle time in seconds
Global $idleTime = (TimerDiff($lastInputInfo.dword) / 1000)
; Check if the idle time is too long
If ($idleTime > 600) Then ; 10 minutes
Shutdown(0) ; Shut down the computer
EndIf
Func Shutdown($force)
Local $sCommand = "shutdown.exe /s /t 0"
If $force Then $sCommand &= " /f"
Run($sCommand)
EndFunc
In this script, we use DllCall
to call GetLastInputInfo
, which retrieves the time of the last input event. We then calculate the idle time in seconds and check if it is greater than 10 minutes (600 seconds). If it is, we call the Shutdown
function to shut down the computer.
The Shutdown
function uses the Run
function to execute the shutdown.exe
command with the /s
option to shut down the computer and the /t 0
option to shut down immediately. If the $force
parameter is true, it also includes the /f
option to force the shutdown.
To run this script continuously, you can create a loop that sleeps for a certain amount of time (e.g., 1 minute) between checks.