Write a keyboard intercept program with VB

zhaozj2021-02-11  176

Write a keyboard intercept program with VB

We know that in some programs, there are some shortcuts (such as: SHIFT key minimization, ESC key exit, Ctrl S, Alt x exit, etc.). There have been some introductions to use the Win32 API to do it, but it is too cumbersome, in fact, VB itself has provided us with this feature. Let's create a new form FORM1. For keyboard operations, you can see three events keypress (), keydown, and keyup, I will introduce them to them: KeyPress () event occurs when the user presses and releases an ANSI button ( ANSI is a visible ASCII character 1-127). Syntax Private Sub Object_KeyPress ([Index As Integer,] Keyascii AS Integer "KeyPress event syntax includes the following sections: section describes an object expression that is" Apply to "the list in the list. Index an integer, which is used to uniquely identify a control in the control array (only when the control is set). Keyascii is an integer that returns a standard digital ANSI key code. Keyascii changes it to the object by reference to the object. The key can be canceled when Keyascii is changed to 0, so that the object does not receive the character. Note Objects with focus receives the event. This event can only be received when the keypreview property is set to TRUE. A KeyPress event can reference any of the printed keyboard characters, a combination of characters from a standard alphabet or a few special characters, and a combination of Ctrl keys, and Enter or Backspace keys. The keypress () event process is very useful when intercepting the keystered keystered in the TextBox or ComboBox control. It can immediately test the validity of the keystroke or format it in the character input. Change the value of the Keyascii parameter changes the displayed characters. You can use the following expressions to convert the keyASCII parameter into a character: chr (keyascii) and then perform a string operation, reflecting the characters into a control can be interpreted by this expression: Keyascii = ASC (char) in KeyPress () can not handle the functions may be handled by the KeyDown () and the KeyUp () event: syntax Private Sub object_KeyDown ([index As Integer,] keycode As Integer, shift As Integer) Private Sub object_KeyUp ([index As Integer,] keycode As Integer, Shift AS Integer, keydown and keyup events include the following: section describe an object expression that is an object in the "Apply to" list. Index is an integer that is used to uniquely identify a control in an array of controls (there is only an array of controls). KeyCode is a key code such as VBKEYF1 (F1) or VBKEYHOME (HOME button). Shift is an integer in response to the state of Shift, Ctrl, and Alt keys when the event occurs. SHIFT, CTRL, ALT key corresponds to values ​​1, 2 and 4, respectively. For example: If both the Ctrl and Alt are pressed, the value of the SHIFT is 6. Note For these two events, all of the objects with focus receive all keystrokes. A form can only be focused when there is no visible and valid control.

Although KeyDown () and Keyup () events can be applied to most keys, they are most often applied to: extended characters such as function keys, positioning keys, keyboard modifies, buttons, distinguishable digital keypads and regular numbers Key; use the keydown and keyup event procedures when needed to press and release a key to respond. The following conditions cannot reference the keydown and keyup event: The form has a CommandButton control, and the Default property is set to the Enter key when TRUE. The form has a CommandButton control, and the CANCEL property is set to True, the Tab key, keydown and keyup use two parameters to explain the uppercase form and lowercase form of each character: Keycode - Display the physical key (A and A) As the same key returns) and Shift- Displays the SHIFT KEY key and one of A or A is returned. If you need to test the Shift parameter, you can use this parameter to define the SHIFT constant. This constant has the following value: constant value describes the bit shield of the VBSHiftmask 1 Hift key. The bit shield of the VBCTRLMASK 2 Ctrl key. The bit shield of the Vbaltmask 4 Alt key. This constant is used as a bit shield, which can be used to test any key combination. Note: If the KeyPreView property is set to True, a form is first received on the form. You can create a global keyboard processing routine with the KeyPreview property.

转载请注明原文地址:https://www.9cbs.com/read-5488.html

New Post(0)