注册 登录
编程论坛 VB6论坛

【求援助】按键盘,让鼠标单击,求思路

mmttvv11 发布于 2014-04-03 17:30, 303 次点击
大神帮帮忙!
比如按下Q键,让鼠标右键在300,200处,单击一下,什么思路啊
最好写个例子看看。。


[ 本帖最后由 mmttvv11 于 2014-4-3 17:32 编辑 ]
3 回复
#2
风吹过b2014-04-03 20:02
HOOK 键盘,定义热键。然后 使用 API 模拟 鼠标点击。

API 不熟,自己琢磨吧。
#3
lowxiong2014-04-03 20:15
新建一个工程,在窗口里放一个按钮控件,放一个标签控件(label),放一个时钟控件,拷贝下列代码,鼠标会每隔2秒钟自动点击按钮,标签里的数自动累加(手动点击按钮也会累加)
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 Sub Command1_Click()
  Label1.Caption = Val(Label1.Caption) + 1
End Sub

Private Sub Form_Load()
  Timer1.Interval = 2000
End Sub

Private Sub Timer1_Timer()
  Dim x As Long, y As Long
  x = (Me.Left + Command1.Left + 600) / 15
  y = (Me.Top + Command1.Top + 600) / 15
  SetCursorPos x, y
  mouse_event &H2, 0, 0, 0, 0
  mouse_event &H4, 0, 0, 0, 0
End Sub
#4
茅十八2014-04-03 21:29
程序代码:
'新建一个VB工程,把form1的KeyPreview属性设置为True。这样,当有按键操作时,首先执行form1的KeyPress事件

'需要用到2个API函数,SetCursorPos用来设置光标位置,mouse_event模拟鼠标按键
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_RIGHTDOWN As Long = &H8
Private Const MOUSEEVENTF_RIGHTUP As Long = &H10
'下面,模拟按键操作
Private Sub Form_KeyPress(KeyAscii As Integer)
If KeyAscii = Asc("Q") Or KeyAscii = Asc("q") Then  '如果按下Q建
SetCursorPos 300, 200 '鼠标移动到(300,200)处
mouse_event MOUSEEVENTF_RIGHTDOWN, 0, 0, 0, 0 '鼠标右键按下
mouse_event MOUSEEVENTF_RIGHTUP, 0, 0, 0, 0 '鼠标右键抬起
End If
End Sub
1