title: GetKeyboardState can't detect while i am pressing the key
body: <p>I am trying to understand how <code>GetKeyboardState()</code> works. I have read through the introduction to virtual keys in the <a href="https://msdn.microsoft.com/en-ca/library/windows/desktop/ms646267(v=vs.85).aspx#_win32_Keyboard_Input_Model" rel="nofollow">Keyboard Input Model</a>
  and the <a href="https://msdn.microsoft.com/en-us/library/windows/desktop/ms646299.aspx" rel="nofollow"><code>GetKeyboardState()</code> documentation</a>.</p>
  
  <p>From my understanding, <code>GetKeyboardState()</code> should state all the key status in the array it returns. So I was trying to see if there is any difference in the following code when I toggle the <kbd>Caps Lock</kbd> key on and off:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;time.h&gt;
  #include &lt;conio.h&gt;
  #include &lt;string.h&gt;
  #include &lt;windows.h&gt;
  #include &lt;iostream&gt;
  int main(int argc, char *argv[])
  {
      INPUT buffer[1];
      bool stop = false;
      unsigned char kbstate[256];
      int timer = 0;
      MouseSetup(buffer);
      char result = 0;
      //doesn't work right now
      do{
  
          timer++;
          Sleep(3000);
          std::cout &lt;&lt; "function output: " &lt;&lt; GetKeyboardState(kbstate) &lt;&lt; std::endl;
          for (int i = 0; i &lt; 256; i++) {
              std::cout &lt;&lt; (int)kbstate[i];
              if (i % 16 == 0){
                  std::cout &lt;&lt; std::endl;
              }
          }
          std::cout &lt;&lt; std::endl;
      } while (!result);
      return 0;
  }
  </code></pre>
  
  <p>My output from both cap and uncap are the same.</p>
  
  <p>I guess the way I understand <code>GetKeyboardState()</code> is wrong. Can someone tell me where I am going wrong?</p>
  
  <hr>
  
  <p>Never mind, I got it. Here is what I really want to do. Thanks a lot for all your help guys.</p>
  
  <pre><code>int main(int argc, char *argv[])
  {
      INPUT buffer[1];
      bool stop = false;
      unsigned char ifPress[256];
      int timer = 0;
      char result = 0;
      std::cout &lt;&lt; "press any key to quite" &lt;&lt; std::endl;
      do{
          timer++;
          Sleep(100);
          //check all the status if any key is pressed
          for(int i = 0; i &lt; 256; i++) {
              ifPress[i] = GetAsyncKeyState(i);
          }
          //if any key is pressed, change the status to the result
          //and the loop will be quit
          for (int j = 1; j &lt; 256; j++){
              result = result || ifPress[j];
          }
      } while (!result);
      return 0;
  }
  </code></pre>