title: Why does my low level windows key hook stop working?
body: <p>This is a continuation of my original question
  <a href="https://stackoverflow.com/questions/8934374/why-is-d3d10sdklayers-dll-loaded-during-my-dx11-game">Why is D3D10SDKLayers.dll loaded during my DX11 game?</a> I am creating a DX11 game and am using a low-level windows key hook to capture Alt+Enter so that I can toggle fullscreen using my own methods instead of having Windows do it automatically, which inevitably causes problems. A description of this process and details are available in the linked question. My problem is that the key hook consistently stops working after the 6th Alt+Enter for some reason. I am not unregistering it myself. </p>
  
  <p>Here is the key hook code:</p>
  
  <pre><code>
  LRESULT _stdcall MyClass::WindowsKeyHook( s32 nCode, WPARAM wParam, LPARAM lParam ) {
      printf("Key hook called, nCode: %d. ", nCode);
      if( nCode &lt; 0 || nCode != HC_ACTION )  { // do not process message 
          return CallNextHookEx( MyClassVar.GetWindowsKeyHook(), nCode, wParam, lParam );
      }
      printf(" Key hook status ok.\n");
  
      BOOL bEatKeystroke = FALSE;
      KBDLLHOOKSTRUCT* p = ( KBDLLHOOKSTRUCT* )lParam;
      switch( wParam ) {
          //NOTE: Alt seems to be a system key when it is PRESSED, but a regular key when it is released...
          case WM_SYSKEYDOWN:
              if(p-&gt;vkCode == VK_MENU || p-&gt;vkCode == VK_LMENU || p-&gt;vkCode == VK_RMENU) {
                  MyClassVar.SetAltPressed(TRUE);
              }
              if(MyClassVar.IsAltPressed() && p-&gt;vkCode == VK_RETURN) {
                  bEatKeystroke = TRUE;
                  MyClassVar.SetAltEnterUsed(TRUE);
                  printf("Alt+Enter used.\n");
              }
              break;
          case WM_SYSKEYUP:
              //NOTE: releasing alt+enter causes a SYSKEYUP message with code 0x13: PAUSE key...
              break;
          case WM_KEYDOWN:
              break;
          case WM_KEYUP: {
              if(p-&gt;vkCode == VK_MENU || p-&gt;vkCode == VK_LMENU || p-&gt;vkCode == VK_RMENU) {
                  MyClassVar.SetAltPressed(FALSE);
              }
              bEatKeystroke = ( !MyClassVar.IsShortcutKeysAllowed() &&
                                  ( p-&gt;vkCode == VK_LWIN || p-&gt;vkCode == VK_RWIN ) );
              break;
          }
      }
  
      if( bEatKeystroke ) {
          return 1;
      }
      else {
          return CallNextHookEx( MyClassVar.GetWindowsKeyHook(), nCode, wParam, lParam );
      }
  }
  </code></pre>
  
  <p>If you need more information, just tell me what is needed. I have no idea why this is happening, so I'm not sure what kind of information I need to provide. The only way to get rid of a key hook besides unregistering it explicitly is if Windows times it out, as far as I know. All MyClassVar methods are inline to be as fast as possible, and Alt+Enter is handled from a separate thread. </p>