WinForms: Setting the accelerator key on a menu item

This article is referenced directly by Microsoft Accessibility Insights for Windows. Microsoft Accessibility Insights for Windows can help spotlight many accessibility issues in UI, and guide you to the relevant UI framework-specific code sample to help you resolve the issue. Learn more about Microsoft Accessibility Insights for Windows.

Problem

My ToolStripMenuItems have associated accelerator keys​. For example, "Ctrl+N" for New, and "F1" for Help. When my customers using a screen reader reach the menu items, the screen reader does not announce details of the accelerator keys, and so my customers may be blocked from efficiently accessing the functionality associated with the menu items.

Suggested Fix

A ToolStripMenuItem can have an "access key" set on it by including an ampersand in the associated text string. For example, setting the text to be "&New", will result in the UI Automation (UIA) element representing this item, having a UIA AccessKey property of 'n'.

The ToolStripMenuItem can also have a "shortcut key" set on it, such as Ctrl+N. In versions of the .NET Framework prior to 4.7.2, the shortcut key information is not exposed through UIA. With .NET 4.7.2 and later, the shortcut key information is exposed through the UIA AcceleratorKey property. For example, as "Ctrl+N".

To leverage the change introduced with .NET 4.7.2, either rebuild your app to target that version of .NET, or update your app's config file to specify that it should pick up the fix when .NET 4.7.2 is available on the machine, even if the app itself has not been rebuilt to target that version of .NET.