473,396 Members | 1,757 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Capturing key event and remove beep!



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
2 4737
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Randi | last post by:
Hi All, I have a problem on a username password login form. I use: txtPassword.Text = StrConv(txtPassword.Text, vbProperCase) to validate the proper case when someone enters their name and...
3
by: Greg | last post by:
I want to be able to capture the user pressing Ctrl+S. I know that the IE browser has a key binding for Ctrl+S but is there any way that I can be notified of this key press anyway. I have found...
2
by: Ivo | last post by:
In an Win/IE-only application, some checkboxes and the like have been assigned accesskeys, so pressing alt+w focuses a checkbox, alt+b another. This is very convenient. However, when the checkboxes...
5
by: Tim Marshall | last post by:
In an A97 app I'm redeveloping, I've got a field in a table called TS_LAST_PRINT which indicates the last date this record was printed. That is a hard copy was produced, not an on screen print...
2
by: Peter | last post by:
Hello Thanks for reviewing my question. I am trying to consume the Keydown event of the Cntrl-shift-E in a textbox and have noticed that when I press key this combination a beep is made even...
10
by: Andrew | last post by:
Hi, I have a messagebox that pops up due to an event. I did it in javascript. ie. alert("Time's up. Assessment Ended"); I want to capture the OK and Cancel events of this alert messagebox. My...
17
by: Lauren Wilson | last post by:
Does anyone know if it is possible to capture FTP responses to various FTP commands when managing an FTP session from a VBA procedure? For example, if we try to login to an FTP server and the...
6
by: MLH | last post by:
Using A97. Want to examine 17-char VIN entered by user. VIN codes are alphanumeric and do not contain Oh's to prevent the confusion that could result if zeros were misread as O's or o's. So, if...
16
by: Neil | last post by:
I am using Access 2003, and have an ongoing problem of every once in a while losing an event procedure for a form or control. The procedure's still in the code module, but the form or control's...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.