Keydown, keypress and keyup

Source: Internet
Author: User

Content Reprinted from http://www.cnblogs.com/stan0714/archive/2008/10/12/1309573.html

Recently I am working onProgramTo refresh F5 in the window.

After studying keydown, keypress, and keyup, we found that there is still some knowledge between them.

Let's explain it with the following questions. If you know these questions, this articleArticleYou should review it :)

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)

Here, we will give a simple example of the combination of Ctrl + other keys.Code:

Code
Private void form3_keyup (Object sender, keyeventargs E)
{
If (E. Control)
{
MessageBox. Show ("keyup: Ctrl +" + E. keyValue. tostring ());
}
}
 

(6) Capture prscrn key events
You can use a hook to determine the prscrn button event. The Hook can obtain any keyboard event.

Here we provide the openSource code. If you are interested, study it yourself :).

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.