Keydown,keypress and KeyUp explanation (turn)

Source: Internet
Author: User

Keydown,keypress and KeyUp detailed

Windows Forms handles keyboard input in response to Windows messages by raising keyboard events, and most Windows Forms applications handle keyboard input exclusively by handling keyboard events.

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 values accompanying the WM_KEYDOWN and wm_syskeydownwindows messages. In addition, program developers can detect most physical keystroke operations by handling KeyDown or KeyUp events. The character key is a subset of the keys enumeration, which corresponds to the values accompanying the WM_CHAR and Wm_syschar Windows messages, and if a character is obtained by combining keystrokes, the character can be detected by processing the KeyPress event.

2. 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.

Note: After the KeyDown is triggered, the keyup is not necessarily triggered, and when the KeyDown is pressed, the KeyUp event will not be triggered when the mouse is dragged.

3. The Order of keyboard events

There may be 3 keyboard-related events on a control, the following is the general order in which these events occur:

The user presses the "a" key, the key is preprocessed and dispatched, and the KeyDown event occurs.
The user presses the "a" key, the key is preprocessed and dispatched, and the KeyPress event occurs.
The user releases the "a" key, the key is preprocessed and dispatched, and the KeyUp event occurs.

4. Preprocessing of Keys

Like other messages, keyboard messages are handled in the WndProc method of a form or control. Before a form or control processes a keyboard message, the preProcessMessage method invokes one or more methods that can be overridden to handle special character and physical keys.


5. The difference between KeyPress and KeyDown, KeyPress

KeyPress is primarily used to capture numbers (note: symbols including shift+ numbers), letters (note: include capitalization), keypads, 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 keys.
KeyDown and KeyUp can usually capture keyboards in addition to prscrn all keys (special keys are not discussed here).
KeyPress can only capture a single character
KeyDown and KeyUp can capture key combinations (the Control.modifierkeys property is available at any time to get the state of shift, ALT, and CTRL, which is useful for controls that do not support KeyDown events).
KeyPress can capture the case of a single character
The keyvalue of KeyDown and KeyUp for a single character capture is a value, that is, the case of a single character cannot be judged. (The state of CapsLock can be judged by the new control.iskeylocked (Keys.capslock) method in. NET 2.0, similar to Scroll lock and num Lock can also be judged by the control.iskeylocked () method).
The 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.
where Prscrn keys KeyPress, KeyDown, and KeyUp are not captured.

The above instructions can be tested by 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. Get key State

To obtain each key state, use the Getkeystate () and Getasynckeystate () methods, 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 have not been pressed since, the last call.";
Break
Case 1:
LbL. Text = "D is not currently pressed, but had been pressed since the last call.";
Break
case-32767:
LbL. Text = "D is currently pressed.";
Break
}

So what's the difference between the two? Checked, it was mentioned that the result of Getkeystate is an image of the key state on the keyboard at the time of acquiring the current message, that is, the state does not change with the key's actual key state. In fact, it is often sufficient to get such non-real-time results. Because it's just a table, it's faster, but never write the following code.

while (Getkeystate (Nvirkey))

{

code here

}

To get a real-time state of a key, you should use Getasynckeystate, a function that gets real-time information by querying driver. But this function is slower than getkeystate. (This is not quite clear ...) )


7. System Combination Key Determination

When using the keyboard, it is common to use a key combination function similar to Ctrl+shift+alt. How do we determine this?

Through the KeyUp event can be handled (here to explain why not KeyDown, because in the decision KeyDown, CTRL, Shift and ALT belong to hold the state, and then add another key is not accurately capture the key combination, so use KeyDown Can not be accurately judged by the KeyUp event to determine)

Here is a simple list of CTRL + the other 7 key combination judgment code:
private void Form3_keyup (object sender, KeyEventArgs e)
{
if (E.control)
{
MessageBox.Show ("keyup:ctrl+" + e.keyvalue.tostring ());
}
}


8. 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. Everyone is interested in researching it yourself: Global System Hooks in. NET.


In fact, when we press a key, KeyDown and KeyPress are all going to happen, and two people look no different. But there is actually a real difference between the two.

For example: Enter a character in a multiline text box, why don't we define a keyboard response event, but the keyboard responds, and the character is entered into the text box? In fact, I think because this event is a system default message response, it is keypress. If you define your own response to KeyPress, then the program executes the program you defined and then completes the response to the system's required display character event, but if you define the KeyPress event yourself, such as: private void textbox1_ KeyPress (object sender, System.Windows.Forms.KeyPressEventArgs e) Adds a statement inside: E. Handled=true, the system requires that the display character response process not be executed, because the statement means that the message response has been completed. However, if you add this statement in Keydown,keyup, the display string and other messages will still occur, indicating that the system display string and Keydown,keyup are not related, and for carriage return and backspace message response is controlled by KeyDown, Add that statement and the return line will not be executed, and the backspace response will not be executed.

Comprehensive: to block the keyboard of the input of a character, you can add a statement inside the KeyPress, and to block the carriage return and backspace can be added in the KeyDown inside the statement.

==============================================================

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?

(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 captures 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.

Keydown,keypress and KeyUp explanation (turn)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.