Keydown, keypress, and keyup
Windows Forms process keyboard input by triggering a keyboard event to respond to Windows messages. Most Windows Forms applications process keyboard input exclusively by handling Keyboard Events.
1. Key type
In Windows form, the keyboard is marked as a virtual key code represented by the byte keys enumeration. Using the keys enumeration, you can combine a series of buttons to generate a single value, which corresponds to the values of wm_keydown and wm_syskeydownwindows messages. In addition, program developers can detect most physical button operations by handling keydown or keyup events. The character keys are subsets of the keys enumeration. They correspond to the values of wm_char and wm_syschar Windows messages. If a character is obtained by combining the keys, you can detect this character by handling the keypress event.
2. Definition
Keydown: The Key is pressed when the control has focus.
Keypress: The Key is pressed when the control has focus. (The difference between the following and keydown)
Keyup: when the control has focus, the key is released.
Note: After keydown is triggered, keyup is not necessarily triggered. When keydown is pressed, drag the mouse to trigger the keyup event.
3. Sequence of Keyboard Events
Three keyboard-related events may appear on a control. The following is the general sequence of these events:
Press the "A" key to pre-process and schedule the key, and a keydown event occurs.
Press the "A" key to pre-process and schedule the key, and a keypress event occurs.
When you release the "A" key, the key is pre-processed and scheduled, and a keyup event occurs.
4. Key preprocessing
Like other messages, keyboard messages are processed in the wndproc method of the form or control. Before a form or control processes a keyboard message, the preprocessmessage method calls one or more methods that can be overwritten to process special character keys and physical keys.
5. Differences between keypress, keydown, and keypress
Keypress is mainly used to capture numbers (Note: including SHIFT + digit symbols) and letters (Note: including uppercase and lowercase), keypad, in addition to F1-12, shift, ALT, Ctrl, insert, home, pgup, delete, end, pgdn, scrolllock, pause, numlock, {menu key}, {START key} and ANSI characters outside the direction key.
Keydown and keyup can usually capture all the buttons except prscrn (the special keys of the special keyboard are not discussed here ).
Keypress can only capture a single character
Keydown and keyup can capture the combination of keys (you can use the control. modifierkeys attribute to get the status of shift, ALT, and CTRL at any time, which is very useful for controls that do not support keydown events ).
Keypress can capture the case sensitivity of a single character
Keydown and keyup are values for the keyValue captured by a single character, that is, the case of a single character cannot be determined. (You can use. NET 2.0 adds control. iskeylocked (keys. capslock) method to determine the status of capslock. Similarly, scroll lock and num lock can also be controlled. iskeylocked () method ).
Keypress does not distinguish between a keypad and a keyboard's numeric characters.
Keydown and keyup distinguish between numbers on the keypad and the primary keypad.
The prscrn buttons keypress, keydown, and keyup cannot be captured.
The preceding instructions can be tested using some simple code, for example:
Private void txtkey_keypress (Object sender, keypresseventargs E)
{
Label1.text = "Key press:" + E. keychar. tostring ();
}
Private void txtkey_keydown (Object sender, keyeventargs E)
{
Label2.text = "Key CoDE: "+ E. keycode. tostring ();
Label2.text + = "\ nkey value:" + E. keyValue. tostring ();
Label2.text + = "\ nkey daTa: "+ E. keydata. tostring ();
}
6. Obtain the button status
The getkeystate () and getasynckeystate () methods are used to obtain the key States, as follows:
[DllimpORT ("user32.dll")]
Private Static extern short getkeystate (system. Windows. Forms. Keys key );
......
If (getkeystate (Keys. insert) = 1)
{
// Overwrite mode is on.
}
Else
{
// Insert mode is on.
}
[DllimpORT ("user32.dll")]
Private Static extern short getasynckeystate (system. Windows. Forms. Keys key );
...........
Short state = getasynckeystate (Keys. D );
Switch (state)
{
Case 0:
LBL. Text = "d has not been pressed since the last call .";
Break;
Case 1:
LBL. Text = "D is not currently pressed, but has been pressed since the last call .";
Break;
Cases-32767:
LBL. Text = "d is currently pressed .";
Break;
}
So what is the difference between the two? Some people mentioned that the result of getkeystate is an image of the key status on the keyboard when the current message is obtained, that is, this status will not change as the actual button status changes. In fact, it is enough to obtain such non-real-time results. Because it is only a Table query, it is fast, but the following code cannot be written.
While (getkeystate (nvirkey ))
{
// CODe here
}
To obtain the real-time state of a key, use getasynckeystate. This function retrieves real-time information by querying the driver. However, this function is slower than getkeystate. (This is not clear yet ···)
7. Determination of system key combinations
When using the keyboard, the key combination function similar to Ctrl + Shift + ALT is usually used. How can we determine this?
The keyup event can be used for processing (here we will explain why keydown is not required, because Ctrl, shift, and ALT are always pressed when determining keydown, then, if you add another key, you cannot accurately capture the key combination. Therefore, you cannot accurately determine the key combination by using the keydown event)
Here is a simple combination of Ctrl + other 7 keys:
Private void form3_keyup (Object sender, keyeventargs E)
{
If (E. Control)
{
MessageBox. Show ("keyup: Ctrl +" + E. keyValue. tostring ());
}
}
8. Capture prscrn button events
You can use a hook to determine the prscrn button event. The Hook can obtain any keyboard event.
The open-source code solution on codeproject is provided here. If you are interested, study global system hooks in. net.
In fact, when we press the next key, keydown and keypress will both happen, and there seems to be no difference between the two. However, there are substantial differences between the two.
For example, if you enter a character in a multi-line text box, why didn't we define a keyboard response event, but the keyboard responded, and entered the character into the text box? In fact, I think this event is the default message response of the system, and it is keypress. If you have defined your own response for keypress, the program will first execute the program you defined, and then complete the response process for displaying character events as required by the system, however, if you define a keypress event such as private void textbox1_keypress (Object sender,
System. windows. forms. add the statement E. handled = true; then the system requires that the display of the character response process will not be executed, because this statement means that the message response has been completed. However, if you add this statement in keydown and keyup to display messages such as strings, the system displays strings and keydown, and keyup is irrelevant, the keydown control controls the return and return message responses. if you add the Statement, the line feed will not be executed, and the return response will not be executed.
In summary: to shield the input of a character on the keyboard, you can add a statement in keypress, and add a statement in keydown to shield the carriage return and return.
========================================================== ======================================
1. What is the sequence of the three events?
2. Does keyup be triggered after keydown is triggered?
3. Definitions of three events
4. Differences between keydown, keyup, and keypress
5. How do I differentiate whether a keypad is used?
6. Can three prscrn events be captured?
7. Can I capture the {menu key} and {START key} keypress?
(1) key events occur in the following order:
Keydown
Keypress
Keyup
(2) After keydown is triggered, keyup may not be triggered. When keydown is pressed, drag the mouse to trigger the keyup event.
(3) Definition
Keydown: The Key is pressed when the control has focus.
Keypress: The Key is pressed when the control has focus. (The difference between the following and keydown)
Keyup: when the control has focus, the key is released.
(4) differences between keypress, keydown, and keypress
1. keypress is mainly used to capture numbers (Note: including SHIFT + digit symbols) and letters (Note: including uppercase and lowercase), keypad, in addition to F1-12, shift, ALT, Ctrl, insert, home, pgup, delete, end, pgdn, scrolllock, pause, numlock, {menu key}, {START key} and ANSI characters outside the direction key
Keydown and keyup can usually capture all the buttons on the keyboard except prscrn (the special keys on the special keyboard are not discussed here)
2. keypress can only capture a single character
Keydown and keyup can capture the combination of keys.
3. keypress can capture the case sensitivity of a single character
4. Both keydown and keyup have a value for the keyValue captured by a single character, that is, the case of a single character cannot be determined.
5. keypress does not distinguish between keypad and keypad numeric characters.
Keydown and keyup distinguish between numbers on the keypad and the primary keypad.
6. The prscrn keys keypress, keydown, and keyup cannot be captured.
//////////////////////////////////////// ///////////////////////////////
I have studied the knowledge of keydown, keypress, and keyup. Let's explain it with the following questions:
1. What is the sequence of the three events?
2. Does keyup be triggered after keydown is triggered?
3. Definitions of three events
4. Differences between keydown, keyup, and keypress
5. How do I differentiate whether a keypad is used?
6. Can three prscrn events be captured?
7. Can I capture the {menu key} and {START key} keypress?
8. How to capture system Keys?
9. How can I capture the prscrn button event?
(1) key events occur in the following order:
Keydown
Keypress
Keyup
(2) After keydown is triggered, keyup may not be triggered. When keydown is pressed, drag the mouse to trigger the keyup event.
(3) Definition
Keydown: The Key is pressed when the control has focus.
Keypress: The Key is pressed when the control has focus. (The difference between the following and keydown)
Keyup: when the control has focus, the key is released.
(4) differences between keypress, keydown, and keypress
1. keypress is mainly used to capture numbers (Note: including SHIFT + digit symbols) and letters (Note: including uppercase and lowercase), keypad, in addition to F1-12, shift, ALT, Ctrl, insert, home, pgup, delete, end, pgdn, scrolllock, pause, numlock, {menu key}, {START key} and ANSI characters outside the direction key
Keydown and keyup can usually capture all the buttons on the keyboard except prscrn (the special keys on the special keyboard are not discussed here)
2. keypress can only capture a single character
Keydown and keyup can capture the combination of keys.
3. keypress can capture the case sensitivity of a single character
4. Both keydown and keyup have a value for the keyValue captured by a single character, that is, the case of a single character cannot be determined.
5. keypress does not distinguish between keypad and keypad numeric characters.
Keydown and keyup distinguish between numbers on the keypad and the primary keypad.
6. The prscrn keys keypress, keydown, and keyup cannot be captured.
(5) Determination of system key combinations
When using the keyboard, the key combination function similar to Ctrl + Shift + ALT is usually used. How can we determine this?
The keyup event can be used for processing (here we will explain why keydown is not required, because Ctrl, shift, and ALT are always pressed when determining keydown, then, if you add another key, you cannot accurately capture the key combination. Therefore, you cannot accurately determine the key combination by using the keydown event)
(6) Capture prscrn key events
You can use a hook to determine the prscrn button event. The Hook can obtain any keyboard event.
Reference from: http://blog.163.com/lidawei201@yeah/blog/static/760738552010112321937775/