I have WPF application which is having tool bar. In tool bar I have some user controls as tools.
I have set access key to each control, it is working fine.
The issue is: If I click a user control(which is consist of Button and Label, I have set access key for Button) the given task is completed, but when I press any access key without pressing 'Alt' key then it is getting selected.
Any ideas?
Apparently, this was a deliberate change by Microsoft. See Atanas Koralski's answer here:
http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/14f6f49f-0027-471b-b68c-e7f6ba012012
Menu and ToolBar mnemonics work without pressing Alt key. We decided that we will have uniform behavior in all cases so access key work without pressing Alt key.
I understand that this is not in parity with Forms and we will consider this issue and change the behavior in the next version.
For now as a workaround you can register a class handler for all AccessKeyPressed events and handle the event if Alt key is not pressed.
EventManager.RegisterClassHandler(typeof(UIElement), AccessKeyManager.AccessKeyPressedEvent, new AccessKeyPressedEventHandler(OnAccessKeyPressed));
...
private static void OnAccessKeyPressed(object sender, AccessKeyPressedEventArgs e) { if (!e.Handled && e.Scope == null && (e.Target == null || e.Target == label)) { // If Alt key is not pressed - handle the event if ((Keyboard.Modifiers & ModifierKeys.Alt) != ModifierKeys.Alt) { e.Target = null; e.Handled = true; } } }
Also see mfc2wpf's reply:
I've used the above and it works. However, that prevented the default action for Enter and ESC. So I inserted the following at the top of the method.
if (Keyboard.IsKeyDown(Key.Enter) || Keyboard.IsKeyDown(Key.Escape)) return;
Access keys include Enter and Esc, which are the default keys for Button
s which have IsDefault = true
or IsCancel = true
. If you don't want to require Alt+Enter and Alt+Esc for those buttons, you would need to add the special condition to the handler.