请问如何注册热键?例如按下ctrl+art+F5 就呼出窗口,或者隐藏窗口,或者执行一段程序。
还有怎样检测要注册的热键是否已被占用?谢谢各位。

解决方案 »

  1.   

    以下代码放在bas模块中:
    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 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 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 Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long
    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 LongConst GWL_WNDPROC = (-4)
    Const WM_HOTKEY = &H312Public Enum ModKeys
      MOD_ALT = &H1
      MOD_CONTROL = &H2
      MOD_SHIFT = &H4
      MOD_WIN = &H8
    End EnumDim iAtom As Integer
    Dim OldProc As Long, hOwner As LongPublic Function SetHotKey(hWin As Long, ModKey As ModKeys, vKey As Long) As Boolean
      If hOwner > 0 Then Exit Function
      hOwner = hWin
      iAtom = GlobalAddAtom("MyHotKey")
      SetHotKey = RegisterHotKey(hOwner, iAtom, ModKey, vKey)
      OldProc = SetWindowLong(hOwner, GWL_WNDPROC, AddressOf WndProc)
    End FunctionPublic Sub RemoveHotKey()
      If hOwner = 0 Then Exit Sub
      Call UnregisterHotKey(hOwner, iAtom)
      Call SetWindowLong(hOwner, GWL_WNDPROC, OldProc)
    End SubPublic Function WndProc(ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
      If wMsg = WM_HOTKEY And wParam = iAtom Then
         '你要执行的代码
      Else
         WndProc = CallWindowProc(OldProc, hwnd, wMsg, wParam, lParam)
      End If
    End Function
      

  2.   

    以下代码放在窗体中,
    设置热键:SetHotKey Me.hwnd, MOD_CONTROL + MOD_ALT, vbKeyF5
    取消热键:RemoveHotKey
      

  3.   

    Private Const MOD_ALT = &H1
    Private Const MOD_CONTROL = &H2
    Private Const MOD_SHIFT = &H4
    Private Const PM_REMOVE = &H1
    Private Const WM_HOTKEY = &H312
    Private Type POINTAPI
        x As Long
        y As Long
    End Type
    Private Type Msg
        hWnd As Long
        Message As Long
        wParam As Long
        lParam As Long
        time As Long
        pt As POINTAPI
    End Type
    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 PeekMessage Lib "user32" Alias "PeekMessageA" (lpMsg As Msg, ByVal hWnd As Long, ByVal wMsgFilterMin As Long, ByVal wMsgFilterMax As Long, ByVal wRemoveMsg As Long) As Long
    Private Declare Function WaitMessage Lib "user32" () As Long
    Private bCancel As Boolean
    Private Sub ProcessMessages()
        Dim Message As Msg
        'loop until bCancel is set to True
        Do While Not bCancel
            'wait for a message
            WaitMessage
            'check if it's a HOTKEY-message
            If PeekMessage(Message, Me.hWnd, WM_HOTKEY, WM_HOTKEY, PM_REMOVE) Then
                'minimize the form
                WindowState = vbMinimized
            End If
            'let the operating system process other events
            DoEvents
        Loop
    End Sub
    Private Sub Form_Load()
        Dim ret As Long
        bCancel = False
        'register the Ctrl-F hotkey
        ret = RegisterHotKey(Me.hWnd, &HBFFF&, MOD_CONTROL, vbKeyF)
        'show some information
        Me.AutoRedraw = True
        Me.Print "Press CTRL-F to minimize this form"
        'show the form and
        Show
        'process the Hotkey messages
        ProcessMessages
    End Sub
    Private Sub Form_Unload(Cancel As Integer)
        bCancel = True
        'unregister hotkey
        Call UnregisterHotKey(Me.hWnd, &HBFFF&)
    End Sub