Capturing key event and remove beep!

G

Guest

I have a tabcontrol where I want to capture the CTRL+C key combination. My
tabcontrol has on its tabpages some treeView controls. My intention is to
intercept the event of pressing the CTRL+C when one of the controls embedded
in the tabControl has the focus. I'm doing this using the KeyDown event.
However, when pressing the CTRL+C my computer sounds a beep - as in 'not
allowed' beep. The capturing of the keycombination works fine, however, the
beep is unacceptable and must be removed.

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).

1) Does Anyone know how to capture the CTRL+C using the KeyPress event.

I've managed to remove the beep and keep program functionality intact using
the following:

private void tabControlTop_KeyPress(object sender, KeyPressEventArgs e)
{
e.Handled = true;
}

....along with:

private void tabControlTop_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Z && e.Control)......ya da ya da

.....however I feel that it is somewhat of a 'hack' using the Handled
property in this way - merely showing my shortsommings in understanding what
generates the beep.

2) Any suggestions to how this should be done.

Best regards Jesper
 
P

Peter Duniho

Jesper said:
[...]
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
 
G

Gregg Walker

1) Does Anyone know how to capture the CTRL+C using the KeyPress event.

You will need to cast the KeyChar value to int and then check for the value
0x03 (ASCII Control Code) for Ctrl-C.

I do this all the time with code similar to the following...

private void MainForm_KeyPress(object sender, KeyPressEventArgs e)
{
// suppress some keys to avoid annoying beep
switch((int)e.KeyChar)
{
case 0x03: // Control-C (Copy)
case 0x08: // Backspace
case 0x0d: // Carriage Return
case 0x16: // Control-V (Paste)
case 0x18: // Control-X (Cut)
e.Handled = true;
break;
}
}

hth
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top