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
  • 31,138
  • 14
  • 118
  • 137
urdurak
  • 28
  • 1
  • 4
  • 1
    `if (e.KeyData == (Keys.Control | Keys.C)) { }` – Jimi May 22 '22 at 00:08
  • @Jimi thanks but it didn't work again :( – urdurak 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. – urdurak 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

1 Answers1

-1
   if (e.KeyData == Keys.F10 || (e.Modifiers == Keys.Control && e.KeyCode == Keys.S))
            btnSave_Click(btnSave, new EventArgs());
  • 1
    Answer needs supporting information Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](https://stackoverflow.com/help/how-to-answer). – moken Jul 23 '23 at 03:35