title: Hook keyboard from injected DLL using KeyboardProc / SetWindowsHookEx
body: <p>Note: I am working in plain C. Not C++, not C#.</p>
  
  <p>I am working on a mod. I've already written a working DLL-injector, as well as the DLL to be injected. Everything is going well, apart from the userinput.</p>
  
  <p>I want to be able to use hotkeys, so I tried to setup a keyboardhook using SetWindowsHookEx.
  The following is my callback function:</p>
  
  <pre><code>LRESULT CALLBACK keyboardHook(int nCode, WPARAM wParam, LPARAM lParam)
  {
      printf("key touched\n");
      if (wParam == VK_F5)
      {
          keyEvent = VK_F5;
      }
      return CallNextHookEx(NULL, nCode, wParam, lParam);
  }
  </code></pre>
  
  <p>And this is how I set it up:</p>
  
  <pre><code>HHOOK kbHookHandle = SetWindowsHookEx(WH_KEYBOARD,(HOOKPROC)keyboardHook, NULL, GetCurrentThreadId());
  
  if (kbHookHandle != NULL)
  {
      printf("keyboard hook successful!\n");
  }
  else
  {
      printf("keyboard hook failed!\n");
  }
  </code></pre>
  
  <p>As far as I can tell, the hook gets setup well (I used to have a problem with an invalid parameter, but fixed that by using GetCurrentThreadID). It returns a handle which is not NULL.</p>
  
  <p>But whenever I press a key, there is no output.</p>
  
  <p>To further clarify:
  The code above is from the injected DLL. So it effectively 'belongs' to the game process. I have allocated a console using AllocConsole, to print debug messages to.</p>
  
  <p>What am I doing wrong?</p>
  
  <p>EDIT:
  To clarify (even more): the listed code is from the <strong>injected</strong> DLL. It is not the approach I use to inject the DLL - I wrote a seperate (working!) program to do just that.</p>
  
  <p>It surprises some that I use printf(), since that wouldn't show up, considering I call it from inside the host process. <strong>Yes</strong>, I do call it from inside the host process, but that is not an issue, because I have already allocated a <strong>working</strong> console. I used an approach very similar to the one mentioned <a href="http://www.halcyon.com/~ast/dload/guicon.htm" rel="nofollow">here</a></p>
  
  <p>EDIT2:
  I am not asking why printf() isn't working (because it is), I am asking why this keyboardhook isn't working.</p>