Click here to Skip to main content
14,774,396 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i want to detect power,sleep,wake up , keys of keyboard whenever they are pressed
i used this keyboardlistener class
but it detect all keys except those that i want!
i want to write a program that detect this 3 keys and do volume up/down and mute/unmute
my keyboard do not have keys for volume control

What I have tried:

i used some global keyboard hook that i found in the internet but none of them detect this keys so how windows detect them to shutdown the computer!
Posted
Updated 1-Sep-20 10:37am
v7

1 solution

You would need to track events like:
SystemEvents.PowerModeChanged Event (Microsoft.Win32) | Microsoft Docs[^]
SystemEvents.SessionEnding Event (Microsoft.Win32) | Microsoft Docs[^]

Example:
private static int WM_QUERYENDSESSION = 0x11;
private static bool systemShutdown = false;
protected override void WndProc(ref System.Windows.Forms.Message m)
{
    if (m.Msg==WM_QUERYENDSESSION)
    {
        MessageBox.Show("queryendsession: this is a logoff, shutdown, or reboot");
        systemShutdown = true;
    }

    // If this is WM_QUERYENDSESSION, the closing event should be
    // raised in the base WndProc.
    base.WndProc(ref m);

} //WndProc 

private void Form1_Closing(
    System.Object sender, 
    System.ComponentModel.CancelEventArgs e)
{
    if (systemShutdown)
        // Reset the variable because the user might cancel the 
        // shutdown.
    {
        systemShutdown = false;
        if (DialogResult.Yes==MessageBox.Show("My application", 
            "Do you want to save your work before logging off?", 
            MessageBoxButtons.YesNo))
        {
            e.Cancel = true;
        }
        else
        {
            e.Cancel = false;
        }
    }
}
   
v2
Comments
BillWoodruff 31-Aug-20 1:15am
   
+5
Sandeep Mewara 31-Aug-20 1:45am
   
Thanks!
4L4K1 1-Sep-20 18:06pm
   
thank u so much +5
when i put shutdown to do nothing in power setting in win 10 i cannot detect this event
i want to write a program that detect this 3 keys and do volume up/down and mute/unmute
my keyboard do not have keys for volume control

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900