Laptop lid status
Laptop lid status
Anyone know how to detect if a laptop's lid is closed or open?
Paul
Paul
Re: Laptop lid status
Code: Select all
Procedure Override(WindowID, Message, WParam, LParam)
Result = #PB_ProcessPureBasicEvents
If Message=#WM_SYSCOMMAND
If (wParam&$FFF0)=#SC_MONITORPOWER
Debug "Monitor power has changed"
Select LParam
Case -1
Debug "Monitor is on"
Case 1
Debug "Monitor is on standby"
Case 2
Debug "Monitor is off"
Default
Debug "Unknown"
EndSelect
EndIf
EndIf
ProcedureReturn Result
EndProcedure
OpenWindow(0,0,0,400,300,"Test",#PB_Window_ScreenCentered|#PB_Window_SystemMenu)
SetWindowCallback(@Override(),0)
Repeat
Until WaitWindowEvent()=#PB_Event_CloseWindow
Good morning, that's a nice tnetennba!
PureBasic 6.21/Windows 11 x64/Ryzen 7900X/32GB RAM/3TB SSD
Synology DS1821+/DX517, 130.9TB+50.8TB+2TB SSD
PureBasic 6.21/Windows 11 x64/Ryzen 7900X/32GB RAM/3TB SSD
Synology DS1821+/DX517, 130.9TB+50.8TB+2TB SSD
Re: Laptop lid status
The moment has passed for me, but kudos for answering my question after 19 years!
Re: Laptop lid status
Patience is the mother of all virtues, all things come to those who wait for!paulr wrote: The moment has passed for me, but kudos for answering my question after 19 years!



If my English syntax and lexicon are incorrect, please bear with Google translate and DeepL. They rarely agree with each other!
Except on this sentence...
Except on this sentence...
Re: Laptop lid status
Sorry, it doesn't work.

This does though:

Code: Select all
#DEVICE_NOTIFY_WINDOW_HANDLE = 0
#DEVICE_NOTIFY_SERVICE_HANDLE = 1
Structure POWERBROADCAST_SETTING
PowerSetting.GUID
DataLength.L
Bytes.B[1]
EndStructure
Import "User32.lib"
RegisterPowerSettingNotification.I(hRecipient.L, *PowerSettingGuid, Flags.L)
UnregisterPowerSettingNotification(hPowerNotify.L)
EndImport
Macro DEFINE_GUID(name, l, w1, w2, b1, b2, b3, b4, b5, b6, b7, b8)
Global name.GUID
name\Data1 = l
name\Data2 = w1
name\Data3 = w2
name\Data4[0] = b1
name\Data4[1] = b2
name\Data4[2] = b3
name\Data4[3] = b4
name\Data4[4] = b5
name\Data4[5] = b6
name\Data4[6] = b7
name\Data4[7] = b8
EndMacro
; See https://learn.microsoft.com/en-us/windows/win32/power/power-setting-guids
DEFINE_GUID(GUID_LIDSWITCH_STATE_CHANGE, $BA3E0F4D, $B817, $4094, $A2, $D1, $D5, $63, $79, $E6, $A0, $F3)
Define hNotification.I
Procedure WinCallback(hWnd, uMsg, WParam, LParam)
; Windows fills the parameter automatically, which we will use in the callback...
Protected.S Message
Protected.L State
Protected *PBS.POWERBROADCAST_SETTING
If uMsg = #WM_POWERBROADCAST And Wparam = #PBT_POWERSETTINGCHANGE
*PBS = LParam
State = PeekL(@*PBS\Bytes)
Select State
Case 0
Message = "#WM_POWERBROADCAST - #PBT_POWERSETTINGCHANGE - Lid is closed."
Case 1
Message = "#WM_POWERBROADCAST - #PBT_POWERSETTINGCHANGE - Lid is open."
EndSelect
AddGadgetItem(0, -1, Message)
EndIf
If uMsg = #WM_SIZE
Select WParam
Case #SIZE_MINIMIZED
Message = "Window was minimized"
Case #SIZE_RESTORED
Message = "Window was restored"
Case #SIZE_MAXIMIZED
Message = "Window was maximized"
EndSelect
AddGadgetItem(0, -1, Message)
EndIf
ProcedureReturn #PB_ProcessPureBasicEvents
EndProcedure
If OpenWindow(0, 0, 0, 500, 400, "Messages", #PB_Window_MinimizeGadget | #PB_Window_MaximizeGadget)
ListViewGadget(0, 5, 5, 490, 390)
hNotification = RegisterPowerSettingNotification(WindowID(0), @GUID_LIDSWITCH_STATE_CHANGE, #DEVICE_NOTIFY_WINDOW_HANDLE)
SetWindowCallback(@WinCallback(), 0) ; set the callback
Repeat
Select WaitWindowEvent()
Case #PB_Event_CloseWindow
Break
EndSelect
ForEver
UnregisterPowerSettingNotification(hNotification)
EndIf
Re: Laptop lid status
Thanks all! Spikey, that mostly works for me, except the Imports fail, I had to load "user32.dll" at runtime and CallFunction() instead, for some reason.
Re: Laptop lid status
I can't explain that one. Just about everything will be using User32! What version of PB and OS architecture are you using?kenmo wrote: Thu Jun 20, 2024 12:14 am I had to load "user32.dll" at runtime and CallFunction() instead, for some reason.