Interactions Among Key-Handling Methods
Again, the methods in Table 2 appear in the order they're executed; however, just because the table shows a method as available does not mean that it is always executed. Figure 1
shows the somewhat complicated interactions among the various key-handling methods. The colors in this figure correspond to the colors used in Table 2: blue indicates methods to fire events and salmon indicates primary methods to alter behavior.
|Figure 1. Processing Flow: An initial diagram of methods and events involved in keyboard interactions (per "jfo's coding" blog), and how they may be altered. See Figure 8 for revisions to this flowchart.|
I constructed the flowchart in Figure 1
based on the information in jfo's coding blog
. You will shortly learn how to actually observe the flow indicated in the figure using Keystroke Sandbox—and to see that it needs some revision!
Figure 1 shows that you can break a keystroke down into three principal actions: KeyDown, KeyPress, and KeyUp. Each of these actions wends its way toward a goal of firing an event of the same name (the blue boxes). However, it is by no means inevitable that an action results in firing its associated event—that depends on how you handle the methods that lead up to it, as you'll see later.
Keystroke Sandbox: A Tool for Observing the Flow of Key-Handling Actions
Before moving on to the practical guide to handling keyboard input, you should be familiar with the Keystroke Sandbox (see Figure 2), a tool designed just for this article. Keystroke Sandbox visually shows which methods fire in which controls when you press one or more keys.
Keystroke Sandbox Overview
|Figure 2. Keystroke Sandbox: Using this diagnostic tool you can observe the flow of key-handling activity through any instrumented controls.|
Keystroke Sandbox displays key-handling activity for any instrumented control. By default, the application includes an InstrumentedTextBox and an InstrumentedDataGridView. You can very easily instrument any other control you wish, place it on the form, and it will automatically be integrated into the application (you'll see more information later in the section "Extending the Keystroke Sandbox"). The form is also instrumented. Any key handling performed by any of these controls appear in the main output pane, modulated by any settings you have made in the control pane at the bottom of the window.
Using the Control Pane
To focus precisely on what you want to see, use the control pane at the bottom to modify the behavior of any instrumented control in the application.
First, select the control of interest in the drop down box at the left. All the other elements in the control pane (the checkboxes and arrows) will immediately update to reflect the settings for that control. Just below the drop down you have two checkboxes. The "Enable" checkbox enables or disables the referenced control on the fly. Disabled controls receive no keystrokes—they're effectively turned off. The "Display" checkbox serves as a filter. When checked, the selected control's activity appears in the main output pane; uncheck it to suppress output from that control.
The most interesting part of the control pane, however, is the graphical flow chart. The graph shows the four principal methods that provide customization of key handling, discussed in previous sections, along with the three key related events (using the same color coding as in Table 2 and Figure 1). Adding key-handling customization consumes a keystroke rather than letting it continue percolating through the system. By clicking on an arrow in the graph emanating from any of the four methods, you can instruct the application to emulate consuming the keystroke. Observe the standard circle-and-slash cross out on the first arrow emanating from ProcessCmdKey in Figure 2. Clicking the arrow again lets the keystroke pass. When a keystroke is consumed the output pane notes this by a short red tick between key panels; you can see one between the second and third key panels on the last line of the output pane in Figure 2.
What events are activated—and when—depends on a variety of factors in your application: which control has focus (and therefore the control must be enabled), what type of control, whether the form has KeyPreview enabled, whether the key combination represents a shortcut key, a mnemonic key, or a navigational key, etc. The sandbox therefore includes many commonly available controls (besides just the instrumented ones): checkboxes, combo boxes, buttons, labels, fixed menu items, and context menu items (on the textbox). Most of these therefore do double duty: they both change a setting in the application and also serve as components that have focus in the sandbox when you press a key. Table 3 is a quick reference guide to activate the element types—first introduced in Table 1.
Table 3. Triggering Scenarios in Keystroke Sandbox: The first two columns are identical to Table 1; however, the third column here shows how to trigger each of the elements in the sandbox.
||Keystroke Sandbox implementation
|Open a fixed menu (i.e. on a menu bar)
||Alt + alphanumeric
||Alt+F opens the file menu.
|Mnemonic on a fixed menu item
||The first item in the File menu, Fixed Menu Item, has its mnemonic set to X with no shortcut key set.
|Mnemonic on a context menu item
||The TextBox has a context menu with two items, one with a mnemonic (<SPAN CLASS="PF">M</SPAN>) and the other without. Once you open the context menu, then, pressing m will invoke the Context menu with mnemonic item. The other item defaults to an implicit mnemonic—the first character of the string—so pressing <SPAN CLASS="PF">C</SPAN> will invoke Context menu without mnemonic.
|Mnemonic on another control
||Alt + alphanumeric
||The Load Grid button has its mnemonic set to <SPAN CLASS="PF">G</SPAN>, so pressing Alt+G will click the button.
|Shortcut on a fixed menu item
||The second item in the File menu, Fixed Menu with shortcut, has its shortcut set to the Delete key—which provides a shortcut with just one key. The next item, Fixed Menu with compound shortcut, has a more conventional shortcut combination: Ctrl+B. The final menu item looks and acts almost the same, but actually uses a fake shortcut, to be discussed in the "how to" section towards the end of this article.
|Modify a mouse click
||The Load Grid button loads the grid when clicked, but if you hold down Ctrl when you click it, it empties the grid instead.
|Echo a typed character
||You can type anything into the TextBox, when it's enabled.
— Arrow keys
— Page Up/Page Down
|After typing several lines of text in the TextBox or loading the grid, you can click somewhere inside either control and use these standard navigational keys.
||Click in the separation spinner, and then press the Tab key several times, pausing just briefly between each press. You'll see different output each time as the focus changes from control to control.
|Change program state
||Open the file menu with Alt+F, and then press Escape to close it. You will see output when you open the menu but not when you close it!
||The spacebar toggles state in a CheckBox. Try giving the Key Preview box the focus (by clicking it), and then press the spacebar a couple of times to toggle the state and change the output.