C
C#2y ago
Cloudy

❔ Laptop Lid Close Event?

Anyone know how I can detect when the laptops lid is closed? This is my code so far but it doesn't work, any help is greatly appreciated
static void CreateLidCloseEvent()
{
// Create a new WMI event query to monitor for changes in the state of the lid
WqlEventQuery query = new WqlEventQuery("SELECT * FROM Win32_PowerManagementEvent WHERE EventType = 4");

// Create a new WMI event watcher to watch for the event
ManagementEventWatcher watcher = new ManagementEventWatcher(query);

// Attach an event handler to the watcher
watcher.EventArrived += new EventArrivedEventHandler(LidClosed);

watcher.Start();

Console.WriteLine("Waiting for lid closing event...");
Console.ReadLine();

watcher.Stop();
}

private static void LidClosed(object sender, EventArrivedEventArgs e)
{
Console.WriteLine("Lid closing event detected.");
}
static void CreateLidCloseEvent()
{
// Create a new WMI event query to monitor for changes in the state of the lid
WqlEventQuery query = new WqlEventQuery("SELECT * FROM Win32_PowerManagementEvent WHERE EventType = 4");

// Create a new WMI event watcher to watch for the event
ManagementEventWatcher watcher = new ManagementEventWatcher(query);

// Attach an event handler to the watcher
watcher.EventArrived += new EventArrivedEventHandler(LidClosed);

watcher.Start();

Console.WriteLine("Waiting for lid closing event...");
Console.ReadLine();

watcher.Stop();
}

private static void LidClosed(object sender, EventArrivedEventArgs e)
{
Console.WriteLine("Lid closing event detected.");
}
3 Replies
Jester
Jester2y ago
interresting question doesnt your laptop just fall asleep then or did you disable that?
Jester
Jester2y ago
Stack Overflow
Capturing laptop lid closing event in windows?
I am looking for a way to intercept the laptop lid closing event. In windows 7, the power management allows me to select a desired behavior when the lid is closed. So there must be a way windows kn...
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server