Page 1 of 1

Laptop lid status

Posted: Wed Sep 21, 2005 5:37 pm
by paulr
Anyone know how to detect if a laptop's lid is closed or open?

Paul

Posted: Wed Sep 21, 2005 7:44 pm
by Trond
Display "YOU ARE INFECTED WITH A VIRUS" in big blinking letters on the middle of the screen. Then listen for the microphone input. If it records a lot of screaming the lid is open, else it's closed.

Posted: Wed Sep 21, 2005 8:12 pm
by paulr
Hmmm, ingenious, but unreliable! Any other suggestions?

Posted: Fri Sep 23, 2005 2:56 pm
by delta69
Hello !

If I were you, I would have a look at the MS WMI functions... I'm not sure that the one you're looking for exists, but there is a chance.

If I have time, I will have a look.

Regards,
Nicolas

Re: Laptop lid status

Posted: Wed Jun 19, 2024 3:41 am
by kenmo
Bump :)

Re: Laptop lid status

Posted: Wed Jun 19, 2024 4:10 am
by jacdelad

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
Can someone try this on a laptop (I don't own one)?

Re: Laptop lid status

Posted: Wed Jun 19, 2024 5:11 am
by paulr
The moment has passed for me, but kudos for answering my question after 19 years!

Re: Laptop lid status

Posted: Wed Jun 19, 2024 8:16 am
by boddhi
paulr wrote: The moment has passed for me, but kudos for answering my question after 19 years!
Patience is the mother of all virtues, all things come to those who wait for! :mrgreen: :) :wink:

Re: Laptop lid status

Posted: Wed Jun 19, 2024 11:36 am
by spikey
jacdelad wrote: Wed Jun 19, 2024 4:10 am Can someone try this on a laptop (I don't own one)?
Sorry, it doesn't work. :(
This does though: :D

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

Posted: Thu Jun 20, 2024 12:14 am
by kenmo
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

Posted: Fri Jun 21, 2024 12:05 pm
by spikey
kenmo wrote: Thu Jun 20, 2024 12:14 am I had to load "user32.dll" at runtime and CallFunction() instead, for some reason.
I can't explain that one. Just about everything will be using User32! What version of PB and OS architecture are you using?