title: global keyboard hook slowing down computer
body: <p>I am using the code from this <a href="http://blogs.msdn.com/b/toub/archive/2006/05/03/589423.aspx" rel="nofollow">article</a> : </p>
  
  <pre><code>private const int WH_KEYBOARD_LL = 13;
  private const int WM_KEYDOWN = 0x0100;
  private static LowLevelKeyboardProc _proc = HookCallback;
  private static IntPtr _hookID = IntPtr.Zero;
  
  public static void Main()
  {
      _hookID = SetHook(_proc);
      //Application.Run();
      //UnhookWindowsHookEx(_hookID);
  }
  
  private static IntPtr SetHook(LowLevelKeyboardProc proc)
  {
      using (Process curProcess = Process.GetCurrentProcess())
      using (ProcessModule curModule = curProcess.MainModule)
      {
          return SetWindowsHookEx(WH_KEYBOARD_LL, proc,
              GetModuleHandle(curModule.ModuleName), 0);
      }
  }
  
  private delegate IntPtr LowLevelKeyboardProc(
      int nCode, IntPtr wParam, IntPtr lParam);
  
  private static IntPtr HookCallback(
      int nCode, IntPtr wParam, IntPtr lParam)
  {
      if (nCode &gt;= 0 &amp;&amp; wParam == (IntPtr)WM_KEYDOWN)
      {
          int vkCode = Marshal.ReadInt32(lParam);
          ***custom code***
      }
      return CallNextHookEx(_hookID, nCode, wParam, lParam);
  }
  
  [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
  private static extern IntPtr SetWindowsHookEx(int idHook,
      LowLevelKeyboardProc lpfn, IntPtr hMod, uint dwThreadId);
  
  [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
  [return: MarshalAs(UnmanagedType.Bool)]
  private static extern bool UnhookWindowsHookEx(IntPtr hhk);
  
  [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
  private static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode,
      IntPtr wParam, IntPtr lParam);
  
  [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
  private static extern IntPtr GetModuleHandle(string lpModuleName);
  </code></pre>
  
  <p>with a little modification but sometimes when i press a couple of keystrokes too fast then the computer "slows" down a bit and gets a little "laggy".</p>
  
  <p>So when it hits asyncronosly the event HookCallback it sometimes get a little laggy, but i guess its the code in this method "HookCallback" that makes it laggy or is it the hooking it self? I had an idea about creating a new thread everytime i enter "HookCallback" , that maybe will help but do i want to start a new thread everytime i press a key? I am getting an asyncronosly call already so i dont know if i should start another thread besides that.</p>
  
  <p>So my question is simple, where and why does it slow down the computer, is it the hooking it self or is it the custom code ? Or should i put the custom code in a different thread?</p>
  
  <p>And i have another question, sometimes after a few keypresses "HookCallback" doesnt get called, as if i have "unhooked" the event, and it wont capture any keystrokes at all. How  can i make sure that it never unhooks the event unless i do it in manually? </p>
  
  <p>Thank you.</p>