title: Keyboard Hooking, prevent keyboard input to application
body: <p>I am trying to learn how keyboard hooking works and am failing.  I have gathered the following code which does some of what I want it to.<br>
  What I'm trying to do is Intercept certain keyboard inputs and execute my code, and not pass those specific keyboard inputs back to the application.  My code is capable of intercepting keyboard inputs and executing my code, but the keyboard inputs are always passed onto the application after my code has executed.  Any suggestions?</p>
  
  <pre><code>Imports System
  Imports System.Runtime.CompilerServices
  Imports System.Runtime.InteropServices
  Imports System.Windows.Forms
  
  
  Friend Class KeyboardHooking
  ' Methods
  &lt;DllImport("user32.dll", CharSet:=CharSet.Auto, SetLastError:=True)&gt; _
  Private Shared Function CallNextHookEx(ByVal hhk As IntPtr, ByVal nCode As Integer, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As IntPtr
  End Function
  
  &lt;DllImport("kernel32.dll", CharSet:=CharSet.Auto, SetLastError:=True)&gt; _
  Private Shared Function GetModuleHandle(ByVal lpModuleName As String) As IntPtr
  End Function
  
  Private Shared Function HookCallback(ByVal nCode As Integer, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As Integer
      If ((nCode &gt;= 0) AndAlso (nCode = 0)) Then
          Dim keyData As Keys = DirectCast(CInt(wParam), Keys)
  
          If ((BindingFunctions.IsKeyDown(Keys.ControlKey) AndAlso BindingFunctions.IsKeyDown(Keys.ShiftKey)) AndAlso BindingFunctions.IsKeyDown(My.Settings.keyLinkQuickToDB)) Then
              linkQuickToDB()
  
          'Qty Changer
          ElseIf ((BindingFunctions.IsKeyDown(Keys.ControlKey) AndAlso BindingFunctions.IsKeyDown(Keys.ShiftKey)) AndAlso BindingFunctions.IsKeyDown(My.Settings.keyQtyChange)) Then
              qtyChanger()
          End If
      End If
  
      Return CInt(KeyboardHooking.CallNextHookEx(KeyboardHooking._hookID, nCode, wParam, lParam))
  
  End Function
  
  Public Shared Sub ReleaseHook()
      KeyboardHooking.UnhookWindowsHookEx(KeyboardHooking._hookID)
  End Sub
  
  Public Shared Sub SetHook()
      KeyboardHooking._hookID = KeyboardHooking.SetWindowsHookEx(2, KeyboardHooking._proc, IntPtr.Zero, Convert.ToUInt32(AppDomain.GetCurrentThreadId))
  End Sub
  
  &lt;DllImport("user32.dll", CharSet:=CharSet.Auto, SetLastError:=True)&gt; _
  Private Shared Function SetWindowsHookEx(ByVal idHook As Integer, ByVal lpfn As LowLevelKeyboardProc, ByVal hMod As IntPtr, ByVal dwThreadId As UInt32) As IntPtr
  End Function
  
  &lt;DllImport("user32.dll", CharSet:=CharSet.Auto, SetLastError:=True)&gt; _
  Private Shared Function UnhookWindowsHookEx(ByVal hhk As IntPtr) As &lt;MarshalAs(UnmanagedType.Bool)&gt; Boolean
  End Function
  
  
  ' Fields
  Private Shared _hookID As IntPtr = IntPtr.Zero
  Private Shared _proc As LowLevelKeyboardProc = New LowLevelKeyboardProc(AddressOf KeyboardHooking.HookCallback)
  Private Const WH_KEYBOARD As Integer = 2
  Private Const WH_KEYBOARD_LL As Integer = 13
  Private Const WM_KEYDOWN As Integer = &amp;H100
  
  ' Nested Types
  Public Delegate Function LowLevelKeyboardProc(ByVal nCode As Integer, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As Integer
  
  End Class
  
  Public Class BindingFunctions
  ' Methods
  &lt;DllImport("user32.dll")&gt; _
  Private Shared Function GetKeyState(ByVal nVirtKey As Integer) As Short
  End Function
  
  Public Shared Function IsKeyDown(ByVal keys As Keys) As Boolean
      Return ((BindingFunctions.GetKeyState(CInt(keys)) And &amp;H8000) = &amp;H8000)
  End Function
  
  End Class
  </code></pre>