views:

1890

answers:

5

I am betting this is a really simple question to answer, I just can't get such a simple thing to work.

I use a keydown event to detect keys pressed and have several common key combination's to do various operations.

if (e.KeyCode == Keys.C && e.Modifiers == Keys.Control && e.Modifiers == Keys.Shift)
{
    //Do work
}
else if (e.KeyCode == Keys.V && e.Modifiers == Keys.Control)
{
    //Paste
}

For some reason or other the key combination where I am hitting ctrl+shift+c is not working. I have re ordered them, and placed it at the top thinking it might be interference from the ctrl+c, and even removed the ctrl+c to see if it was causing a problem. Yet it still does not work. I know it's probably something very simple, but can't quite grasp what it is. All of my 1 modifier + 1 key combination's work fine, as soon as I add a second modifier is when it seems to not work.

Thank you in advance.

+7  A: 
if (e.KeyCode == Keys.C && e.Modifiers == (Keys.Control | Keys.Shift))
{
    //Do work
}
else if (e.KeyCode == Keys.V && e.Modifiers == Keys.Control)
{
    //Paste
}
Rom
A: 

Have you tried e.Modifiers == (Keys.Control | Keys.Shift)?

Chris J
A: 

Try this. Should behave the way you want it to, and it's a little simpler.

 if (e.Control)
 {
    if (e.Shift && e.KeyCode == Keys.C)
    {
       //Do work
    }
    else if (e.KeyCode == Keys.V)
    {
       //Paste
    }
 }
Donut
A: 

If you want to allow Ctrl and Shift then use the bitwise OR (as Keys is a Flags enum)

if (e.KeyCode == Keys.C && e.Modifiers == (Keys.Control | Keys.Shift))
{
    //Do work (if Ctrl-Shift-C is pressed, but not if Alt is pressed as well)
}
else if (e.KeyCode == Keys.V && e.Modifiers == Keys.Control)
{
    //Paste (if Ctrl is only modifier pressed)
}

This will fail if Alt is pressed as well

JDunkerley
A: 

Another way would be to add an invisible menu item, assign the Ctrl-Shift-C shortcut to it, and handle the event there.

Druid