Laptop lid status

Just starting out? Need help? Post your questions and find answers here.
paulr
User
User
Posts: 70
Joined: Sat Sep 27, 2003 2:53 pm

Laptop lid status

Post by paulr »

Anyone know how to detect if a laptop's lid is closed or open?

Paul
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post 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.
paulr
User
User
Posts: 70
Joined: Sat Sep 27, 2003 2:53 pm

Post by paulr »

Hmmm, ingenious, but unreliable! Any other suggestions?
delta69
User
User
Posts: 15
Joined: Fri Apr 25, 2003 10:56 pm

Post 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
User avatar
kenmo
Addict
Addict
Posts: 2033
Joined: Tue Dec 23, 2003 3:54 am

Re: Laptop lid status

Post by kenmo »

Bump :)
User avatar
jacdelad
Addict
Addict
Posts: 1991
Joined: Wed Feb 03, 2021 12:46 pm
Location: Riesa

Re: Laptop lid status

Post 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)?
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
paulr
User
User
Posts: 70
Joined: Sat Sep 27, 2003 2:53 pm

Re: Laptop lid status

Post by paulr »

The moment has passed for me, but kudos for answering my question after 19 years!
boddhi
Enthusiast
Enthusiast
Posts: 524
Joined: Mon Nov 15, 2010 9:53 pm

Re: Laptop lid status

Post 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:
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...
User avatar
spikey
Enthusiast
Enthusiast
Posts: 750
Joined: Wed Sep 22, 2010 1:17 pm
Location: United Kingdom

Re: Laptop lid status

Post 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
User avatar
kenmo
Addict
Addict
Posts: 2033
Joined: Tue Dec 23, 2003 3:54 am

Re: Laptop lid status

Post 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.
User avatar
spikey
Enthusiast
Enthusiast
Posts: 750
Joined: Wed Sep 22, 2010 1:17 pm
Location: United Kingdom

Re: Laptop lid status

Post 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?
Post Reply