Consuming the KeyDown Event?

G

Guest

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 though the event works correctly? How can I get rid of this beep? I thought that setting the e.Handled to true would do it but no luck

private void txtbxEmail_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e

if ( (e.KeyCode == Keys.E) && (e.Control) && (e.Shift)

if ( txtbxHomeEmail.Text.Length > 0

System.Diagnostics.Process.Start("MAILTO:"+txtbxHomeEmail.Text)
e.Handled = true




Many Thank
Peter
 
H

Henk Verhoeven

Peter
Have you tried this on another machine with a different keyboard.?

Perhaps it is keyboard feature (Special Keyboard) that does the beep and not
the key down. Try shutting down any resident keyboard programs or another
machine with a different keyboard than yours.

Henk
Peter said:
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 though the event works correctly?
How can I get rid of this beep? I thought that setting the e.Handled to
true would do it but no luck.
private void txtbxEmail_KeyDown(object sender,
System.Windows.Forms.KeyEventArgs e)
 
S

Stoitcho Goutsev \(100\) [C# MVP]

Hi Peter,

The beep is made by the DefWindowProc for keys which is not handled by the
text box. Your code doesn't work because beep is produced in responce to
WM_CHAR (KeyPress), I believe. However you have options.
1. you can override TextBox and its WndProc method and process WM_CHAR
message without passing it to the DefWindowProc
-or-
2. Add a main menu item (it can be hidden) with shortcut for Ctrl+Shift+E
and handle the key press there. You should check the focused control if you
want to react on this combination only for the text box. The downside is
that you always swallow this key combination.
-or-
3. Handle KeyPress instead of key down. e.Handled works for this event. For
Ctrl+Shift+E key press is fired only for Ctrl+E (ASCII 5) so, you have to
chack for Shift yourself. KeyPressEventArgs doesn't have the key modifiers
that's why probably you should use Control.ModifierKeys to check for Shift.
The code could be something like

private void textBox2_KeyPress(object sender,
System.Windows.Forms.KeyPressEventArgs e)
{
if ( e.KeyChar == 5 && (ModifierKeys & Keys.Shift) != 0)
{
if ( textBox2.Text.Length > 0 )
{
Console.WriteLine("Handled in keydown");
e.Handled = true;

}
}
}
--

HTH
Stoitcho Goutsev (100) [C# MVP]


Peter said:
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 though the event works correctly?
How can I get rid of this beep? I thought that setting the e.Handled to
true would do it but no luck.
private void txtbxEmail_KeyDown(object sender,
System.Windows.Forms.KeyEventArgs e)
 

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