VBA控制鼠标键盘

发布时间 2023-07-01 13:18:50作者: CrossPython

 

'sleep函数
Private Declare Sub Sleep Lib “kernel32” (ByVal dwMilliseconds As Long)

'获取鼠标坐标
Private Type POINTAPI
    X As Long
    Y As Long
End Type

Private Declare Function GetCursorPos Lib “User32” (lpPoint As POINTAPI) As Long

'移动鼠标
Private Declare Function SetCursorPos Lib “User32” (ByVal X As Long, ByVal Y As Long) As Long
Private Declare Sub mouse_event Lib “User32” (ByVal dwFlags As Long, ByVal dx As Long, ByVal dy As Long, ByVal cButtons As Long, ByVal dwExtraInfo As Long)
Private Const MOUSEEVENTF_LEFTDOWN = &H2
Private Const MOUSEEVENTF_LEFTUP = &H4
Private Const MOUSEEVENTF_RIGHTDOWN = &H8
Private Const MOUSEEVENTF_RIGHTUP = &H10

Private Sub Command1_Click()
Text2.Text = Text1.Text
End Sub

Private Sub Form_KeyPress(KeyAscii As Integer) '在form上敲空格触发事件

If KeyAscii = 32 Then '如果按下的是空格键,注意空格Asc码是32
Call Command1_Click '那么执行command1点击事件
End If
End Sub

Private Sub Timer1_Timer()
Dim Point As POINTAPI
GetCursorPos Point
Text1.Text = “鼠标横坐标为:” & Point.X & vbCrLf & vbCrLf & “鼠标纵坐标为:” & Point.Y

End Sub

正确例题
'sleep函数
Private Declare Sub Sleep Lib “kernel32” (ByVal dwMilliseconds As Long)

'获取鼠标坐标
Private Type POINTAPI
X As Long
Y As Long

End Type
Private Declare Function GetCursorPos Lib “User32” (lpPoint As POINTAPI) As Long

'移动鼠标
Private Declare Function SetCursorPos Lib “User32” (ByVal X As Long, ByVal Y As Long) As Long
Private Declare Sub mouse_event Lib “User32” (ByVal dwFlags As Long, ByVal dx As Long, ByVal dy As Long, ByVal cButtons As Long, ByVal dwExtraInfo As Long)
Private Const MOUSEEVENTF_LEFTDOWN = &H2
Private Const MOUSEEVENTF_LEFTUP = &H4
Private Const MOUSEEVENTF_RIGHTDOWN = &H8
Private Const MOUSEEVENTF_RIGHTUP = &H10

Private Sub Command1_Click()
Text2.Text = Text1.Text
End Sub

Private Sub Form_Load()
Call Command1_Click
End Sub

Private Sub Timer1_Timer()
Dim Point As POINTAPI
GetCursorPos Point
Text1.Text = “鼠标横坐标为:” & Point.X & vbCrLf & vbCrLf & “鼠标纵坐标为:” & Point.Y

End Sub

全正确例题

'sleep函数
Private Declare Sub Sleep Lib “kernel32” (ByVal dwMilliseconds As Long)

'获取鼠标坐标
Private Type POINTAPI
X As Long
Y As Long

End Type
Private Declare Function GetCursorPos Lib “User32” (lpPoint As POINTAPI) As Long

'移动鼠标
Private Declare Function SetCursorPos Lib “User32” (ByVal X As Long, ByVal Y As Long) As Long
Private Declare Sub mouse_event Lib “User32” (ByVal dwFlags As Long, ByVal dx As Long, ByVal dy As Long, ByVal cButtons As Long, ByVal dwExtraInfo As Long)
Private Const MOUSEEVENTF_LEFTDOWN = &H2
Private Const MOUSEEVENTF_LEFTUP = &H4
Private Const MOUSEEVENTF_RIGHTDOWN = &H8
Private Const MOUSEEVENTF_RIGHTUP = &H10

Private Sub Command2_Click()
Clipboard.Clear
Clipboard.SetText Text1.Text
End Sub

Private Sub Form_Load()
Call Command1_Click
End Sub

Private Sub Timer1_Timer()
Dim Point As POINTAPI
GetCursorPos Point
Text1.Text = “鼠标横坐标为:” & Point.X & vbCrLf & “鼠标纵坐标为:” & Point.Y

End Sub

Private Sub Command1_Click()

Text2.Text = Text1.Text

End Sub

Private Sub Command4_Click()
Clipboard.Clear
Clipboard.SetText Text2.Text

End Sub

Private Sub Command5_Click()
Text2.SelText = Clipboard.GetText
End Sub

Private Sub Command3_Click()
Static n As Integer
n = n + 1
If n > 5 Then n = 1
Select Case n
Case 1
Print Text1.Text & vbCrLf
Case 2
Print Text1.Text & vbCrLf
Case 3
Print Text1.Text & vbCrLf
Case 4
Print Text1.Text & vbCrLf
Case 5
Print Text1.Text & vbCrLf
End Select
End Sub