Hotkeys are always active even w/o ALT pressed in .NET! ???

  • Thread starter Thread starter Tony Brummel
  • Start date Start date
T

Tony Brummel

Ok, I can't be the first to notice this but I can't find
anything on MSDN, TechNet, etc...

Create a simple Windows Form app and throw a couple
buttons on the form. Give each button a hotkey by
preceding a character in the .Text property with an &.

Now, run the thing and note that regardless of which
button the focus is on, simply pressing the hotkey letter
WITHOUT HOLDING DOWN THE ALT fires the hotkey (the button
press events in this case).

This has got to be a bug or there is some
strange "default" behavior with .NET Windows Forms. Has
anyone seen this? Any idea how to get around it?
Obviously, it's real bad news -- if the user has the
focus on button or datagrid, etc. (control that doesn't
want the key) and starts typing, the focus is "stolen"
once they stumble on a hotkey.
 
Not a bug. The same behavior holds for any Windows app.

The key is in which control is focused when you press the key. If, for
example, an edit box or list box has the focus, then that control knows what
to do when you press the 'A' key. So it does its thing, and tells Windows
"yes, I've processed this key, you don't need to do anything else with it".
But if the focused control doesn't know what to do with an 'A' key -- for
example, a button, which doesn't really take any keyboard input (other than
Spacebar and Enter) -- then it says to Windows, "I'm not going to handle
this key. Why don't you see if there's a corresponding accelerator?"

Try this in any dialog box that has accelerators on its buttons (for
example, the Add Printer wizard in Windows XP). As long as the focus isn't
on an edit box, you can just press the N key to go to the next page, B to go
back, or the underlined character on a radio button to select that radio
button.
 
Back
Top