Catching Ctrl + C in a textbox

Viewed 50887

Despite me working with C# (Windows Forms) for years, I'm having a brain fail moment, and can't for the life of me figure out how to catch a user typing Ctrl + C into a textbox.

My application is basically a terminal application, and I want Ctrl + C to send a (byte)3 to a serial port, rather than be the shortcut for Copy to Clipboard.

I've set the shortcuts enabled property to false on the textbox. Yet when the user hits Ctrl + C, the keypress event doesn't fire.

If I catch keydown, the event fires when the user presses Ctrl (that is, before they hit the C key).

It's probably something stupidly simple that I'm missing.

8 Answers

If you want to catch such combinations of keys in KeyPress Event look at this table here:

http://www.physics.udel.edu/~watson/scen103/ascii.html

in Non-Printing Characters section you can see the Dec numbers for each combination. For example, Dec number for Ctrl + C is 3. So you can catch it in KeyPress Event like this:

private void btnTarget_KeyPress(object sender, KeyPressEventArgs e)
{
    if (e.KeyChar != 3) // if it is not Ctrl + C
    {
       // do something
    }
}
Related