VB多个全局热键问题

我想设计个VB程序,按F1就点鼠标左键,按F2就点鼠标右键,按方向键移动鼠标,请给具体代码。Thank!... 我想设计个VB程序,按F1就点鼠标左键,按F2就点鼠标右键,按方向键移动鼠标,请给具体代码。Thank! 展开
 我来答
二白Q0
2010-01-25 · TA获得超过2413个赞
知道大有可为答主
回答量:990
采纳率:0%
帮助的人:1091万
展开全部
需要一个窗体和一个模块,其中:
VB窗体代码:
'------------------------------------------------------
Private Declare Function GlobalAddAtom Lib "kernel32" Alias "GlobalAddAtomA" (ByVal lpString As String) As Integer
Private Declare Function GlobalDeleteAtom Lib "kernel32" (ByVal nAtom As Integer) As Integer
Private Declare Function RegisterHotKey Lib "user32" (ByVal hWnd As Long, ByVal id As Long, ByVal fsModifiers As Long, ByVal vk As Long) As Long
Private Declare Function UnregisterHotKey Lib "user32" (ByVal hWnd As Long, ByVal id As Long) As Long
Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Private Const GWL_WNDPROC = (-4)

Dim nAtom(1 To 6) As Long
Dim lHotkey(1 To 6) As Long
Dim i As Integer

Private Sub Form_Load()
lHotkey(1) = vbKeyF1
lHotkey(2) = vbKeyF2
lHotkey(3) = vbKeyLeft
lHotkey(4) = vbKeyRight
lHotkey(5) = vbKeyUp
lHotkey(6) = vbKeyDown

For i = 1 To 6
nAtom(i) = GlobalAddAtom("myProgram" & i)
RegisterHotKey Me.hWnd, nAtom(i), ByVal 0&, lHotkey(i)
Next

lOldProc = SetWindowLong(Me.hWnd, GWL_WNDPROC, AddressOf WndProc)
End Sub

Private Sub Form_Unload(Cancel As Integer)
For i = 1 To 6
UnregisterHotKey Me.hWnd, nAtom(i)
GlobalDeleteAtom nAtom(i)
Next
SetWindowLong Me.hWnd, GWL_WNDPROC, lOldProc
End Sub
'------------------------------------------------------

模块代码:
'------------------------------------------------------
Private Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Long, ByVal hWnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Private Const WM_HOTKEY = &H312
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 ' left button down
Private Const MOUSEEVENTF_LEFTUP = &H4 ' left button up
Private Const MOUSEEVENTF_RIGHTDOWN = &H8 ' right button down
Private Const MOUSEEVENTF_RIGHTUP = &H10 ' right button up
Private Const MOUSEEVENTF_MOVE = &H1 ' mouse move

Public lOldProc As Long

Public Function WndProc(ByVal hWnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Select Case Msg
Case WM_HOTKEY
Select Case (lParam And &HFFFF0000) / &H10000
Case vbKeyF1
mouse_event MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0
mouse_event MOUSEEVENTF_LEFTUP, 0, 0, 0, 0
Case vbKeyF2
mouse_event MOUSEEVENTF_RIGHTDOWN, 0, 0, 0, 0
mouse_event MOUSEEVENTF_RIGHTUP, 0, 0, 0, 0
Case vbKeyLeft
mouse_event MOUSEEVENTF_MOVE, -1, 0, 0, 0
Case vbKeyRight
mouse_event MOUSEEVENTF_MOVE, 1, 0, 0, 0
Case vbKeyUp
mouse_event MOUSEEVENTF_MOVE, 0, -1, 0, 0
Case vbKeyDown
mouse_event MOUSEEVENTF_MOVE, 0, 1, 0, 0
End Select
End Select
WndProc = CallWindowProc(lOldProc, hWnd, Msg, wParam, lParam)
End Function
'------------------------------------------------------
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式