Windows Forms handles keyboard input to respond to Windows messages by raising keyboard events, and most Windows Forms applications handle keyboard input exclusively by handling keyboard events. To get a better grip on keyboard handling in Windows Forms, program developers should understand how keyboard messages work to implement more advanced keyboard input schemes (for example, intercept keys before they reach the control). This section provides details about keyboard processing operations in Windows Forms.
1. Type of key
Windows Forms identifies keyboard input as the virtual key code represented by the bitwise Keys enumeration. Using the Keys enumeration, you can synthesize a series of keystrokes to generate a single value that corresponds to the value that accompanies the Wm_keydown and wm_syskeydownwindows messages. In addition, program developers can detect most physical key actions by handling KeyDown or KeyUp events. The character keys is a subset of the keys enumeration that corresponds to the values associated with WM_CHAR and Wm_syschar Windows messages, which can be detected by handling KeyPress events If a character is obtained by combining keystrokes.
2. Sequence of keyboard events
There may be 3 keyboard-related events on one control, and the following is the general order in which these events occur:
When the user presses the "a" key, the key is preprocessed and scheduled, and the KeyDown event occurs.
When the user presses the "a" key, the key is preprocessed and scheduled, and the KeyPress event occurs.
The user releases the "a" key, which is preprocessed and scheduled, and the KeyUp event occurs.
3. Preprocessing of Keys
Like other messages, keyboard messages are handled in the WndProc method of a form or control. Before a form or control handles keyboard messages, the preProcessMessage method invokes one or more methods that can be overridden to handle special character keys and physical keystrokes. Lists the actions that are being performed and the associated methods that appear, in the order in which the KeyDown event and the KeyPress event preprocessing methods appear.
Table KeyDown Event preprocessing
specific action |
call method |
description |
|
processcmdkey |
|
|
isinputkey |
|
TD style= "BORDER:0;PADDING:0;" > Check whether the key is a navigation key (ESC, Tab, enter, or arrow key)
This method handles the inside of the control realA physical key that is now a special feature, such as switching focus between the control and its parent. If the middle control does not process the key, the processDialogKey of the parent control is invoked until the topmost control in the hierarchy. If this method returns True, preprocessing is completed and key events are not generated. If this method returns false, the KeyDown event |
will occur
Table KeyPress Event preprocessing
Specific operations |
Call method |
Description |
Check if the key is a normal character that the control should handle |
isInputChar |
If the character is a normal character, this method returns True, and the KeyPress event is raised, and preprocessing is no longer performed. Otherwise, the processDialogChar is invoked |
Check whether the character is a mnemonic (such as OK (&o) on the button) |
processDialogChar |
Similar to processDialogKey, this method is raised along the hierarchy of the control. If the control is a container control, this method checks the mnemonics by calling the processmnemonic of the control and its child controls. If processDialogChar returns True, the KeyPress event does not occur |