Bookmark and Share

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.

blog comments powered by Disqus

KodefuGuru.GetInfo()

Chris Eargle
LinkedIn Twitter Technorati Facebook

Chris Eargle
Telerik Developer Evangelist, C# MVP

JustCode

Telerik .NET Ninja

 

INETA Community Speakers Program

 

MVP - Visual C#

 

Friend of RedGate

World Map

Tag cloud

Month List

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

© Copyright 2010
Disclaimer: The opinions expressed herein are my own personal opinions and do not represent my employer’s view in any way.