'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 Declare Function SetCapture Lib “user32” (ByVal hwnd As Long) As Long
Private Declare Function ReleaseCapture Lib “user32” () As Long
Private Declare Function PostMessage Lib “user32” Alias “PostMessageA” (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Private Const WM_MOUSEMOVE As Long = &H200 ’ 鼠标移动消息
Private Const WM_LBUTTONDOWN As Long = &H201 ’ 左键按下消息
Private Const WM_LBUTTONUP As Long = &H202 ’ 左键松开消息
Private Sub Timer1_Timer()
Dim POINT As POINTAPI
GetCursorPos POINT
Label1.Caption = “鼠标坐标:” & vbCrLf & vbCrLf & “鼠标横坐标为:” & POINT.X & vbCrLf & “鼠标纵坐标为:” & POINT.Y
End Sub
’ 窗体的键盘事件
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
Dim POINT As POINTAPI
GetCursorPos POINT
SetCursorPos POINT.X, POINT.Y
Select Case KeyCode
Case vbKeyS
'拟鼠标左键点击
mouse_event MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0
mouse_event MOUSEEVENTF_LEFTUP, 0, 0, 0, 0
Case vbKeyD
'拟鼠标右键点击
mouse_event MOUSEEVENTF_RIGHTDOWN, 0, 0, 0, 0
mouse_event MOUSEEVENTF_RIGHTUP, 0, 0, 0, 0
Case vbKeyLeft
'左移动鼠标
SetCursorPos POINT.X - 10, POINT.Y ' 向左移动鼠标到指定位置
Case vbKeyRight
'右移动鼠标
SetCursorPos POINT.X + 10, POINT.Y ' 向右移动鼠标到指定位置
Case vbKeyUp
'上移动鼠标
SetCursorPos POINT.X, POINT.Y - 10 ' 向上移动鼠标到指定位置
Case vbKeyDown
'下移动鼠标
SetCursorPos POINT.X, POINT.Y + 10 ' 向下移动鼠标到指定位置
End Select
End Sub