views:

113

answers:

3

Is it possible to detect when a laptop's lid is open or closed? From what I've read, this isn't possible, but SO has helped me with the impossible before.

The only thing I've found that might be in the right direction is an MSDN blog post about IOCTLs needed to report power buttons. Is it possible to "sniff" these as the OS calls them?

I'm using VB.NET, but will take suggestions in any language. Thank you for your time and advice.

Edit: My software will be (eventually) overriding the actions (based on user preference) that occur when the lid is closed, so listening for suspend and other actions that typically occur when the lid is closed isn't an option.

A: 

Power Managment

Handling Power-Saving Events The focus so far has been on conserving the battery life while your application is running. There is an additional consideration that you should make: how your application behaves when the computer suspends operation. There are two key scenarios to consider here:

  • When the computer is idle for a certain period of time, the active Power Scheme is likely to specify that the hardware either goes into Stand By or Hibernate mode.
  • When the user takes an action that puts the computer into a suspend operation, such as shutting the lid of a laptop or pressing the power button.

I hope it give u some direction :)

lukas
C'mon man, CTRL+C, CTRL+V, no insight?
bobobobo
+1 for CTRL+C, CTRL+V
Hamish Grubijan
Sorry, suspend is not an appropriate trigger in my case.
Brad
+2  A: 

Use WM_POWERBROADCAST. Here's a link that can help you: Lid Close Action change notification

Zabba
Thanks Zabba! This is exactly what I was looking for, but it seems it will only work for Vista and 7. Any thoughts out there for something equivalent in XP? Thanks again.
Brad
It will work on Windows 2000 and greater - http://msdn.microsoft.com/en-us/library/aa373247%28VS.85%29.aspx
Zabba
The RegisterPowerSettingNotification step requires Vista or higher.
Ben Voigt
+3  A: 

Keep in mind that most laptops, when the lid closes, it depresses a button. This button is usually just a sleep button. The WMI classes expose the ACPI and you would ideally want to use the PowerManagement Class. Unfortunately, the class does not raise an event when the operating system is set to "do nothing". The only way around this would be to use the DDK (Driver Development Kit) to create a filter that intercepts the IOCTL_GET_SYS_BUTTON_EVENT event. Here are two links to help you get started:

http://blogs.msdn.com/b/doronh/archive/2006/09/08/746834.aspx

and

http://support.microsoft.com/kb/302092

icemanind
Thanks, this is helpful.
Brad