VB 怎样用键盘控制鼠标移动?
我是VB新手,想请教一下我下面的代码为什么按下字母a,鼠标指针就总是指在屏幕的左上角?我本来是想让鼠标向右移动的,该怎么做?PrivateDeclareFunctionS...
我是VB新手,想请教一下我下面的代码为什么按下字母a,鼠标指针就总是指在屏幕的左上角?我本来是想让鼠标向右移动的,该怎么做?
Private Declare Function SetCursorPos Lib "user32" (ByVal x As Long, ByVal y As Long) As Long
Private Sub Timer1_Timer()———Timer1的Enbled属性以被我设置为False
Interval = 500
Dim a As Integer
Dim b As Integer
a = 500
b = 500
a = a + 10
End Sub
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = vbKeyA Then
Timer1.Enabled = True
SetCursorPos a, b
End If
End Sub
Private Sub Form_KeyUp(KeyCode As Integer, Shift As Integer)
If KeyCode = vbKeyA Then
Timer1.Enabled = False
End If
End Sub 展开
Private Declare Function SetCursorPos Lib "user32" (ByVal x As Long, ByVal y As Long) As Long
Private Sub Timer1_Timer()———Timer1的Enbled属性以被我设置为False
Interval = 500
Dim a As Integer
Dim b As Integer
a = 500
b = 500
a = a + 10
End Sub
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = vbKeyA Then
Timer1.Enabled = True
SetCursorPos a, b
End If
End Sub
Private Sub Form_KeyUp(KeyCode As Integer, Shift As Integer)
If KeyCode = vbKeyA Then
Timer1.Enabled = False
End If
End Sub 展开
展开全部
建议使用mouse_event直接设置相对移动坐标
Declare Sub mouse_event Lib "user32" Alias "mouse_event" (ByVal dwFlags As Long, ByVal dx As Long, ByVal dy As Long, ByVal cButtons As Long, ByVal dwExtraInfo As Long)
const MOUSEEVENTF_MOVE = &h1'鼠标移动
const MOUSEEVENTF_ABSOLUTE = &h8000 '是否采用绝对坐标
需要说明的是,如果没有使用MOUSEEVENTF_ABSOLUTE,函数默认的是相对于鼠标当前位置的点进行移动。
例:
mouse_event MOUSEEVENTF_ABSOLUTE or MOUSEEVENTF_MOVE, 500, 500, 0, 0 '移动绝对坐标到屏幕位置500,500
mouse_event MOUSEEVENTF_MOVE, 130, -50, 0, 0 '鼠标当前位置开始向右移动130,向上移动50
mouse_event MOUSEEVENTF_MOVE, -130, 50, 0, 0 '鼠标当前位置开始向左移动130,向下移动50
Declare Sub mouse_event Lib "user32" Alias "mouse_event" (ByVal dwFlags As Long, ByVal dx As Long, ByVal dy As Long, ByVal cButtons As Long, ByVal dwExtraInfo As Long)
const MOUSEEVENTF_MOVE = &h1'鼠标移动
const MOUSEEVENTF_ABSOLUTE = &h8000 '是否采用绝对坐标
需要说明的是,如果没有使用MOUSEEVENTF_ABSOLUTE,函数默认的是相对于鼠标当前位置的点进行移动。
例:
mouse_event MOUSEEVENTF_ABSOLUTE or MOUSEEVENTF_MOVE, 500, 500, 0, 0 '移动绝对坐标到屏幕位置500,500
mouse_event MOUSEEVENTF_MOVE, 130, -50, 0, 0 '鼠标当前位置开始向右移动130,向上移动50
mouse_event MOUSEEVENTF_MOVE, -130, 50, 0, 0 '鼠标当前位置开始向左移动130,向下移动50
展开全部
是这样的,a,b是表示你的鼠标在屏目的位置a表示x轴的坐标,b是y轴的坐标,如果你要右移鼠标,那你就加大a的值就可以了,最后为什么按a键时才起作用,是因为代码用的是获取键盘代码,不知道你明白了没有
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
改好了,每500毫秒向右10
Private Declare Function SetCursorPos Lib "user32" (ByVal x As Long, ByVal y As Long) As Long
Dim a As Integer
Dim b As Integer
Private Sub Form_Load()
Timer1.Interval = 500
a = 500
b = 500
End Sub
Private Sub Timer1_Timer()
a = a + 10
End Sub
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = vbKeyA Then
Timer1.Enabled = True
SetCursorPos a, b
End If
End Sub
Private Sub Form_KeyUp(KeyCode As Integer, Shift As Integer)
If KeyCode = vbKeyA Then
Timer1.Enabled = False
End If
End Sub
Private Declare Function SetCursorPos Lib "user32" (ByVal x As Long, ByVal y As Long) As Long
Dim a As Integer
Dim b As Integer
Private Sub Form_Load()
Timer1.Interval = 500
a = 500
b = 500
End Sub
Private Sub Timer1_Timer()
a = a + 10
End Sub
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = vbKeyA Then
Timer1.Enabled = True
SetCursorPos a, b
End If
End Sub
Private Sub Form_KeyUp(KeyCode As Integer, Shift As Integer)
If KeyCode = vbKeyA Then
Timer1.Enabled = False
End If
End Sub
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
Option Explicit
Private Declare Function SetCursorPos Lib "user32" (ByVal x As Long, ByVal y As Long) As Long
Dim a As Integer, b As Integer
Private Sub Form_Load()
a = 500
b = 500
Timer1.Interval = 500
End Sub
Private Sub Timer1_Timer()
a = a + 10
SetCursorPos a, b
End Sub
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = vbKeyA Then
Timer1.Enabled = True
End If
End Sub
Private Sub Form_KeyUp(KeyCode As Integer, Shift As Integer)
If KeyCode = vbKeyA Then
Timer1.Enabled = False
End If
End Sub
Private Declare Function SetCursorPos Lib "user32" (ByVal x As Long, ByVal y As Long) As Long
Dim a As Integer, b As Integer
Private Sub Form_Load()
a = 500
b = 500
Timer1.Interval = 500
End Sub
Private Sub Timer1_Timer()
a = a + 10
SetCursorPos a, b
End Sub
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = vbKeyA Then
Timer1.Enabled = True
End If
End Sub
Private Sub Form_KeyUp(KeyCode As Integer, Shift As Integer)
If KeyCode = vbKeyA Then
Timer1.Enabled = False
End If
End Sub
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
找微软的API吧,我记得windows系统里有这个功能,用小键盘代替鼠标。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询