vb.net 全局键盘钩子

Posted VB.Net

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了vb.net 全局键盘钩子相关的知识,希望对你有一定的参考价值。

应网友邀请写的代码。

模块部分的代码:

Imports System.Runtime.InteropServices


Module Keyboard
    <DllImport("User32.dll", CharSet:=CharSet.Auto, CallingConvention:=CallingConvention.StdCall)>
    Public Function SetWindowsHookEx(ByVal idHook As Integer, ByVal HookProc As KeyHook, ByVal hInstance As IntPtr, ByVal wParam As Integer) As Integer
    End Function
    <DllImport("User32.dll", CharSet:=CharSet.Auto, CallingConvention:=CallingConvention.StdCall)>
    Public Function CallNextHookEx(ByVal idHook As Integer, ByVal nCode As Integer, ByVal wParam As Integer, ByVal lParam As IntPtr) As Integer
    End Function
    <DllImport("User32.dll", CharSet:=CharSet.Auto, CallingConvention:=CallingConvention.StdCall)>
    Public Function UnhookWindowsHookEx(ByVal idHook As Integer) As Boolean
    End Function
    <DllImport("kernel32.dll", CharSet:=CharSet.Auto, CallingConvention:=CallingConvention.StdCall)>
    Public Function GetModuleHandle(ByVal name As String) As IntPtr
    End Function

    <StructLayout(LayoutKind.Sequential)>
    Public Structure KBDLLHOOKSTRUCT
        Public vkCode As Keys
        Public scanCode As Keys
        Public flags As Integer
        Public time As Integer
        Public dwExtraInfo As Integer
    End Structure
    Public Const HC_ACTION As Integer = 0

    Public Const WH_KEYBOARD_LL As Integer = 13

    Public Delegate Function KeyHook(ByVal Code As Integer, ByVal wParam As Integer, ByVal lParam As IntPtr) As Integer

    '<MarshalAs(UnmanagedType.FunctionPtr)>
    Public callback As KeyHook
End Module

窗体的代码:

   Public KeyHandle As Integer
    Public Function KeyCallback(ByVal Code As Integer, ByVal wParam As Integer, ByVal lParam As IntPtr) As Integer
        If Code >= HC_ACTION Then
            Dim keyStruct As KBDLLHOOKSTRUCT
            keyStruct = CType(Marshal.PtrToStructure(lParam, GetType(KBDLLHOOKSTRUCT)), KBDLLHOOKSTRUCT)
            TextBox1.Text &= keyStruct.vkCode.ToString & ControlChars.CrLf
            '这里是检测并屏蔽Win按键
            If keyStruct.vkCode = Keys.LWin Or keyStruct.vkCode = Keys.RWin Then
                Return 1
            End If
        End If
        Return CallNextHookEx(KeyHandle, Code, wParam, lParam)
    End Function



    Public Sub HookKeyboard()

        callback = New KeyHook(AddressOf KeyCallback)

        Dim hins As IntPtr = IntPtr.Zero
        hins = GetModuleHandle(Process.GetCurrentProcess.MainModule.ModuleName)
        KeyHandle = SetWindowsHookEx(WH_KEYBOARD_LL, callback, hins, 0)
        If (KeyHandle > 0) Then
            TextBox1.Text &= "启动钩子" & ControlChars.CrLf
        Else
            TextBox1.Text &= "启动钩子失败:" & Err.LastDllError & ControlChars.CrLf
        End If
    End Sub


    Public Sub UnhookKeyboard()
        Call UnhookWindowsHookEx(KeyHandle)
        TextBox1.Text &= "停止钩子" & ControlChars.CrLf
    End Sub
    '启动钩子
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        HookKeyboard()
    End Sub
    '停止钩子
    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        UnhookKeyboard()
    End Sub

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Me.TopMost = True
    End Sub

由于.net平台下C#和vb.NET很相似,本文也可以为C#爱好者提供的参考。

学习更多vb.net知识,请参看 vb.net 教程 目录

以上是关于vb.net 全局键盘钩子的主要内容,如果未能解决你的问题,请参考以下文章

delphi 键盘全局钩子

C++怎样简单实现全局钩子或者键盘监控

使用 vb.net,挂钩指定的 USB 键盘,接收所需控制的字符并将接收到的字符阻止到其余窗口

全局键盘钩子

想用VC++做个全局键盘改键,键盘钩子代码,详细怎么做。跪求代码。

csharp 全局键盘鼠标钩子