This chapter describes the lighted event process of keyboard and mouse.
KeyPress event
KeyPress events occur when a key on the keyboard is depressed
Precise Description: Press a key and we will trigger the KeyPress event with focus at this time.
The input focus can only be on one control. If the form has no active or visible control, the input focus is on the form.
We can use vb to automatically generate a keypress event like this
Explain:
- keyAscii is used for individual controls. keyAscii is the ASCII code of the key.
Example
Private Sub Text1_Key Press(KeyAscii As Integer) if KeyAscii < 48 Or KeyAscii > 57 Then Beep KeyAscii = 0 End Sub
Enter 0 (48) ~ 9 (57) only
Private Sub Text1_KeyPress(KeyAscii As Integer) If KeyAscii < 48 Or KeyAscii > 57 Then Beep KeyAscii = 0 End If KeyAscii = 0 End Sub Private Sub Text2_KeyPress(KeyAscii As Integer) If KeyAscii >= 65 And KeyAscii <= 122 Then KeyAscii = 42 End If End Sub Private Sub Text3_KeyPress(KeyAscii As Integer) If KeyAscii = 13 Then Printer.Print Text3.Text End If KeyAscii = 0
When learning database, the video screen introduces a method called abstraction and instantiation comparison.
10.1 Writing Password Program
Private Sub Form_Load() Text1.Text = "" Text1.FontSize = 10 Label1.FontSize 12 Label1.FontBold = True Label1.FontName = "Official script" Label1.Caption = "please input password" End Sub Private Sub Text1_KeyPress(KeyAscii As Integer) Private Sub Text1_KeyPress(KeyAscii As Integer) Static PWord As String Static Counter As Integer Static Numberoftries As Integer If Numberoftries = 12 Then End Counter = Counter + 1 PWord = PWord + Chr$(KeyAscii) KeyAscii = 0 Text1.Text = String$(Counter, "*") If LCase$(PWord) = "abcd" Then Text1.Text = "" PWord = 0 MsgBox "Correct password" Counter = 0 Print "Continue" ElseIf Counter = 4 Then Counter = 0 PWord = "" Text1.Text = "" MsgBox "Is the password correct?,Please input from the star" End If End Sub
KeyDown KeyUp event
KeyDown and KeyUp return keyboard status.
The control for which the parameters of the procedure are used, KeyCode As Integer Shift As Integer
KeyCode uppercase and lowercase letters are the same, because they all use a keyboard.
SHIFT
Conversion key, shift ctrl AL, he 001 010 100
We can build the following process
Private Sub Text1_KeyDown(KeyCode As Integer, Shift As Integer)
End Sub
KeyDown is an event that occurs when the keyboard is pressed, and KeyUp is an event that occurs when the keyboard is loosened.
Example
When pressed, the label displays the scan code, which is clear when loosened.
Private Sub Text1_KeyDown(KeyCode As Integer, Shift As Integer) Label1.Caption = Str$(KeyCode) End Sub Private Sub Text1_KeyUp(KeyCode As Integer, Shift As Integer) Label.Caption = "" End Sub
We can use it.
Const Shift = 1
Const Ctrl = 2
Const Alt = 4
shift And Shift > 0
Shift And Ctrl > 0
Shift And Alt > 0
Mouse events
Click and double-click
Press and release
Press down
MouseDown
Release
MouseUp
move
MouseMove
Parameters (Button As Integer,Shift As Integer,x As Single, y As Single)
Button Press the Mouse Key
SHift denotes shift Ctrl Alt status
Current position of X Y mouse cursor
Shape of mouse cursor
MousePointer
Setting mouse cursor shape
- Object. MousePoint = Set value
windows Rules
1. Represents functions available to users
Drag and drop
attribute
DragMode
DragIcon
Manual drag and drop
Automatic Drag and Drop