By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,705 Members | 1,301 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,705 IT Pros & Developers. It's quick & easy.

Capturing key event and remove beep!

P: n/a


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


Aug 11 '07 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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
Aug 11 '07 #2

P: n/a
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
--
Gregg Walker
Aug 13 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.