Recently used WinForm to do a Tetris game, which in the handling of keyboard events, a bit of a problem, after consulting data and repeated debugging, summary keyboard events are as follows:
1. The response sequence for keyboard events is KeyDown>KeyPress>KeyUp;
2.KeyDown can respond to all keys,KeyPress can respond in addition to the arrow keys and F1, F2 ... the other keys;
3. Before WndProc processing, thepreprocessmessage method invokes multiple methods to preprocess the keyboard message;
4.KeyDown preprocessing method: (according to the Order of execution)
(1)processCmdKey, this method handles the command key, and the command key has a higher precedence than the regular key. If this method returns True, the key message will not be dispatched and the key event will not occur. If this method returns false, isInputKeyis called.
(2)isInputKey, If this method returns True, indicates that the control is a regular character, and the KeyDown event is raised. If this method returns False, processDialogKeyis called.
(3)processDialogKey, This method handles physical keys that implement special functions within a control, such as switching focus between a control and its parent. If the middle control does not process the key, the parent control's processDialogKeyis called 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 occurs . (I do the program is the problem here, there are two buttons on the window, then the arrow keys can not respond to the KeyDown event.) After overriding the processDialogKey method, the next breakpoint debugging discovers that the parent class has a method with the same name that returns a value of true, which indicates that preprocessing has completed. This should be the window by default the direction key to handle the button's focus switch, changed to directly return false after the KeyDown event can be responded to, but not the focus of the switch button. )
5.KeyPress Preprocessing Method:
(1)isInputChar, if the character is a normal character, this method returns true, and the KeyPress event is raised, and no preprocessing is done. Otherwise, processDialogCharwill be called.
(2)processDialogChar, similar to processDialogKey, will be raised along the hierarchy of the control with this method. If the control is a container control, this method examines the mnemonic by invoking the processmnemonic of the control and its child controls. If processDialogChar returns true, the KeyPress event does not occur.
6. After the message arrives at WndProc , it is processed by a set of methods that can be overridden, which return values are Boolean. a return value of TRUE indicates that message processing is complete, that it will not be passed to the base control or the parent control for further processing, otherwise the message will remain in the message queue, possibly with the base control or parent control being further processed.
(1)ProcessKeyMessage, this method handles all keyboard messages that are accepted by the WndProc method of the control.
(2)processKeyPreview, this method sends a keyboard event to the parent control. If the return value is true, no keyboard events are generated, otherwise the ProcessKeyEventArgs is called
(3)ProcessKeyEventArgs, This method raises KeyDown,KeyPress , and KeyUp events as needed.
The above is my summary of WinForm keyboard events.
WinForm Keyboard Events