The study of Keydown,keypress and KeyUp, found that there is a little learning between. let's take the following questions to illustrate, if you see these questions you know, then this article you should review it:)
1. What is the order of the three events?
2.KeyDown triggered, KeyUp is not necessarily triggered?
3. Definition of three events
The difference between 4.KeyDown, KeyUp and KeyPress
5. How do I tell if I'm pressing the keypad?
6.PRSCRN key three events can be captured?
7.{Menu Key} and {Start key}keypress can I capture it?
8. How do I capture the system's key combinations?
9. How do I capture prscrn key events?
(a) Key events occur in the following order:
-
KeyDown
-
KeyPress
-
KeyUp
(b) KeyDown trigger, not necessarily trigger KeyUp, when KeyDown pressed, drag the mouse, then will not trigger the KeyUp event.
(iii) Definition
KeyDown: Occurs when a key is pressed while the control has focus.
KeyPress: Occurs when a key is pressed while the control has focus. (The difference between that and KeyDown)
KeyUp: Occurs when a key is released when the control has focus.
(iv) Differences between KeyPress and KeyDown, KeyPress
1.KeyPress is primarily used to capture numbers ( Note: Symbols including shift+ numbers ), letters ( Note: include Case ),
keypad, etc. except f1-12, SHIFT, Alt, Ctrl, Insert, Home, PgUp, Delete, End, PgDn, ScrollLock,
Pause, NumLock, {Menu key}, {Start key}, and ANSI characters outside the arrow key
KeyDown and KeyUp can usually capture keyboards in addition to prscrn all keys (special keys for special keyboards are not discussed here)
2.KeyPress can capture only a single character, KeyDown and KeyUp can capture key combinations.
3.KeyPress can capture the case of a single character
4.KeyDown and KeyUp the keyvalue for a single character capture is a value, that is, the case of a single character cannot be judged.
5.KeyPress does not differentiate between the numeric characters of the keypad and the main keyboard. KeyDown and KeyUp distinguish the numeric characters of the keypad and the main keyboard.
6. Prscrn keys KeyPress, KeyDown and KeyUp cannot be captured.
(v) Determination of system key combinations
When using the keyboard, it is common to use a key combination function similar to Ctrl+shift+alt. How do we determine this?
Can be handled through the KeyUp event (this explains why you don't need to KeyDown, because when you judge KeyDown,
CTRL, Shift, and ALT belong to the pressed state, and then another key is not accurate
Capture key combinations, so the use of KeyDown is not accurately judged by the KeyUp event to determine)
Here is a simple list of CTRL + other key combination judgment code:
private void Form3_keyup (object sender, KeyEventArgs e)
{
if (E.control)
{
MessageBox.Show ("keyup:ctrl+" + e.keyvalue.tostring ());
}
}
(vi) Capturing PRSCRN key events
The PRSCRN key event can be determined by means of a hook, and the hook can get any keyboard event.
Here's a solution to the open source code above CodeProject. We're interested in researching and researching,
Keydown,keypress and KeyUp