Jesper wrote:
[...]
Previously I've done this by using the KeyPress event and used a "e.Handled
= true;" command to avoid the beep. However, I cannot manage to capture the
CTRL+C key combination (I'm aware of the Controls.KeyModifier functionality
but not sure how to construct the clause that keys on the CTRL+C combination).
The Control.ModifierKeys property is a flags enum, so you can use it
like this:
if (e.KeyChar == 'C' && (((Control)sender).ModifierKeys &
Keys.Control) == Keys.Control)
{
// Ctrl-C was pressed.
}
(The use of "sender" is to ensure that the correct control is used to
get the ModifierKeys property...the sender is the one for which those
flags will be set and relevant, even if you should happen to move the
code to someplace else. Obviously, if you are writing an override to
OnKeyPress rather than handling the KeyPress event, this isn't necessary).
I don't have time at the moment to go back and verify myself, but I'm a
bit surprised that you still get the beep if you set e.Handled in the
KeyDown handler.
That said, it's my preference to handle this sort of thing more
explicitly. By overriding Control.ProcessDialogKey, you make it clear
that the key is a specific user-command type of key, and avoid having
the key-press getting passed along to other code in the chain of window
message event handling. In particular, the base class implementation
that emits the beep when it sees a key it doesn't know how to deal with. :)
Pete