title: Keyboard Hook freezes keyboard while app performs other task
body: <p>I am using the following class found <a href="http://sim0n.wordpress.com/2009/03/28/vbnet-keyboard-hook-class/" rel="nofollow">here</a> to hook a keyboard combination globally in an app of mine.</p>
  
  <pre><code>Imports System.Runtime.InteropServices
  
  Public Class KeyboardHook
  
  &lt;DllImport("User32.dll", CharSet:=CharSet.Auto, CallingConvention:=CallingConvention.StdCall)&gt; _
  Private Overloads Shared Function SetWindowsHookEx(ByVal idHook As Integer, ByVal HookProc As KBDLLHookProc, ByVal hInstance As IntPtr, ByVal wParam As Integer) As Integer
  End Function
  &lt;DllImport("User32.dll", CharSet:=CharSet.Auto, CallingConvention:=CallingConvention.StdCall)&gt; _
  Private Overloads Shared Function CallNextHookEx(ByVal idHook As Integer, ByVal nCode As Integer, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As Integer
  End Function
  &lt;DllImport("User32.dll", CharSet:=CharSet.Auto, CallingConvention:=CallingConvention.StdCall)&gt; _
  Private Overloads Shared Function UnhookWindowsHookEx(ByVal idHook As Integer) As Boolean
  End Function
  
  &lt;StructLayout(LayoutKind.Sequential)&gt; _
  Private Structure KBDLLHOOKSTRUCT
      Public vkCode As UInt32
      Public scanCode As UInt32
      Public flags As KBDLLHOOKSTRUCTFlags
      Public time As UInt32
      Public dwExtraInfo As UIntPtr
  End Structure
  
  &lt;Flags()&gt; _
  Private Enum KBDLLHOOKSTRUCTFlags As UInt32
      LLKHF_EXTENDED = &amp;H1
      LLKHF_INJECTED = &amp;H10
      LLKHF_ALTDOWN = &amp;H20
      LLKHF_UP = &amp;H80
  End Enum
  
  Public Shared Event KeyDown(ByVal Key As Keys)
  Public Shared Event KeyUp(ByVal Key As Keys)
  
  Private Const WH_KEYBOARD_LL As Integer = 13
  Private Const HC_ACTION As Integer = 0
  Private Const WM_KEYDOWN = &amp;H100
  Private Const WM_KEYUP = &amp;H101
  Private Const WM_SYSKEYDOWN = &amp;H104
  Private Const WM_SYSKEYUP = &amp;H105
  
  Private Delegate Function KBDLLHookProc(ByVal nCode As Integer, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As Integer
  
  Private KBDLLHookProcDelegate As KBDLLHookProc = New KBDLLHookProc(AddressOf KeyboardProc)
  Private HHookID As IntPtr = IntPtr.Zero
  
  Private Function KeyboardProc(ByVal nCode As Integer, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As Integer
      If (nCode = HC_ACTION) Then
          Dim struct As KBDLLHOOKSTRUCT
          Select Case wParam
              Case WM_KEYDOWN, WM_SYSKEYDOWN
                  RaiseEvent KeyDown(CType(CType(Marshal.PtrToStructure(lParam, struct.GetType()), KBDLLHOOKSTRUCT).vkCode, Keys))
              Case WM_KEYUP, WM_SYSKEYUP
                  RaiseEvent KeyUp(CType(CType(Marshal.PtrToStructure(lParam, struct.GetType()), KBDLLHOOKSTRUCT).vkCode, Keys))
          End Select
      End If
      Return CallNextHookEx(IntPtr.Zero, nCode, wParam, lParam)
  End Function
  
  Public Sub New()
      HHookID = SetWindowsHookEx(WH_KEYBOARD_LL, KBDLLHookProcDelegate, System.Runtime.InteropServices.Marshal.GetHINSTANCE(System.Reflection.Assembly.GetExecutingAssembly.GetModules()(0)).ToInt32, 0)
      If HHookID = IntPtr.Zero Then
          Throw New Exception("Could not set keyboard hook")
      End If
  End Sub
  
  Protected Overrides Sub Finalize()
      If Not HHookID = IntPtr.Zero Then
          UnhookWindowsHookEx(HHookID)
      End If
      MyBase.Finalize()
  End Sub
  
  End Class
  </code></pre>
  
  <p>It works nice even if the app has focus or not but i have a problem. My application is using </p>
  
  <pre><code> My.Computer.Network.UploadFile
  </code></pre>
  
  <p>During the upload the kayboard strokes stop responding. Mouse is ok.</p>
  
  <p>How can i keep keyboard alive during other operations inside my application?</p>