We know that in some programs, there are some shortcuts (such as: Shift key to minimize, ESC key to exit, Ctrl+S to save, Alt+x to exit, etc.). There have been some introductions in the past that it can be done using the Win32 API, but it is too cumbersome. In fact, VB itself has already provided us with this function.
Let's create a new form Form1. For keyboard operations, we can see three events: KeyPress(), KeyDown and KeyUp. I will introduce them respectively below:
The KeyPress() event occurs when the user presses and releases an ANSI key (ANSI is the visible ASCII characters 1-127).
grammar
partial description
object is an object expression whose value is "applied to" an object in the list.
index is an integer that uniquely identifies a control in the control array (only available for control arrays).
keyascii is an integer that returns a standard numeric ANSI key code. Keyascii passes an object by reference, making changes to it possible.
Send a different character. Changing keyascii to 0 cancels the keystroke so that the object receives no characters.
illustrate
The object with focus receives this event. A form can receive this event only if the KeyPreview property is set to True. A KeyPress event can reference any printable keyboard character, a character from the standard alphabet or one of a few special characters in combination with the CTRL key, and the ENTER or BACKSPACE key. The KeyPress() event procedure is useful when intercepting keystrokes entered in a TextBox or ComboBox control. It can instantly test the validity of keystrokes or format characters as they are entered. Changing the value of the keyascii parameter will change the characters displayed.
You can use the following expression to convert the keyascii parameter to a character:
illustrate
For both events, the focused object receives all keystrokes. A form can only gain focus if it has no visible and valid controls. Although the KeyDown() and KeyUp() events can be applied to most keys, they are most commonly applied to: extended character keys such as function keys, navigation keys, keyboard modifier keys and key combinations, distinguishing numeric keypads from regular numbers Key; when you need to respond to both pressing and releasing a key, you can use the KeyDown and KeyUp event procedures.
The KeyDown and KeyUp events cannot be referenced in the following situations: the form has a CommandButton control and the ENTER key when the Default property is set to True. The form has a CommandButton control, and when the Cancel property is set to True, the ESC key, TAB key, KeyDown and KeyUp use two parameters to interpret the uppercase and lowercase forms of each character: keycode - displays the physical key (replace A and a Returns as the same key) and shift-displays the status of the shift+key key and returns one of A or a.
If you need to test the shift parameter, you can use the shift constant defined in the parameter. This constant has the following values:
Constant value description
vbShiftMask 1 HIFT key bit
shield.
VbCtrlMask 2 CTRL key
bit mask.
VbAltMask 4 ALT key bits
shield.
This constant acts as a bit mask, which can be used to test any key combination.
Note: If the KeyPreview property is set to True, a form receives this event before the controls on the form. You can use the KeyPreview property to create global keyboard handling routines.
With the above knowledge, we can create a perfect program with shortcut keys. For example, in a program we need to use Ctrl+S to save, Shift to minimize, Alt+X and ESC to exit:
First start VB and select a new EXE file, pull a TextBox on the Form1 form, and set the KeyPreview property of Form1 to True, double-click Form1, select the KeyPress event of the Form, and enter the following code: