如何使用低级键盘挂钩检测Ctrl + Alt + H?

时间:2016-03-14 19:07:10

标签: vb.net

我正在编写一个隐藏/显示我的应用程序的功能,我想使用Ctrl + Alt + H的快捷键组合来隐藏和显示表单。

到目前为止,这是我的代码:

Imports System.Runtime.InteropServices
''' <summary>
''' Global Keyboard Hook
''' </summary>
''' <remarks>This is the keyboard hook class which will carry out the process to identify which keystroke is pressed. </remarks>
Public Class GlobalKeyboardHook
    Private Const HC_ACTION As Integer = 0
    Private Const WH_KEYBOARD_LL As Integer = 13
    Private Const WM_KEYDOWN As Integer = &H100
    Private Const WM_KEYUP As Integer = &H101
    Private Const WM_SYSKEYDOWN As Integer = &H104
    Private Const WM_SYSKEYUP As Integer = &H105
    Private Structure Hook_Structure
        Public VK_Code As Integer
        Public Scan_Code As Integer
        Public _Flags As Integer
        Public _Time As Integer
        Public DW_Extra_Info As Integer
    End Structure
    Private Declare Function SetWindowsHookEx Lib "user32" Alias "SetWindowsHookExA" (ByVal ID_Hook As Integer, ByVal LP_FN As Key_ProcessDelegate, ByVal H_Mod As Integer, ByVal DW_Thread_ID As Integer) As Integer
    Private Declare Function CallNextHookEx Lib "user32" (ByVal H_Hook As Integer, ByVal N_Code As Integer, ByVal W_Param As Integer, ByVal L_Param As Hook_Structure) As Integer
    Private Declare Function UnhookWindowsHookEx Lib "user32" (ByVal H_Hook As Integer) As Integer
    Private Delegate Function Key_ProcessDelegate(ByVal N_Code As Integer, ByVal W_Param As Integer, ByRef L_Param As Hook_Structure) As Integer
    Public Shared Event KeyDown(ByVal Key As Keys)
    Public Shared Event KeyUp(ByVal Key As Keys)
    Private Shared KeyHook As Integer
    Private Shared KeyHookDelegate As Key_ProcessDelegate
    Public Sub Hook()
        KeyHookDelegate = New Key_ProcessDelegate(AddressOf Key_Process)
        KeyHook = SetWindowsHookEx(WH_KEYBOARD_LL, KeyHookDelegate, System.Runtime.InteropServices.Marshal.GetHINSTANCE(System.Reflection.Assembly.GetExecutingAssembly.GetModules()(0)).ToInt32, 0)
    End Sub
    Private Function Key_Process(ByVal N_Code As Integer, ByVal W_Param As Integer, ByRef L_Param As Hook_Structure) As Integer
        If (N_Code = HC_ACTION) Then
            Select Case W_Param
                Case WM_KEYDOWN, WM_SYSKEYDOWN
                    RaiseEvent KeyDown(CType(L_Param.VK_Code, Keys))
                Case WM_KEYUP, WM_SYSKEYUP
                    RaiseEvent KeyUp(CType(L_Param.VK_Code, Keys))
            End Select
        End If
        Return CallNextHookEx(KeyHook, N_Code, W_Param, L_Param)
    End Function
    Public Sub Unhook()
        UnhookWindowsHookEx(KeyHook)
        MyBase.Finalize()
    End Sub
    Public Function Feed(ByVal e As Keys) As String
        Return e.ToString
    End Function
End Class

在Form1类中:

Private WithEvents KeyHook As New GlobalKeyboardHook
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            KeyHook.Hook()
    End Sub
    Private Sub KeyPress_Event(ByVal e As Keys) Handles KeyHook.KeyDown
            If Control.ModifierKeys = Keys.Alt Then 'Or Control.ModifierKeys = Keys.Ctrl
                If KeyHook.Feed(e) = "H" Then
                    'Show Form/Hide Form
                End If
            End If
End Sub

如果我使用其中一个修改键Alt,Ctrl,Shift等,这段代码对我来说很好。但是因为我想使用两个修改键,即ALT和Ctrl然后是H键,我正在尝试使用逻辑:

Private Sub KeyPress_Event(ByVal e As Keys) Handles KeyHook.KeyDown
            If Control.ModifierKeys = Keys.Alt AndAlso Control.ModifierKeys = Keys.Ctrl Then
                If KeyHook.Feed(e) = "H" Then
                    'Show Form/Hide Form
                End If
            End If
End Sub

但我明白这不是正确的做法。这里的逻辑是什么?

1 个答案:

答案 0 :(得分:-1)

我刚使用了My.Computer类。

Private Sub KeyRelease_Event(ByVal e As Keys) Handles KeyHook.KeyDown
        If My.Computer.Keyboard.AltKeyDown AndAlso My.Computer.Keyboard.CtrlKeyDown Then
            If KeyHook.Feed(e) <> Nothing Then
                'Show Form/Hide Form
            End If
        End If
    End Sub