0
votes

I'm implementing a WPF application in C# 4.5 for Windows 7, and I would like to be able to grab the event when a user presses the Windows Key and C.

I've implemented the OnPreviewKeyDownEvent and this fires as expected (in my code I have some other events that fire off other key presses), however it seems only the Windows Key down fires, and not the second key press. I can see this from the debug.Writeline command.

Any ideas how I capture both the windows Key and C?

private void OnPreviewKeyDownEvent(object sender, KeyEventArgs e)
{
    if ((e.Key == Key.C && (Keyboard.Modifiers & ModifierKeys.Windows) == ModifierKeys.Windows) && e.IsDown)
       Messagbox.Show("Pressed1");

    if (e.Key == Key.C && (Keyboard.IsKeyDown(Key.LWin) || Keyboard.IsKeyDown(Key.RWin)))
            Messenger.Default.Send(new RightClickMessage())
         Messagbox.Show("Pressed2");


        System.Diagnostics.Debug.WriteLine(e.Key + " " + Keyboard.IsKeyDown(Key.LWin) + " ");
}

Many thanks in advance for your help.

1
i think you need to use windows api and make a hook to intercept windows keyRang

1 Answers

1
votes

here is how you can detect Win + C

    private void OnPreviewKeyDownEvent(object sender, KeyEventArgs e)
    {
        if ((Keyboard.IsKeyDown(Key.LWin) || Keyboard.IsKeyDown(Key.RWin)) && Keyboard.IsKeyDown(Key.C))
        {
            // Win + C 
        }
    }

or

    private void OnPreviewKeyDownEvent(object sender, KeyEventArgs e)
    {
        if (((Keyboard.GetKeyStates(Key.LWin) & KeyStates.Down) == KeyStates.Down ||
            (Keyboard.GetKeyStates(Key.RWin) & KeyStates.Down) == KeyStates.Down) &&
            (Keyboard.GetKeyStates(Key.C) & KeyStates.Down) == KeyStates.Down)
        {
            // Win + C 
        }
    }