Jump to content

Lock my pc


Recommended Posts

When I leave my desk, and forget to lock my pc, someone is often writing stuff on my pc...
So here's a script that locks it after 5 seconds of inactivity, you can always increase it.

#include <Misc.au3>

Global $iIdleThreshold = 20000 ; 20 seconds in milliseconds

While True
    Local $iLastInputTime = TimerInit()
    Local $bActivityDetected = False

    While TimerDiff($iLastInputTime) < $iIdleThreshold
        For $i = 0x01 To 0xFF
            If _IsPressed(Hex($i, 2)) Then
                $bActivityDetected = True
                ExitLoop
            EndIf
        Next

        ; Get initial mouse position
        $aInitialMousePos = MouseGetPos()

        ; Allow a small delay to detect mouse movement
        Sleep(50)

        ; Check for mouse movement
        $aCurrentMousePos = MouseGetPos()
        If $aInitialMousePos[0] <> $aCurrentMousePos[0] Or $aInitialMousePos[1] <> $aCurrentMousePos[1] Then
            $bActivityDetected = True
            ExitLoop
        EndIf

        Sleep(100) ; Adjust the sleep time as needed for better responsivenesds
    WEnd

    If $bActivityDetected Then
        ConsoleWrite("Keyboard or mouse activity detected." & @CRLF)
    Else
        ConsoleWrite("No keyboard or mouse activity detected." & @CRLF)
        ShellExecute("rundll32.exe", 'user32.dll,LockWorkStation')
    EndIf
WEnd

 

Edited by legend
Link to comment
Share on other sites

  • Moderators

Lol, I used to do that to people!!!  Turn their screens upside down too 😂

Global $iIdleThreshold = 20000 ; 5 seconds in milliseconds

That's 20 seconds btw...

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...