KeyDown, KeyUp, KeyPress difference

Source: Internet
Author: User

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. 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.
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 processes a keyboard message, the preProcessMessage method invokes one or more methods that can be overridden to handle special character and physical keys.

/*********************/

Key events occur in the following order:
KeyDown
KeyPress
KeyUp
After the KeyDown is triggered, the keyup is not necessarily triggered, and when the KeyDown is pressed, the mouse is dragged and the KeyUp event is not triggered.
Defined
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.
The difference between KeyPress and KeyDown and KeyPress


1. KeyPress is mainly 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
2, KeyDown and KeyUp can usually capture the keyboard in addition to PRSCRN all keys (here does not discuss special keyboard special keys)
3. KeyPress can only capture a single character
4, KeyDown and KeyUp can capture key combinations.
5. KeyPress can capture the case of a single character
6, KeyDown, and KeyUp the keyvalue for a single character capture is a value, that is, the case of a single character cannot be judged.
7. KeyPress does not differentiate between the numeric characters of the keypad and the main keyboard.
8, KeyDown and KeyUp distinguish the numeric characters of the keypad and the main keyboard.
9, which prscrn keys KeyPress, KeyDown and KeyUp can not be captured.
/*******************/

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, in a multi-line text box to enter characters, why we do not define a keyboard response event, but the keyboard responds, the character entered into the text box? Actually 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.

From:http://www.cnblogs.com/poren/articles/1536724.html

--------------------------------------Other Information:

protected override bool processCmdKey (ref Message msg, Keys keyData)
{
Const int wm_keydown = 0x100;
const int wm_syskeydown = 0x104;
if (msg. MSG = = Wm_keydown) | | (Msg. MSG = = Wm_syskeydown))
{
switch (keyData)
{
Case keys.down:
this. Parent.text = "Down key has been captured";
break;
Case Keys.up:
this. Parent.text = "Up key has been captured";
break;
Case Keys.left:
this. Parent.text = "Left arrow has been captured";
break;
Case Keys.right:
this. Parent.text = "Right ARROW key has been captured";
break;
Case Keys.home:
this. Parent.text = "Home key has been captured";
break;
Case Keys.end:
this. Parent.text = "End key has been captured";
break;
}
}
return base. processCmdKey (ref msg, keyData);
}
}

Add: To use e.handled=true screen input, the ordinary key should be placed in the KeyPress event, and control keys, etc. should be in the KeyDown.

(Turn) KeyDown, KeyUp, KeyPress difference

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.