Home > Software engineering >  Vb keyup keypress, what is the difference between keydown event has occurred?
Vb keyup keypress, what is the difference between keydown event has occurred?

Time:09-20

Vb keyup keypress, what is the difference between keydown event has occurred?

CodePudding user response:

Keypress events only for printable characters "key", is a character key has been pressed, and there have been some "early treatment after the trigger,"

Keydown event has occurred is any key on the keyboard by pressing the trigger (except individual "system level function" key),
And there is a "key code" (virtual keying yards) identifier is which key to press,
Don't let go, after most of the key pressed delay after a certain period of time, will continue to trigger the keydown event has occurred,

Keyup event. It's similar with the keydown event has occurred, just is to "let go" trigger,
And it is impossible to "let go" continue to trigger, also is to let go of a can trigger a,

Keydown, keyup these two events, in view of the "key", not only in view of the "character", such as:
1. You are in the main press the number keys on the keyboard region, with small keyboard on the right area, press the number keys 1, their KeyCode is not equal,
2. Separate press the key characters A, with "first pressing Shift, then press characters A",
"After A" character key press KeyCode, both cases are the same,

CodePudding user response:

First, they trigger time sequence is KeyDown -- KeyPress KeyUp,

Secondly, the parameters of different events,

KeyPress KeyAscii only one parameter, such as you press the capital returns $65 - A Asc (" A ")=65, that is, it applies only to ASCII code key,

KeyDown and KeyUp has two parameters: the KeyCode and Shift,
KeyCode key code, is a number, but there are some key code VB constants can help to you: vbKeyA, vbKeyW, vbKeyF1, vbKeyPgUp, etc., can you don't have to remember their values,
Shift said Ctrl, Shift and Alt pressed down what or which a few, there are also special constants:
VbShiftMask=1
VbCtrlMask=2
VbAltMask=4
You can use (Shit And vbShiftMask) to check whether the Shift key press, And so on,

CodePudding user response:

Is actually a button press three action to resolve

CodePudding user response:


KeyDown, KeyUp event


These events is when an object has the focus when press release (KeyDown) or (KeyUp) occurs when a key, (KeyPress events should be used to explain the ANSI character,,)

Syntax

Private Sub Form_KeyDown (keycode As an Integer, shift the As an Integer)

Private Sub object_KeyDown (index As an Integer, keycode As an Integer, shift the As an Integer)

Private Sub Form_KeyUp (keycode As an Integer, shift the As an Integer)

Private Sub object_KeyUp (index As an Integer, keycode As an Integer, shift the As an Integer)

KeyDown and KeyUp event includes the following parts:

Part of the description
Object to an object expression, and its value is "applied" in the list of an object,
Index is an integer, which is to a unique identifier in a control array of controls,
Keycode is a key code, such as vbKeyF1 (F1) or vbKeyHome (HOME button), and to specify the key code, using Visual Basic (VB) in the object browser object library of constant,
Shift is when the incident response shift, CTRL and ALT key state an integer shift parameter is a domain, it USES the least bit response shift key (0), CTRL key (1) and (2) the ALT key, these correspond to the value of 1, 2 and 4, can be based on some, all or nothing Settings to point to have some, all or zero key is pressed, for example, if the CTRL and ALT these two keys are pressed, the shift has a value of 6,


Description

For these two events, with a focus on objects receive all keystrokes, only in a form does not have a visual and effective control can get focus, although the KeyDown and KeyUp event can be applied to most of the key, or they are most often used in:

Extended character keys such as function keys, such as


The positioning key,


The combination of the keyboard modifier keys and buttons,


Difference between the number keys, numeric keypad and conventional
In the need to press and release a key response, can use KeyDown and KeyUp event process,

The following situations cannot reference KeyDown and KeyUp event:

Form a CommandButton control, and the Default attribute is set to True, the ENTER key,


Form a CommandButton control and Cancel attribute set to True when the ESC key,


The TAB key,
KeyDown and KeyUp explained in two parameters of each character uppercase and lowercase: keycode - showing physical keys (A and A as the same key return) and shift shift + key status and return to A or A one,

If you want to test the shift parameters, can use the parameters defined in each shift constant, the constant in any of the following values:

Constant value description
VbShiftMask 1 bit mask of the SHIFT key,
VbCtrlMask 2 CTRL key bit mask,
VbAltMask 4 ALT key bit mask,


This constant is used as a shield, it can be used to test any key combination,

Testing a condition, the first to distribute each result to a temporary integer variables, And then compare shift with a shield, the following example, available And operator And shift parameter to test condition is greater than zero, the condition that the correct key is pressed:

ShiftDown=(Shift And vbShiftMask) & gt; 0

According to this case in a test any conditions in the process of combination:

If ShiftDown And CtrlDown Then

Note if KeyPreview property is set to True, the controls on a form before the form to receive this event, the available KeyPreview attributes to create a global keyboard processing routines,

CodePudding user response:

I came in just to see zhao four copy what the teacher again

CodePudding user response:


Form in a text box, and then paste the following code, then each key press in the past, look at the output of the immediate window differences and display order, immediately see


 

Option Explicit


Private Sub Text1_KeyDown (KeyCode As an Integer, Shift the As an Integer)
The Debug. Print "keydown" KeyCode, Shift
End Sub

Private Sub Text1_KeyPress (KeyAscii As Integer)
The Debug. Print "keypress," KeyAscii
End Sub

Private Sub Text1_KeyUp (KeyCode As an Integer, Shift the As an Integer)
The Debug. Print "keyup" KeyCode, Shift
End Sub

CodePudding user response:

KeyDown, KeyUp events, these events is when an object has the focus when the press release (KeyDown) or (KeyUp) occurs when a key, ( to explain the ANSI character, should use the KeyPress event,


KeyDown, KeyUp event example
This example demonstrates a response F2 and ALT, SHIFT or CTRL key combination of associated generic keyboard handler, the key sequence in Visual Basic (VB) in the object browser object database, try this example, the code can be pasted into a form containing a TextBox control declaration section, and then press F5 and ALT + F2, SHIFT + F2, or CTRL + F2 key combination,

 Private Sub Text1_KeyDown (KeyCode As an Integer, Shift the As an Integer) 
Dim ShiftDown AltDown, CtrlDown, Txt
ShiftDown=(Shift And vbShiftMask) & gt; 0
AltDown=(Shift And vbAltMask) & gt; 0
CtrlDown=(Shift And vbCtrlMask) & gt; 0
If KeyCode=vbKeyF2 Then 'shows that key combination.
If ShiftDown And CtrlDown And AltDown Then
Txt="SHIFT + CTRL + ALT + F2."
ElseIf ShiftDown And AltDown Then
Txt="SHIFT + ALT + F2."
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related