The following warnings occurred:
Warning [2] count(): Parameter must be an array or an object that implements Countable - Line: 895 - File: showthread.php PHP 7.2.30 (Linux)
File Line Function
/showthread.php 895 errorHandler->error

Thread Rating:
  • 4 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to hook the keyboard. [VB.NET] [TuT]
OK, this is how to make a low level keyboard hook in VB.NET (A keyboard hook, not a keylogger Nono Although it can be used for the latter.)

So make a form with a text box, then view the code.

Put this into your "Public Class Form1"

Public Class KeyboardHook
        Private Const HC_ACTION As Integer = 0
        Private Const WH_KEYBOARD_LL As Integer = 13
        Private Const WM_KEYDOWN = &H100
        Private Const WM_KEYUP = &H101
        Private Const WM_SYSKEYDOWN = &H104
        Private Const WM_SYSKEYUP = &H105

        Private Structure KBDLLHOOKSTRUCT
            Public vkCode As Integer
            Public scancode As Integer
            Public flags As Integer
            Public time As Integer
            Public dwExtraInfo As Integer
        End Structure

        Private Declare Function SetWindowsHookEx Lib "user32" _
        Alias "SetWindowsHookExA" _
        (ByVal idHook As Integer, _
        ByVal lpfn As KeyboardProcDelegate, _
        ByVal hmod As Integer, _
        ByVal dwThreadId As Integer) As Integer

        Private Declare Function CallNextHookEx Lib "user32" _
        (ByVal hHook As Integer, _
        ByVal nCode As Integer, _
        ByVal wParam As Integer, _
        ByVal lParam As KBDLLHOOKSTRUCT) As Integer

        Private Declare Function UnhookWindowsHookEx Lib "user32" _
        (ByVal hHook As Integer) As Integer

        Private Delegate Function KeyboardProcDelegate _
        (ByVal nCode As Integer, _
        ByVal wParam As Integer, _
        ByRef lParam As KBDLLHOOKSTRUCT) 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 KeyboardProcDelegate

        Public Sub New() ' Installs The Hook

            KeyHookDelegate = New KeyboardProcDelegate(AddressOf KeyboardProc)
            KeyHook = SetWindowsHookEx(WH_KEYBOARD_LL, KeyHookDelegate, System.Runtime.InteropServices.Marshal.GetHINSTANCE(System.Reflection.Assembly.GetExecutingAssembly.GetModules()(0)).ToInt32, 0)
        End Sub

        Private Shared Function KeyboardProc(ByVal nCode As Integer, ByVal wParam As Integer, ByRef lParam As KBDLLHOOKSTRUCT) As Integer

            If (nCode = HC_ACTION) Then
                Select Case wParam

                    Case WM_KEYDOWN, WM_SYSKEYDOWN

                        RaiseEvent KeyDown(CType(lParam.vkCode, Keys))
                    Case WM_KEYUP, WM_SYSKEYUP

                        RaiseEvent KeyUp(CType(lParam.vkCode, Keys))
                End Select
            End If

            Return CallNextHookEx(KeyHook, nCode, wParam, lParam)
        End Function
        Protected Overrides Sub Finalize()

            UnhookWindowsHookEx(KeyHook)   'Un-Hooks When Program Closes
        End Sub
    End Class

Now under that we create the hook.

Private WithEvents kbHook As New KeyboardHook

Then we handle the events.

Private Sub kbHook_KeyDown(ByVal Key As System.Windows.Forms.Keys) Handles kbHook.KeyDown
If Key = Keys.A Then
            TextBox1.Text = TextBox1.Text & "a"

Then Finish the code.

End Sub
End Class

[Image: hook.png]


***Originality written by Vorfin(Me) For LeetCoders***
[Image: Lj]

Messages In This Thread
How to hook the keyboard. [VB.NET] [TuT] - by Vorfin - 10-09-2009, 12:52 PM

Possibly Related Threads...
Thread Author Replies Views Last Post
  VB.NET Port Scanner [TUT] Fragma 30 8,998 11-27-2012, 11:26 PM
Last Post: ƃu∀ ıʞƃu∀
  [TUT] MD5 Encrypter & Finder [VB.NET] Fragma 12 4,574 11-25-2012, 10:36 PM
Last Post: ƃu∀ ıʞƃu∀
  [TUT] How to make a simple WebBrowser in VB 2010! [TUT] - [ Pictures] Statics 95 36,236 10-07-2012, 06:56 AM
Last Post: a99
  [TUT]Enable and Disable TaskManger in [TUT] HB Virus 4 1,580 12-19-2011, 10:10 AM
Last Post: euverve
  [Vb.Net]Save user settings with "My.Settings"[TUT] Digital-Punk 7 4,139 11-21-2011, 09:29 AM
Last Post: UrbanByte

Forum Jump:

Users browsing this thread: 1 Guest(s)