First understand the difference between each event
KeyDown: Occurs when a key is pressed when the control has focus
KeyPress: Occurs when a key is pressed when the control has focus
KeyUp: In Occurs when the key is released when the control has focus
1. KeyPress is mainly used to receive ANSI characters such as letters and numbers. The KeyDown and KeyUP event processes can usually capture all keys on the keyboard except PrScrn (special keys of special keyboards are not discussed here
2. KeyPress can only capture a single character, while KeyDown and KeyUp can capture key combinations.
3. KeyPress does not display the physical status of the keyboard (SHIFT key), but only passes a character. KeyPress interprets the upper and lower case forms of each character as different key codes, that is, KeyDown and KeyUp cannot be used. Determine the size of the key's letters. KeyDown and KeyUp interpret the uppercase and lowercase forms of each character with two parameters: keycode — which displays the physical key (returns A and a as the same key) and shift — which indicates the shift of the key. Status and returns one of A or a.
5. KeyPress does not distinguish between the numeric characters of the small keyboard and the main keyboard. KeyDown and KeyUp distinguish the numeric characters of the small keyboard and the main keyboard.
6. , KeyDown, KeyUp events occur when a key is pressed (KeyDown) or released (KeyUp). Since generally pressed keyboard keys are often released immediately (this is different from the mouse), which one to use for these two events. The difference is not big. Moreover, there is another difference between up and the other two: to determine the modified state of the key, you must use up. We can use the keydown event to prevent the user from inputting. For example, a certain input field can only use up. Enter numbers
keyCode of the numeric keys on the keyboard
[48-57] numeric keys
[96-105] numeric keypad
In addition, the Backspace key is allowed to delete
The code is as follows