28

I am using a keydown event to detect keys pressed and have several key combinations for 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 the key combination in which I hit 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. 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 no longer works.

jsmith
  • 7,198
  • 6
  • 42
  • 59

9 Answers9

48
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
  • 4,129
  • 23
  • 18
  • i used the exact same code, holding down cntrl and shift, but the if never gets fired. Is there something i am possibly doing wrong in the real life execution? – A.bakker Aug 17 '23 at 09:44
  • @A.bakker, this is real life code that is verified works. To debug your case, I'd suggest creating a `if (e.KeyCode == Keys.C) {` condition and placing a breakpoint there. Once the breakpoint is hit, inspect the value of `e.Modifiers` and adjust your code accordingly – Rom Aug 18 '23 at 08:10
9

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

McGarnagle
  • 101,349
  • 31
  • 229
  • 260
Chris J
  • 2,160
  • 4
  • 16
  • 23
6

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

Jasper
  • 2,166
  • 4
  • 30
  • 50
JDunkerley
  • 12,355
  • 5
  • 41
  • 45
2

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

Jasper
  • 2,166
  • 4
  • 30
  • 50
Druid
  • 6,423
  • 4
  • 41
  • 56
2
      if ((Keyboard.Modifiers & ModifierKeys.Shift | ModifierKeys.Control) > 0)
          Debugger.Launch();
Stephane Ehret
  • 340
  • 2
  • 9
1

This is what I did for a Ctrl+Z Undo and Ctrl+Shift+Z Redo operation and it worked.

  Private Sub Form_Main_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown
    Select Case e.KeyCode
      Case Keys.Add
        diagramView.ZoomIn()
      Case Keys.Subtract
        diagramView.ZoomOut()
      Case Keys.Z
        If e.Modifiers = Keys.Control + Keys.Shift Then
          diagram.UndoManager.Redo()
        ElseIf e.Modifiers = Keys.Control Then
          diagram.UndoManager.Undo()
        End If
    End Select
  End Sub
Chris
  • 33
  • 8
0

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
  • 110,061
  • 20
  • 134
  • 146
0

Seeing as no one else mentions them, i'm just going to leave the suggestion to use KeyEventArgs.KeyData:

if (e.KeyData == (Keys.C | Keys.Control | Keys.Shift)
{
  //do stuff
  //potentially use e.Handled = true
}
if (e.KeyData == (Keys.V | Keys.Control)
{
  //do other stuff
  //potentially use e.Handled = true
}

This should only act on specific key combinations, though the order of the modifiers don't seem to matter, the first one is always the last pressed key.

And e.Handled = true should stop it, though i don't know the specific mechanics behind it.

Martin
  • 1
  • 1
0

Another approach more suitable if you have more cases, and easier to read is:

private void Form_KeyDown(object sender, KeyEventArgs e)
{
    Action action = e.KeyData switch
    {
      (Keys.Control | Keys.Shift | Keys.C) => DoWork,
      (Keys.Control | Keys.V) => Paste,
      _ => null
    };
    action?.Invoke();
}
private void DoWork() 
{ // do work }
private void Paste() 
{ // paste }
Ahmad Kelany
  • 376
  • 3
  • 8