title: Keyboard hook storing keys
body: <p>I have implemented a keyboard hook as presented in <a href="https://stackoverflow.com/questions/11377977/global-hotkeys-in-wpf-working-from-every-window/11378213#11378213">this</a> answer. Everything works fine except for one case. If I want to hold a key pressed for some time, thus technically remaning in the method which I'm calling, two thing happen: </p>
  
  <ol>
  <li>I remain in the method (which is good)</li>
  <li>the application records the key being held down, as independent keystrokes which are then captured by the hook.  </li>
  </ol>
  
  <p>Hook:  </p>
  
  <pre><code>private IntPtr HwndHook(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
      {
  
          const int WM_HOTKEY = 0x0312;
              switch (msg)
              {
                  case WM_HOTKEY:
                      if (battleBotStatus == 0)
                      {
                          switch (wParam.ToInt32())
                          {
  
                              case HOTKEY_ID:
                                  OnHotKeyPressed();
                                  handled = true;
                                  break;
  
                              case HOTKEY_ID9:
                                  enableBattleBotKeys();
                                  battleBotStatus = 1;
                                  //performCombo();
                                  handled = true;
                                  break;
                          }
                      }
                      else
                      {
                          switch (wParam.ToInt32())
                          {
  
                              case BattleKey1:
                                  performCombo(1);
                                  handled = true;                                    
                                  buttonIsPressed = false;
                                  break;
                          }
                      }
                      break;
              }
          return IntPtr.Zero;
      }
  </code></pre>
  
  <p>PerformCombo code  </p>
  
  <pre><code>private void performCombo(int n)
          {
              string simpleAction;
              Sleep(cmbDelay);
              Key k = new Key();
              switch (n) 
              {
                  case 1:
                      k = Key.D1;
                      break;
                  case 2:
                      k = Key.D2;
                      break;
                  case 3:
                      k = Key.D3;
                      break;
                  case 4:
                      k = Key.D4;
                      break;
                  case 5:
                      k = Key.D5;
                      break;
                  case 6:
                      k = Key.D6;
                      break;
              }
          if ((Keyboard.GetKeyStates(k) &amp; KeyStates.Down) &gt; 0)
              Sleep(1000);
          do
          {
              if (comboList[n - 1].Count &gt; 0)
              {
                  foreach (string action in comboList[n - 1])
                  {
                      simpleAction = action.Split('~')[0];
                      if (simpleAction.Length == 1)
                      {
                          dkeyinput(simpleAction, gamePid, 500);
                          Sleep(cmbDelay);
                      }
                      else
                      {
                          if (simpleAction == "LeftClick")
                          {
                              dmouseclick("left", 300);
                              Sleep(cmbDelay);
                          }
                          else if (simpleAction == "RightClick")
                          {
                              dmouseclick("right", 300);
                              Sleep(cmbDelay);
                          }
                          else if (simpleAction == "Space")
                          {
                              dkeyinput("\"{SPACE}\"", gamePid);
                              Sleep(cmbDelay);
                          }
                          else if (simpleAction.Contains("&amp;"))
                          {
                              dkeyinput2(simpleAction, cmbDelay);
                              Sleep(cmbDelay);
                          }
                      }
                  }
              }
          }
          while ((Keyboard.GetKeyStates(k) &amp; KeyStates.Down) &gt; 0);
  
  
      }  
  </code></pre>
  
  <p>I have tried to use flags, but they didn't seem to work either. When inspecting the using the debugger, I've noticed that it constantly goes onto the handler and continously perfoms the action for the <code>BattleKey1</code> case, even after releasing the key.</p>