Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = 112 Then '112是F1的键值
     Command1_Click
End If
End Sub
这是我的代码,就是按下F1 Command1的单击一次,但我把焦点移到别的程序,按下这个热键就没效了举个例子,我在我编写的外挂上的某个按钮上设了热键F1,焦点在外挂上按F1就有效,但我切进游戏按F1就没效了,怎么实现在游戏按F1也有效,麻烦高手们帮帮忙

解决方案 »

  1.   

    需要注册全局热键。更多源码,尽在天狼工作室
    天狼工作室 http://www.j2soft.cn/代码如下:
    注册热键为:Alt+A,自己修改自己想用的热键模块:Option ExplicitDeclare Function SetWindowLong Lib "User32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
    Declare Function GetWindowLong Lib "User32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long
    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
    Declare Function RegisterHotKey Lib "User32" (ByVal hwnd As Long, ByVal id As Long, ByVal fsModifiers As Long, ByVal vk As Long) As Long
    Declare Function UnregisterHotKey Lib "User32" (ByVal hwnd As Long, ByVal id As Long) As LongPublic Const WM_HOTKEY = &H312
    Public Const MOD_ALT = &H1
    Public Const MOD_CONTROL = &H2
    Public Const MOD_SHIFT = &H4
    Public Const GWL_WNDPROC = (-4)Public preWinProc As Long
    Public Modifiers As Long, uVirtKey As Long, idHotKey As LongPrivate Type taLong
        ll As Long
    End TypePrivate Type t2Int
        lWord As Integer
        hWord As Integer
    End TypePublic Function Wndproc(ByVal hwnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
        If Msg = WM_HOTKEY Then
            If wParam = idHotKey Then
                Dim lp As taLong, i2 As t2Int
                lp.ll = lParam
                LSet i2 = lp
                If (i2.lWord = Modifiers) And i2.hWord = uVirtKey Then
                    MsgBox "你按下了热键哦~"
                End If
            End If
        End If
        Wndproc = CallWindowProc(preWinProc, hwnd, Msg, wParam, lParam)
    End Function窗体:
    [code=VB]
    Option ExplicitPrivate Sub Form_Load()
        Dim ret As Long
        preWinProc = GetWindowLong(Me.hwnd, GWL_WNDPROC)
        ret = SetWindowLong(Me.hwnd, GWL_WNDPROC, AddressOf Wndproc)
        idHotKey = 1
        Modifiers = MOD_ALT '+ MOD_CONTROL
        uVirtKey = vbKeyA
        ret = RegisterHotKey(Me.hwnd, idHotKey, Modifiers, uVirtKey)
    End SubPrivate Sub Form_Unload(Cancel As Integer)
        Dim ret As Long
        ret = SetWindowLong(Me.hwnd, GWL_WNDPROC, preWinProc)
        Call UnregisterHotKey(Me.hwnd, uVirtKey)
    End Sub
    [/code]
      

  2.   

    哪位加下我好友,或邮箱我[email protected]我在
    If (i2.lWord = Modifiers) And i2.hWord = uVirtKey Then
       MsgBox "你按下了热键哦~"
    End If修改成If (i2.lWord = Modifiers) And i2.hWord = uVirtKey Then
       form1.command1_click
    End IfVB就自动关闭了 - -!
      

  3.   

    可以试试:form1.command.value=true(原因不懂,貌似不能调用事件,但属性可以)