Newm
Newm

Reputation: 1403

WinForms ALT key beep

I have an application in which I have implemented some keyboard shortcut keys for a given set of operations, some of these use ALT as a modifier e.g. ALT + 1. The functionality itself works fine however the system beeps during the key press.

I have read various posts that say I could use e.Handled in the KeyPress event however this is not helping in my scenario. The problem is easily replicated by creating a new Windows Forms application and running it without any modifications, pressing ALT + 1 for example will cause the system to beep.

I have noticed that other application such as Notepad have this behaviour too, if you launch Notepad and press ALT + J (or any other invalid menu keypress) the system will beep.

Is there any way to prevent the beep via my application or is it standard Windows behaviour?

Upvotes: 0

Views: 1168

Answers (3)

user9274088
user9274088

Reputation:

I use Alt+E for editing a record under certain circumstances. Here's what I had to do to eliminate the beep.

private void Object_KeyUp(object sender, KeyEventArgs e)
{
    if (e.KeyCode == Keys.E && e.Alt)
    {
        e.SuppressKeyPress = true;
    }
}

private void Object_KeyUp(object sender, KeyEventArgs e)
{
    if (e.KeyCode == Keys.E && e.Alt)
    {
        e.SuppressKeyPress = true;
        EditRecord();  // This opens a form for the editing process
    }
}

Note: If you try to use EditRecord from Object_KeyDown and only SuppressKeyPress in Object_KeyUp, you will still get the beep.

Upvotes: -1

Pankaj
Pankaj

Reputation: 2754

You can Handle KeyDown Event and do something like this for Alt+1 But i suspect you might have to do this for all the invalid keys

private void keyDown(object sender, KeyEventArgs e)
{
   if (e.KeyCode >= Keys.D1 && e.Alt)
   {
       e.Handled = true;
       e.SuppressKeyPress = true;
   }
}

Upvotes: 2

Nicolas R
Nicolas R

Reputation: 14619

Can you try to use SuppressKeyPress in you KeyPress event management?

 e.SuppressKeyPress = true;

Upvotes: 0

Related Questions