A developer can experience a number of odd rules when working through an application design, especially when it comes to accessibility. The two main reasons to provide a hotkey within an application are to make the application more accessible for users who have a hard time using a mouse and to make it more efficient to use for keyboardists. Pressing Alt+Q to quit an application is often easier for these users than clicking the associated button with a mouse. Both actions accomplish the same goal, but using different approaches.
To create a hotkey in most languages, a developer simply adds an ampersand (&) in front of the hotkey character for a control’s caption (text) or its associated label (as in the case of a text box). For example, a caption of &Quit on a button will create a hotkey of Q and the user would press Alt+Q to perform the default action with that button without using the mouse or by tabbing between controls. Modern version of Windows show the hotkeys as underlined letters when the user presses Alt. (Older versions of Windows showed the hotkeys underlined at all times.)
A reader recently pointed out that there is a technical error in my book, “Start Here! Learn Microsoft Visual C# 2010 Programming.” The Array project that begins on page 90 shows two buttons that use the Q hotkey in Table 4-1 on page 92, LINQ and Quit. When it comes to buttons, every button must have a unique hotkey. That’s because using the hotkey doesn’t select the button—it performs a task. Pressing Alt+Q in the example as written will either perform the task provided by the LINQ button or quit the application (the action provided by the Quit button). To correct this problem, you need to specify the LINQ button use the N hotkey instead by changing the Text property from LIN&Q to LI&NQ. Pressing Alt+N will now execute the task performed by the LINQ button.
However, there is a little rule oddity that you need to know about hotkeys when designing applications. While you can’t duplicate the hotkey used for a button, you can duplicate the hotkey for fields. That’s because the hotkey selects the field. When the user presses the hotkey combination a second time, the next field that relies on the hotkey is selected. If there is a third field with the same hotkey, pressing the hotkey combination a third time will select that field. The fields are selected in order until they have all been selected, and then pressing the hotkey again will select the first field. Consequently, when you have an abundance of fields on a form and a lack of usable hotkeys, duplication becomes an option.
Remember that duplication only works with selections. For example, the label associated with a text box can contain a hotkey. Pressing the hotkey combination defined by the label will select the text box. The same can be said of checkboxes, listboxes, comboboxes, and any other field-type control you can image as long as the default action for that control is selection, rather than performing a task as a button would.
I don’t often make accessibility errors in my applications because it’s a big deal to me to make my applications as friendly and accessible as possible. However, it happens even with me, which points out the need to have other people test your code strenuously; looking for this very sort of problem. Normally, you don’t want to duplicate hotkeys, but when you must use duplicate hotkeys due to the number of controls on a form, use duplication only with controls that select a field. Let me know if you have any questions or comments at John@JohnMuellerBooks.com.