3

I am making key board shortcuts to a Winform application in C# using Visual Studio 2012. My shortcuts work perfect. But it gives a annoying beep sound.

I added e.Handled = true; and e.SuppressKeyPress = true; according to many threads. But it does not work and my winform stuck.

How can I avoid this?

private void textBoxSearch_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.Down)
        {
            do stuff
        }
        else if (e.KeyCode == Keys.Enter)
        {
            //do stuff
        }
        e.Handled = true;
        e.SuppressKeyPress = true;
    }

and I need a solution for this too.

 protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
    {
        if (keyData == (Keys.Control | Keys.F))
        {
            //do stuff
        }
        else if (keyData == (Keys.Control | Keys.G)) {
            //do stuff
        }

        return base.ProcessCmdKey(ref msg, keyData);
    }
user3693167
  • 783
  • 1
  • 9
  • 16
  • Can you please clarify what "an annoying beep" means? Does it do it when the shortcut is correct? When you press it? When it executes the action?? – Nahuel Ianni Jun 30 '14 at 09:47
  • "an annoying beep" means the windows alert sound like "Ding". It executes correctly. The "Ding" sound is the problem. – user3693167 Jun 30 '14 at 09:53

1 Answers1

0

What you have in the KeyDown event should work. The SupressKeyPress = true stopped the ding for me when I copied your code.

In the ProcessCmdKey event you would need this:

protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
    if (keyData == (Keys.Control | Keys.F))
    {
        //do stuff
        return;
    }
    else if (keyData == (Keys.Control | Keys.G)) {
        //do stuff
        return;
    }

    return base.ProcessCmdKey(ref msg, keyData);
}
jaredbaszler
  • 3,941
  • 2
  • 32
  • 40