tags:

views:

1850

answers:

4

Hello all,

I'm developing a large line of business app with C# and WPF. Our standard is to include accelerator keys for buttons on all forms (Save, Cancel, Search, etc.). I've recently noticed that when a form is loaded, the accelerator key is active even when the user does not press the "Alt" key. For example, our Search button uses "Alt-H" as the accelerator, but the user can initiate a search by simply pressing "H". I'm using the standard "_" in the button content to create the accelerator key.

<Button Content="Searc_h"/>

Has anyone else noticed this behavior and has anyone found a suitable workaround that requires the "Alt" key to be pressed?

+1  A: 

There is a Windows setting that displays the "_" without the Alt key being pressed. You might want to check if that is enabled.

In Display Properties -> Appearance -> Effects -> 'Hide Underlined Letters for Keyboard Navigation until I press Alt key". This is for XP - not sure where it is at for Vista.

EDIT: This at least is true in that it will display the mnemonic without first pressing the Alt key. I just re-read your post and noticed that it will automatically launch when you press the 'H' key without Alt. Not sure what is going on there.

j0rd4n
I looked at that setting (in Vista it is in Control Panel -> Ease of Access Center). I'd really like the accelerator to behave like in IE or VS: display the "_" when the user presses "Alt", hide the accelerator when the user presses "Alt" again, and only fire when the "Alt" key is pressed.
I use the underscore syntax on buttons in my WPF app and the shortcut keys don't fire unless Alt is pressed. My guess would be the same as j0rd4n's and there is a Windows setting somewhere allowing the shortcuts without the Alt key.
sipwiz
I also noticed that it only happens when no other control on the form has focus. If I manually set focus to the first control on the form when it is loaded, then all is ok. If I do nothing to focus a specific control, then I get bad behavior.
Are you interop-ing between WinForms and WPF? This article mentions that the AccessKeyManager may automatically handle accelerators without Alt: http://msdn.microsoft.com/en-us/library/ms742474.aspx
j0rd4n
No, just straight WPF.
+3  A: 

This behavior seems to be normal on Windows. At least I just tried it with a Winforms window and pressing the accelerator without Alt did indeed invoke a button or shifted focus to the appropriate control.

Joey
Indeed, this is normal behavior for any Windows application.
Pavel Minaev
A: 

This article on Sara Ford's Weblog discusses this phenomenon.

A commenter mentions this as a solution:

"If you're writing an application and you don't like this behavior, look up WM_CHANGEUISTATE and specifically the UISF_HIDEFOCUS flag."

Alex
A: 

Actually, the Command pattern in WPF allows you more granular control over what keyboard shortcuts are allowed. It goes a step further than the "_" in your button text.

Check the following link for more information:

http://www.switchonthecode.com/tutorials/wpf-tutorial-command-bindings-and-custom-commands

Edit: previous link was dead - provided a new link.

-Doug

Doug
The link is dead.
macias