title: How can I detect if any key is pressed in C++?
body: <p>I'm looking for a way to detect if any key is pressed in a Windows environment C++ console application. Emphasis on any, not a specific key or set of keys. I've searched all over this website and I can't find an answer. The best I've found is to use the GetKeyState function or the GetAsyncKeyState but with no examples of how to use it, and I've checked the documentation on Microsoft's website too and still am unsure of how I would use it.</p>
  
  <p>If this is relevant and it helps, I am using Visual Studio, and when creating this project, I selected Empty Project, not Console App.</p>
  
  <p>The outline of the program is expected to look like this:</p>
  
  <p><a href="https://i.stack.imgur.com/ShuWo.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ShuWo.jpg" alt="Clock flowchart"></a></p>
  
  <p>Here is the code I have used so far in my main .cpp file:</p>
  
  <pre><code>#ifndef MAINCPP
  #define MAINCPP
  
  #include "TwelveClock.h"
  #include "TwentyFourClock.h"
  #include &lt;iostream&gt;
  #include &lt;iomanip&gt;
  #include &lt;Windows.h&gt;
  using namespace std;
  
  void displayMenu() {
      cout &lt;&lt; setw(27) &lt;&lt; setfill('*') &lt;&lt; "" &lt;&lt; endl;
      cout &lt;&lt; "* 1 -- Add One Hour" &lt;&lt; setfill(' ') &lt;&lt; setw(8) &lt;&lt; '*' &lt;&lt; endl;
      cout &lt;&lt; "* 2 -- Add One Minute" &lt;&lt; setw(6) &lt;&lt; '*' &lt;&lt; endl;
      cout &lt;&lt; "* 3 -- Add One Second" &lt;&lt; setw(6) &lt;&lt; '*' &lt;&lt; endl;
      cout &lt;&lt; "* 4 -- Exit Program" &lt;&lt; setw(8) &lt;&lt; '*' &lt;&lt; endl;
      cout &lt;&lt; setw(27) &lt;&lt; setfill('*') &lt;&lt; "" &lt;&lt; endl;
  }
  
  void displayClocks(TwentyFourClock&amp; tfc, TwelveClock&amp; tc) {
      cout &lt;&lt; setfill('*') &lt;&lt; setw(24) &lt;&lt; "" &lt;&lt; "    " &lt;&lt; setw(25) &lt;&lt; "" &lt;&lt; endl;
      cout &lt;&lt; setfill(' ') &lt;&lt; "*" &lt;&lt; setw(17) &lt;&lt; "12-Hour Clock" &lt;&lt; setw(6) &lt;&lt; "*" &lt;&lt; "    *" &lt;&lt; setw(17) &lt;&lt; "24-Hour Clock" &lt;&lt; setw(7) &lt;&lt; "*" &lt;&lt; endl;
  
      cout &lt;&lt; "*" &lt;&lt; setw(5) &lt;&lt; "" &lt;&lt; setw(2) &lt;&lt; setfill('0') &lt;&lt; tc.Get_hour() &lt;&lt; ":" &lt;&lt; setw(2) &lt;&lt; tc.Get_minute() &lt;&lt; ":" &lt;&lt; setw(2) &lt;&lt; tc.Get_second() &lt;&lt; " " &lt;&lt; tc.Get_strPM_status() &lt;&lt;setfill(' ') &lt;&lt; setw(7) &lt;&lt; "*";
      cout &lt;&lt; "    *" &lt;&lt; setw(7) &lt;&lt; "" &lt;&lt; setw(2) &lt;&lt; setfill('0') &lt;&lt; tfc.Get_hour() &lt;&lt; ":" &lt;&lt; setw(2) &lt;&lt; tfc.Get_minute() &lt;&lt; ":" &lt;&lt; setw(2) &lt;&lt; tfc.Get_second() &lt;&lt; setfill(' ') &lt;&lt; setw(9) &lt;&lt; "*" &lt;&lt; endl;
  
      cout &lt;&lt; setfill('*') &lt;&lt; setw(24) &lt;&lt; "" &lt;&lt; "    " &lt;&lt; setw(25) &lt;&lt; "" &lt;&lt; endl;
  }
  
  int main() {
      TwentyFourClock TwentyFour;
      TwelveClock Twelve;
  
      while (1) {
  
          system("cls"); 
          displayClocks(TwentyFour, Twelve);
  
          //FIXME: check if button is pressed 
  
          TwentyFour.Set_second(TwentyFour.Get_second() + 1);
          Twelve.Set_second(Twelve.Get_second() + 1);
          Sleep(1000);
      }
  
      return 0;
  }
  
  #endif
  </code></pre>
  
  <p>Also I'm concerned that my program won't even be able to detect any input because of the sleep function pausing the execution. If this is the case, what would be a better approach to this? I appreciate all help I get.</p>