VB多个全局热键问题
我想设计个VB程序,按F1就点鼠标左键,按F2就点鼠标右键,按方向键移动鼠标,请给具体代码。Thank!...
我想设计个VB程序,按F1就点鼠标左键,按F2就点鼠标右键,按方向键移动鼠标,请给具体代码。Thank!
展开
1个回答
展开全部
需要一个窗体和一个模块,其中:
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
'------------------------------------------------------
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
'------------------------------------------------------
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询