Been a bit busy and a lot lazy recently. Time to post!
One of the tools every user has for accessing menu items are mnemonics. Mnemonics, other being one of the freakiest words in the English Language, is a method of accessing menu and toolbar items via the keyboard. Mnemonics in Windows programs are accessed by an alt-[key] combination, similar to the way shortcuts are accessed via ctrl-[key]. An example of a mnemonic you can use right now is alt-f. That opens up the File menu on just about every Windows program. To get a quick idea what mnemonics are available to you in a program, hit the alt key and look at the program's menus and toolbars. See how some of the letters are now underlined? Those are your mnemonics. Use them with the alt key to perform a "click".
Programming in mnemonics is pretty simple in Windows Forms. Everything you see in a program, from the lowly Label to the big daddy Form itself is an extension of the System.Windows.Forms.Control class. From this virtual class they inherit the Text property. The implementation of this property is left for inheritors, but if the control displays text and takes input, your mnemonic will be set by using this property. The standard convention is to, when setting the text of this property, prefix your mnemonic with the "&" symbol. For example, if I was going to name my File menu, I would do the following:
MenuItem _fileMenu = new MenuItem();
_fileMenu.Text = "&File";
By simply placing the "&" before the letter F I designate that alt-f will be the mnemonic for the control. Nice and simple. And that's where it gets you.
You see, every keypress you make must be examined to determine if it is normal input or a mnemonic key combination. If the keypress event (not an actual event name but a group of events; check this blarg post for more detail than you could ever want about the process) is a combination of the alt key and any of the keys a-z and 0-9, that is treated as a mnemonic. And if no control is watching for that mnemonic, you get beeped at.
What this means is that if you have a menu or toolstrip in your program, you cannot use alt-key combinations for anything other than mnemonics, otherwise every time your users... use... that function, your program will ding at them. Extremely annoying. Unfortunately, the standard events for handling key presses can't be used to intercept the key event and prevent it from bubbling to your menus and toolstrips, thus preventing the beep from bugging the crap out of you and your users. Which is why I'm writing this.
I'm working on a dinky little side project right now, and one of the features is the ability to go into fullscreen mode. The standard mnemonic for this is alt-enter. Try it in Windows Media Player. Unfortunately, because mnemotics are designated by placing an "&" in the text of a control before the letter you wish to use... well, you can see the issue here. Besides, I have a Fullscreen button on my toolbar (and no menu--its a very simple program by design), I want to use alt-f as another means to go to fullscreen. I can add a KeyUp event handler in the form and intercept the alt-enter key combination in order to perform a fullscreen, but again I can't prevent that event from bubbling to the toolbar, which will trigger a beep as it doesn't have an alt-enter mnemonic. So, is there a solution?
Well, its a good thing you asked (goddamn, I'm dragging this out). There is. You can override the form's ProcessDialogKey method and intercept that key combination and mark it (by returning true from the method) as not being a mnemonic. The method is as follows:
/// Processes a dialog key.
/// <param name="keyData">One of the <see cref="T:System.Windows.Forms.Keys"></see> values that represents the key to process.</param>
/// true if the keystroke was processed and consumed by the control; otherwise, false to allow further processing.
/// <remarks>The purpose of this override is to prevent alt-enter from being processed as a command, which results in the windows beep being played as it is not a valid mnemonic.</remarks>
protected override bool ProcessDialogKey(Keys keyData)
if (keyData == (Keys.Alt | Keys.Enter))
Of course, you can substitute Keys.Enter for any key you wish to prevent from being identified as a mnemonic. And that's that.