detect Ctrl + Enter

Obviously e.Key can't be equal to more than one different value in the same event.

You need to handle one of the events that uses KeyEventArgs, there you'll find properties such as Control and Modifiers that will help you detect combinations.

The KeyPress event, which uses KeyPressEventArgs, just doesn't have sufficient information.


Drat, you said WPF didn't you. It looks like you need e.KeyboardDevice.Modifiers.


I think you need a SpecialKey Handler. I googled a bit a found a solution here.

Following code from the referred link may solve your problem:

  void SpecialKeyHandler(object sender, KeyEventArgs e)
{
    // Ctrl + N
    if ((Keyboard.Modifiers == ModifierKeys.Control) && (e.Key == Key.N))
    {
        MessageBox.Show("New");
    }

    // Ctrl + O
    if ((Keyboard.Modifiers == ModifierKeys.Control) && (e.Key == Key.O))
    {
        MessageBox.Show("Open");
    }

    // Ctrl + S
    if ((Keyboard.Modifiers == ModifierKeys.Control) && (e.Key == Key.S))
    {
        MessageBox.Show("Save");
    }

    // Ctrl + Alt + I
    if ((Keyboard.Modifiers == (ModifierKeys.Alt | ModifierKeys.Control)) && (e.Key == Key.I))
    {
        MessageBox.Show("Ctrl + Alt + I");
    }
}

if (e.Modifiers == Keys.Control && e.KeyCode == Keys.Enter)

Tags:

C#

Wpf

Keystroke