Turning off numlock when locking PC, turning on numlock when unlocking PC

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
bravoman21
Posts: 1
Joined: 16 Jan 2022, 17:43

Turning off numlock when locking PC, turning on numlock when unlocking PC

Post by bravoman21 » 16 Jan 2022, 17:55

So I'm having an issue with my script, which was working fine for a period of time and just recently decided to quit working. I piecemealed together the code below so that it would turn off my numlock when I locked my PC and turn it back on when I unlocked the PC. One other thing if we can get this figured out, I would like to add a function to turn off the numlock when the PC goes idle and turns off my monitors. When the PC goes idle it will lock itself, but leaves the numlock on. So I assume that I would have to detect a different change/action for when the PC goes into idle which would trigger turning off the numlock.

Any ideas?

Code: Select all

DetectHiddenWindows, on
hw_ahk := WinExist("ahk_pid " DllCall("GetCurrentProcessId"))
DetectHiddenWindows, off

WM_WTSSESSION_CHANGE = 0x02B1
OnMessage( WM_WTSSESSION_CHANGE, "HandleMessage" )

NOTIFY_FOR_THIS_SESSION = 0
result := DllCall( "Wtsapi32.dll\WTSRegisterSessionNotification", "uint", hw_ahk, "uint", NOTIFY_FOR_ALL_SESSIONS )
return

HandleMessage( p_w, p_l, p_m, p_hw )
{
   if ( p_w = 0x7 )
   {
      SetNumLockState, Off
   }
   else if ( p_w = 0x8 )
   {
      SetNumLockState, On
   }
}

User avatar
mikeyww
Posts: 26848
Joined: 09 Sep 2014, 18:38

Re: Turning off numlock when locking PC, turning on numlock when unlocking PC

Post by mikeyww » 16 Jan 2022, 20:33

Code: Select all

#InstallKeybdHook
#InstallMouseHook

F3:: ; F3 = Lock workstation
SetNumLockState, Off
SoundBeep, 1500
DllCall("User32.dll\LockWorkStation")
Sleep, 3000
SetTimer, Check, 500
Check:
If !DllCall("User32\OpenInputDesktop", "Int", 0, "Int", 0, "Int", 0x0001L*1) ; Locked
 Return
SetTimer, Check, Off
SetNumLockState, On
SoundBeep, 1000
Return

F4::
SetTimer, Idle, 500
SoundBeep, 1500
Idle:
If !GetKeyState("NumLock", "T") || A_TimeIdlePhysical < 1500
 Return
SetNumLockState, Off
SoundBeep, 1000
Return

Post Reply

Return to “Ask for Help (v1)”