views:

98

answers:

2
+1  Q: 

Key Events

Is there a way to handle the multiple key-press event on a C# windows form, like "Ctrl+E" ?

Here's my code:

private void frmDataEntry_KeyDown(object sender, KeyEventArgs e)  
{  
   if (Control.ModifierKeys == Keys.Control && e.KeyCode == Keys.E)
   {  
          //Code  
   }              
}

This condition is always false .. why? I press ctrl + E and e.KeyCode is false and Control.ModifierKeys is true? What am I doing wrong?

+4  A: 

It should be:

if (e.Modifiers == Keys.Control && e.KeyCode == Keys.E) {
    //Code
}

Control.ModifierKeys is for onClick events and the like.

Can Berk Güder
+2  A: 

I think the condition you're looking for is

if (e.Control && e.KeyCode == Keys.E)
{
    // code
}
Leaf Garland
this yields a slightly different behaviour than my answer. my condition is only true for Ctrl+E while this is also true for Ctrl+Shift+E, Ctrl+Alt+E, etc.
Can Berk Güder