RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Exploring Secrets of .NET Keystroke Handling : Page 3

If you've ever been frustrated trying to figure out how to intercept or assign specific keystrokes to specific controls, you'll be glad you found this article.

Keystroke Sandbox: The Key Panel
Each time you invoke a key combination—a set of keystrokes that together comprise a single unit (e.g. Control+Alt+P)—Keystroke Sandbox emits a header and a key sequence. The header is a thick black line in the output pane containing the current settings:

  • A date/time stamp
  • Which control has focus
  • Whether the form's KeyPreview is enabled
  • Which instrumented controls are disabled, if any
  • Which instrumented controls are suppressed, if any
The key sequence immediately follows the header and may wrap onto several lines, depending on your window width. The key sequence comprises a set of zero or more key panels. Each key panel represents the action of a single method of a single control. Table 4 provides these definitions for future reference. Figure 3 shows a collection of assorted key panels to point out what information you may glean from them. A single key panel is magnified towards the left half of the diagram. In the right half, I have artificially exposed a variety of tooltips to show you that, though some things are condensed to fit in the panel, the full information is available by hovering over each item with the mouse. (Each exposed tooltip corresponds to a component to the left and slightly higher than itself.)

Table 4. Key nomenclature: Here's a quick reference to the terms I use in this article.
Term Meaning
Key combination The set of keys comprising a single keystroke, e.g. Control+Alt+R.
Key sequence A set of key panels corresponding to one key combination.
Key panel A proxy representing the action of a single method of a single control.

Figure 3. KeyPanel Details: On the left, a single key panel is enlarged and labeled. On the right, many tooltips have been artificially exposed to show how full information is available beyond the condensed information displayed.
The magnified key panel in Figure 3 itemizes the component parts of a key panel:

  • Control: This displays the truncated or abbreviated class name of the control responding to the key (abbreviated by omitting the prefix "Instrumented," if present). The full name is available as a tooltip. Each control is arbitrarily assigned a color that is used as the background color for this component. For example, in Figure 3 all key panels with the top component yellow are for the InstrumentedTextBox.
  • Method: This component displays the name of the method that handles the keystroke, truncated or abbreviated to fit the key panel by omitting the prefix "Process", if present (as in ProcessCmdKey, ProcessDialogKey, etc.). The full name is available as a tooltip. Each key event (KeyDown, KeyPress, and KeyUp) has a correspondingly color-coded background for the methods associated with it. Thus, all OnKeyDown methods are teal, as are all ProcessDialogKey methods, because ProcessDialogKey is also associated with the KeyDown event.
  • Key name: This component holds the truncated or abbreviated name of the key. This is often obvious, but not always. If you press A, the name is "A." If you press Control, the name is ControlKey (and the Key suffix is omitted). If you press Alt, though, the name is MenuKey. This field is overloaded: if the method is an existential method (e.g. IsInputKey), then this field contains the Boolean return value for that function (and is colored blue to differentiate it).
  • Modifiers: These are symbolic indicators of currently active modifier keys, if any. The possible values are Control (^), Shift (_), or Alt (~). A tooltip provides the words corresponding to these symbols.
  • ASCII value: This component is populated only when the key ends up as a real character, typically evident in the OnKeyPress method. Note that when an ASCII value is available, the key name component also has more resolution, in a sense. That is, in Figure 3 many of the key panels show a capital "A" even though a lowercase "a" was typed. That is because it is not a character at that point; it is a value from the Keys enumeration, specifically Keys.A. Only when it the value gets treated as a character can the case be distinguished.
Output Scenarios from Keystroke Sandbox
The screenshots in this section illustrate how to evaluate output from Keystroke Sandbox. Figure 4 and Figure 5 show the output for the same seven key combinations; however, Figure 4 displays key panels only for the form, while Figure 5 displays key panels only for the TextBox. For each figure, I used the control pane at the bottom of the application to suppress output from unwanted controls.

Author's Note: Be cautious when masking output, because controls can interact with each other. It's often helpful to see all the output, even though sifting through the longer output takes more effort.

Figure 4. Form-Level Key Sequences: Here are key panels for the Form level only; the TextBox and DataGridView key panels are hidden. The keypresses for each row are: (1) An ordinary character. (2) A menu shortcut. (3) A navigation key (right arrow). (4) An escape key. (5) A navigation key (home). (6) A mnemonic on the Load Grid button (Alt+G). (7) The Tab key.
Figure 5. TextBox Key Sequences: Here are key panels for the TextBox only; the Form and DataGridView key panels are hidden. The keypresses for each row are: (1) An ordinary character. (2) A menu shortcut. (3) A navigation key (right arrow). (4) An escape key. (5) A navigation key (home). (6) A mnemonic on the Load Grid button (Alt+G). (7) The Tab key.
Figure 6. Key Panel Sequence: The figure shows panels for a single key combination, Control-Shift-F, with Control key panels highlighted in red and Shift key panels in purple.

Both Figures 4 and 5, because they suppressed some output, produced relatively short key sequences. But take a look at Figure 6, the key sequence for a single key combination, Ctrl+Shift+F, which is rather lengthy.

Recall that there are three events to any keystroke: KeyDown, KeyPress, and KeyUp. Physically, in this example, Ctrl was pressed first, which invoked various methods by firing the KeyDown event. Pressing and holding the Shift key did likewise. Finally, adding the F key invoked KeyDown and KeyPress. I then released the keys: first F, then Shift, then Control (see the last six key panels); KeyUp fires as each key gets released. The order of the press and release of each key in the combination, therefore, affects whether all its key panels are contiguous.

Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date