0

I want to handle the ctrl + c keys in keydown event but it is not working. I am trying this code but not working. when I print e.keycode, I see it as "Controlkey" but I am pressing Ctrl + C. I tried for ALT + A. It is working and e.keycode is coming as "A" key. And I tried to code in this link: Link is here. But didn't work again.

My code (if key is Ctrl+ C, e.keycode = Controlkey ):

private void richTextBox1_KeyDown(object sender, KeyEventArgs e)
{
    if (Control.ModifierKeys == Keys.Control && e.KeyCode == Keys.C)
    {
        Console.WriteLine("work please");
    }
}

I tried this code for another project, and it works but now I am writing again and it doesn't. How can solve it?

Edit: It is working for this code (if key is Alt + C, e.keycode = A ) :

if (Control.ModifierKeys == Keys.Alt && e.KeyCode == Keys.C)
{
    Console.WriteLine("work please");
}
Maytham Fahmi
  • 26,353
  • 12
  • 100
  • 121
Uğur Durak
  • 1
  • 1
  • 1
  • `if (e.KeyData == (Keys.Control | Keys.C)) { }` – Jimi May 22 '22 at 00:08
  • @Jimi thanks but it didn't work again :( – Uğur Durak May 22 '22 at 10:24
  • Of course *it works*. I don't know what you have written or what you're testing, but that code catches `CTRL+C`. – Jimi May 22 '22 at 17:01
  • @Jimi I copied the code you wrote and tried it but it didn't work. Then I tried it in a different project and it doesn't catch the ctrl+c key while the form has a menustrip. – Uğur Durak May 23 '22 at 18:09
  • You mean when you have a MenuStrip **AND** a MenuItem that registers the `Control + C` shortcut. Then of course you don't get the event, since it's supposed to be handled by the MenuItem. In these cases, you handle the Click event of MenuItems and verify what the current `ActiveControl` is. – Jimi May 24 '22 at 07:32

0 Answers0