detect Ctrl + Enter
Asked Answered
S

4

8

(using WPF) i try to detect when Ctrl + Enter gets hit. so i tried this code:

if (e.Key == Key.Return && (e.Key == Key.LeftCtrl || e.Key == Key.RightCtrl))
 {
   //Do Something            
 }

Obviously this is not correct, as it does not work. Could anyone help me out, explaining what the right way should be ?

thanx

Sandon answered 24/4, 2012 at 6:28 Comment(0)
A
16

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.

Ataghan answered 24/4, 2012 at 6:31 Comment(1)
thanx! e.KeyboardDevice.Modifiers should help me out i think :)Sandon
E
8

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");
    }
}
Expect answered 24/4, 2012 at 6:55 Comment(4)
my pleasure, if i had been of any help. :)Expect
Actual answer is here if anyone is wondering. This should be marked as answer, first one is just lazy.Betthezel
@Yusha: You have a very weird concept of what SO should be (and one that the site owners and moderators don't share) if you feel that an answer that provides both an explanation of the problem and a solution is "more lazy" than one that copy+pasted code from another website. Furthermore, the answer you like is buggy, because it checks the state of the Control key asynchronously, while the property I suggested is correctly synchronized with the input queue. You may only see a difference under load, but hard-to-trigger bugs are the worst.Ataghan
Your case would be fine, but think about it, what if you wanted to catch someone doing CTRL + V. That is when your case AND this answer both fail. I'm not trying to be rude, I'm just saying.Betthezel
C
4
if (e.Modifiers == Keys.Control && e.KeyCode == Keys.Enter)
Ctenidium answered 15/2, 2015 at 2:3 Comment(0)
I
-1
   if (e.KeyChar == 10)
            {
             ///Code
            }

Or

 if ((Char)e.KeyChar == '\n')
            {
             ///Code
            }
Itis answered 3/7, 2014 at 14:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.