onkeypress Events
The onkeypress event occurs when the user presses any printable character on the keyboard, and only the component that receives the keyboard input has the onkeypress event. We often use the onkeypress event to intercept keystrokes entered in edit boxes and combo box components, as well as to immediately test the validity of keystrokes or to format them for character input.
For example, capture the onkeypress event on the Tedit component, determine if the input is lowercase, and if so, convert it to uppercase, the code is as follows:
void __fastcall tform1::edit1keypress (tobject *sender, char &key)
{
if (Key >= ' a ' && key <= ' Z ')
{
Key + = ' A '-' a ';
}
}
When you change the value of key to 0, you can cancel the keystroke, so that the object receives no characters, and we can use this feature to block certain characters. For example, sometimes we only allow users to enter numbers, then add the following code:
void __fastcall tform1::edit1keypress (tobject *sender, char &key)
{
if (Key < ' 0 ' | | Key > ' 9 ')
{
Key = 0;//Cancels the character you just entered
}
}
Note: The onkeypress Event can refer to any printable keyboard character, a character from the standard alphabet or one of the few special characters with the CTRL key combination, and the Enter or BACKSPACE key, but it does not recognize the function key (F1~F12), the next key around, The SHIFT key, the CTRL key, and the ALT key, such as special control keys or key combinations, can be used to capture these keys using the onkeydown and onkeyup events.
Onkeydown/onkeyup Events
The onkeydown event is generated when a key is pressed, and the onkeyup event is generated when the key is released. Although the OnKeyDown and onkeyup events can be applied to most keys, they are most often applied to extended character keys, such as functions, to compensate for the lack of onkeypress events.
For example, the Onkeydown/onkeyup event procedure framework for the Tedit component is as follows:
void __fastcall Tform1::edit1keydown (tobject *sender, WORD &key,
Tshiftstate Shift)
void __fastcall Tform1::edit1keyup (tobject *sender, WORD &key,
Tshiftstate Shift)
Where parameter key returns the virtual key code for the keyboard.
Note: The key in the onkeydown and onkeyup events is word type, which means that the user presses which key on the keyboard, and the key in onkeypress is a char type, which indicates what character the user entered.
The parameter shift provides the state of the Shift,ctrl and ALT keys when the event occurs. The shift values for the Shift,ctrl and ALT keys are Ssshift,ssctrl and Ssalt, respectively. For example, the code that determines whether the Shift,ctrl and ALT keys are pressed is as follows:
void __fastcall Tform1::formkeydown (tobject *sender, WORD &key,
Tshiftstate Shift)
{
String Strinfo ("You just pressed: \ n composite key:");
if (Shift.contains (Ssshift))
Strinfo + = "Shift key";
if (Shift.contains (Ssctrl))
Strinfo + = "Ctrl key";
if (Shift.contains (Ssalt))
Strinfo + = "Alt key";
Strinfo + = "\nascii code:";
Strinfo + = IntToStr (Key);
MessageBox (NULL, Strinfo.c_str (), "informational hint", MB_OK);
}
Note: To have an object produce a onkeydown event, onkeyup event, or onkeypress event, first to get control of the object (that is, with focus), a form is set to True only if it does not have a visual and valid control or KeyPreview property To receive the event. If you press and hold a key on the object and then release it, the above three events are triggered. If there are operations in all three events, the order of execution is: The onkeydown event executes first, the onkeypress event is followed, and the onkeyup event is finally executed.
The event handling of the onkeydown and onkeyup events contains information that controls whether the key Alt,shift or CTRL is pressed when the last keystroke occurred, while the onkeypress event returns only the ASCII characters of the key.
Assuming the user presses the SHIFT+P key, these three keyboard events are produced in the following order:
(1) The onkeydown event is generated when the SHIFT key is pressed.
(2) The onkeydown event is generated when you hold the SHIFT key and press p.
(3) The onkeypress event is generated because the P key is pressed.
(4) Releasing the SHIFT key and P key will produce the onkeyup event.
OnKeyPress interprets the large and lowercase forms of each character as different key codes, which are two different characters. OnKeyDown and onkeyup Use two parameters to explain the uppercase and lowercase forms of each character: The parameter key returns the virtual key of the keyboard, displays the physical key (returns a and a as the same key), and the SHIFT key indicates the state of shift, thus returning a or a one.
Question: How do I decide to press the Shift,ctrl and ALT keys at the same time?
Supplemental: Virtual Key Code
The names of most virtual key codes are defined in the WINUSER.H header file to start with Vk_. The following table lists these names and values (decimal and hexadecimal), and the keys on the IBM compatible machine type keyboards that correspond to the virtual keys.
Of the first four virtual keys, three are the mouse buttons, and you will never get them from the keyboard message, but we can get them from the mouse message. The Vk_cancel code is a virtual key code that includes pressing two keys (Ctrl-break) at the same time. Windows applications typically do not use this key.
Windows also includes the virtual key codes for the letters and number keys on the main keyboard, the A key to the Z key is consistent with the corresponding value of the ASCII code ' a ' to ' Z ', and the 0-key to 9-key is consistent with the corresponding value of the ASCII code ' 0 ' to ' 9 '.
96~111 represents the key on the numeric keypad.
Although most keyboards have 12 function keys, Windows only requires 10, while bit flags have 24.
Decimal 16 Virtual Key IBM compatible keyboard
1 Vk_lbutton Mouse left button
2 Vk_rbutton Mouse Right button
3 Vk_cancel Ctrl-break
4 Vk_mbutton Mouse middle button
8 Vk_back Backspace
9 Vk_tab TAB
0C vk_clear CLEAR Key, Num lock on numeric keypad when off 5
0D Vk_return Enter
Ten Vk_shift SHIFT
Vk_control Ctrl
Vk_menu Alt
Vk_pause PAUSE
Vk_capital caps Lock
1 b vk_escape ESC
Vk_space Spacebar
Vk_prior Page up
Vk_next Page Down
Vk_end END
Vk_home HOME
Notoginseng Vk_left left Arrow
Vk_up Up ARROW
Vk_right RIGHT Arrow
Vk_down DOWN ARROW
Vk_select SELECT Key
2A vk_print PRINT Screen key
2B Vk_execute EXECUTE Key
2C vk_snapshot SNAP SHOT key
2D Vk_insert INSERT Key
2E Vk_delete DELETE Key
2F Vk_help Help Key
48-57 30-39 no 0 to 9 on the main keyboard
65-90 41-5a no A to Z
5B Vk_lwin left Windows key
5C Vk_rwin right Windows key
5D Vk_apps Applications Key
96-105 60-69 vk_numpad0 to vk_numpad9 NumLock 0 to 9 on numeric keypad when open
106 6A vk_multiply on the numeric keypad *
107 6B Vk_add on the digital keypad +
108 6C Vk_separator The ENTER key on the numeric keypad
109 6D vk_subtract on the numeric keypad-
6E Vk_decimal on the numeric keypad.
111 6F Vk_divide on the digital keypad/
112-121 70-79 vk_f1 to vk_f10 function key F1 to F10
122-135 7a-87 vk_f11 to Vk_f24 function key F11 to F24
144 Vk_numlock Num Lock
145 Vk_scroll SCROLL Lock
This article is from the "No Water Fish" blog, please be sure to keep this source http://javaqun.blog.51cto.com/10687700/1704224
Analysis of onkeypress events and Onkeydownonkeyup events