Feedback

VB - Globaler Keyhook

Veröffentlicht von am 3/21/2008
(3 Bewertungen)
Hiermit ist es möglich alle Tastenanschläge, systemweit abzufangen.
Um den Keyhook zu aktivieren, muss man nur
KeyHookEnable = true setzen.

In der Funktion KeyboardHookProc, kann man dann nach der Abfrage des nCode-Wertes (muss =0 sein, sonst ungültig) seine Operationen mit den Variablen machen.
    Private Declare Unicode Function GetModuleHandleW Lib "kernel32.dll" (ByVal lpModuleName As IntPtr) As IntPtr

    Private Delegate Function HOOKPROCDelegate( _
        ByVal nCode As Integer, ByVal wParam As IntPtr, ByRef lParam As KBDLLHOOKSTRUCT) As IntPtr

    Private Declare Unicode Function SetWindowsHookExW Lib "user32.dll" ( _
        ByVal idHook As Integer, ByVal lpfn As HOOKPROCDelegate, ByVal hMod As IntPtr, ByVal dwThreadId As UInteger) As IntPtr

    Private HookProc As New HOOKPROCDelegate(AddressOf KeyboardHookProc) 'dauerhafte Delegaten-Variable erzeugen

    Private Declare Unicode Function UnhookWindowsHookEx Lib "user32.dll" (ByVal hhk As IntPtr) As UInteger

    Private Declare Unicode Function CallNextHookEx Lib "user32.dll" ( _
        ByVal hhk As IntPtr, ByVal nCode As Integer, ByVal wParam As IntPtr, ByRef lParam As KBDLLHOOKSTRUCT) As IntPtr

    Private Const WH_KEYBOARD_LL As Integer = 13
    Private Const HC_ACTION As Integer = 0

    Private mHandle As IntPtr
    Public PrevWndProc As Integer

    <StructLayout(LayoutKind.Sequential)> Public Structure KBDLLHOOKSTRUCT
        Public vkCode As Keys
        Public scanCode, flags, time, dwExtraInfo As UInteger

        Public Sub New(ByVal key As Keys, ByVal scancod As UInteger, ByVal flagss As UInteger, ByVal zeit As UInteger, ByVal extra As UInteger)
            vkCode = Keys.A
            scanCode = scancod
            flags = flagss
            time = zeit
            dwExtraInfo = extra
        End Sub
    End Structure

    Public Property KeyHookEnable() As Boolean
        Get
            Return mHandle <> IntPtr.Zero
        End Get
        Set(ByVal value As Boolean)
            If KeyHookEnable = value Then Return
            If value Then
                mHandle = SetWindowsHookExW(WH_KEYBOARD_LL, HookProc, GetModuleHandleW(IntPtr.Zero), 0)
            Else
                UnhookWindowsHookEx(mHandle)
                mHandle = IntPtr.Zero
            End If
        End Set
    End Property

    Private KeyCounter As Integer

    Private Function KeyboardHookProc(ByVal nCode As Integer, ByVal wParam As IntPtr, ByRef lParam As KBDLLHOOKSTRUCT) As IntPtr
        Dim fEatKeyStroke As Boolean
        
        'Console.Write(lParam.vkCode.ToString & ", " & "nCode: " & CStr(nCode) & ", wParam: " & CStr(wParam) & ", lParam.vkCode: " & CStr(lParam.vkCode) & ", lParam.dwExtraInfo: " & CStr(lParam.dwExtraInfo) & ", lParam.flags: " & CStr(lParam.flags) & ", lParam.scanCode: " & CStr(lParam.scanCode) & vbCrLf)

        If nCode <> HC_ACTION Then GoTo Ende
        
'wParam kann folgende Werte annehmen WM_KEYUP und WM_KEYDOWN (Taste gedrückt/losgelassen)
'wird fEatKeyStroke=true gesetzt, so wird dieser Tastendruck "verschluckt", er hat für das System  NIE statt gefunden.

ende:
        If fEatKeyStroke Then
            Return New IntPtr(1)
            Exit Function
        End If

        Return CallNextHookEx(mHandle, nCode, wParam, lParam)
    End Function

Kommentare zum Snippet

 

Logge dich ein, um hier zu kommentieren!