Lock Windows on laptop lid close

Post your working scripts, libraries and tools
GeekDude
Posts: 853
Joined: 02 Oct 2013, 22:13

Lock Windows on laptop lid close

02 Jan 2015, 15:32

Or do anything else, really. I built this after poking around and reading quite a bit on the old forum. Kudos to LinearSpoon for the original solution.

Code: Select all

; Build the LIDSWITCH_STATE_CHANGE GUID
VarSetCapacity(GUID_LIDSWITCH_STATE_CHANGE, 16)
for each, Number in [0xBA3E0F4D, 0x4094B817, 0x63D5D1A2, 0xF3A0E679]
	NumPut(Number, GUID_LIDSWITCH_STATE_CHANGE, 4*(A_Index-1), "UInt")

; Register the hooks
DllCall("RegisterPowerSettingNotification"
, "Ptr", A_ScriptHwnd
, "Ptr", &GUID_LIDSWITCH_STATE_CHANGE
, "UInt", 0)
OnMessage(0x218, "WM_POWERBROADCAST")

WM_POWERBROADCAST(wParam, lParam, Msg, hWnd)
{
	GUID := NumGet(lParam+0, 0, "UInt")
	if (GUID != 0xBA3E0F4D) ; Does not start with lidswitch GUID
		return
	
	Open := NumGet(lParam+0, 20, "UChar") ; 1 or 0
	if Open
	{
		; Opening the lid
		SoundBeep, 750
	}
	else
	{
		; Closing the lid
		SoundBeep, 500
		DllCall("LockWorkStation") ; Lock your session (Win-L)
	}
}

Return to “Scripts and Functions”

Who is online

Users browsing this forum: AHK_user, TheBestPessimist and 59 guests