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?
As of .Net 4.5 you can configure this behavior with the CoreCompatibilityPreferences.IsAltKeyRequiredInAccessKeyDefaultScope property. To change the access key behavior so that they will only fire when Alt is pressed set it to true.
As the documentation states this must early in the app. Setting it will throw an exception after it has been read.
As stated in other answers, setting
IsAltKeyRequiredInAccessKeyDefaultScope
avoids invoking actions for access keys without pressing the Alt key. However, this can also have the effect of disabling the Enter key (for invoking the default action) and Esc key (for invoking the Cancel action).Using the suggested workaround instead, and testing for
Key.Enter
andKey.Escape
, can circumvent this problem. However, you might then find that menu items cannot be selected by their access key without pressing the Alt key, which could be a problem if a button in scope uses the same access key.An alternative could then be to handle the access key event by checking whether a potentially invokable
AccessText
control is within aMenuItem
or not, something along these lines:...
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
Also see mfc2wpf's reply:
Access keys include Enter and Esc, which are the default keys for
Button
s which haveIsDefault = true
orIsCancel = 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.