1
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
    switch (e.KeyCode)
    {
        case Keys.Up:
            this.Text = "up";
            break;

        case Keys.Down:
            this.Text = "down";
            break;

        case Keys.Left:
            this.Text = "<-";
            break;

        case Keys.Right:
            this.Text = "->";
            break;

        case Keys.Delete:
            this.Text = "delete";
            break;

        case Keys.Control:
            this.Text = "control";
            break;

        case Keys.Control | Keys.C:
            this.Text = "control + c";
            break;

        case Keys.Control | Keys.X:
            this.Text = "control + x";
            break;

        case Keys.Control | Keys.V:
            this.Text = "control + v";
            break;

        default:
            break;
    }
}

Everything that related to holding Control do not displays in form header... This is just code example and not real project. In real project I need to catch Control + C / X / V presses to do copy / paste operations.

Kosmo零
  • 4,001
  • 9
  • 45
  • 88
  • 1
    You are just testing for the wrong key. If you want to see the Ctrl key itself, rather than just the modifier, then you'll have to test for Keys.ControlKey. There is no point to it. – Hans Passant Jun 05 '14 at 11:25

1 Answers1

1

try this solution :

case (e.Control && e.KeyCode == Keys.C)

EDIT : if it not works, add paranthesis around your code :

case (Keys.Control | Keys.C):

EDIT 2 : and this one :

e.KeyData == (Keys.Control | Keys.V)

EDIT 3: switch on KeyData

switch (keyData) {
   // Control+ C
   case Keys.Control | Keys.C:
      // ...
      break;
   case Keys.Control | Keys.V:
      // ...
      break;
}
angel
  • 322
  • 1
  • 7
  • Sorry, did you tested by yourself the compile ability of your examples? The one in comment and this one is uncompilable. `Cannot implicitly convert type 'bool' to 'System.Windows.Forms.Keys'`. And the `(Keys.Control | Keys.C)` is same result as my original code. – Kosmo零 Jun 05 '14 at 11:01
  • my first solution is normal able to compile if you use `KeyDown` event the sencond, I think the miss of paranthesis are cause – angel Jun 05 '14 at 11:04
  • are you using `winforms` ? – angel Jun 05 '14 at 11:05
  • `if (e.KeyData == (Keys.Control | Keys.V)) this.Text = "control + v";` This works... But how do make it work in switch case? – Kosmo零 Jun 05 '14 at 11:10
  • whereas to switch `KeyCode`, switch `KeyData` – angel Jun 05 '14 at 11:12
  • 1
    This works, but weird. It still no capture `case Keys.Control:` – Kosmo零 Jun 05 '14 at 11:14
  • 1
    to capture, I think you can override this : `protected override bool ProcessCmdKey (ref Message msg, Keys keyData) { switch ( keyData ) { case Keys.F4 | Keys.Control: // Do something } }` you have an other solution here :-D : `http://stackoverflow.com/questions/1646998/up-down-left-and-right-arrow-keys-do-not-trigger-keydown-event` – angel Jun 05 '14 at 11:21
  • Ughh I will think about it. But now I already happy. `case Keys.Control:` is for future. Thank you. – Kosmo零 Jun 05 '14 at 11:25