Taking the Ding out of Alt

Grabbing the KeyDown event of a Windows Form is a great way to implement behavior that depends on keyboard input. However, it has this annoying tendecy to "ding" if you use the alt key. Investigating this interesting behavior, I discovered that it occurs before the KeyDown event is raised. Luckily, there is a method that can be overridden to implement the proper code and handle it before it reaches the noisy underbelly of WinForms. The following code snipped illustrates grabbing alt-d. To use it with another key stroke, replace Keys with the ones you want.

protected override bool ProcessCmdKey(ref Message msg, Keys keyData)

{

if (keyData == (keyData | Keys.Alt | Keys.D))

{

//Do Something

return true;

}

return base.ProcessCmdKey(ref msg, keyData);

}

It is important that the base method only be called if your override is not handling the key press.

  • Share This Post:
  • Share on Twitter
  • Share on Facebook
  • Share on Technorati

Print | posted on Tuesday, November 20, 2007 5:06 PM

Comments on this post

No comments posted yet.

Your comment:

 (will show your gravatar)