-1

I'm trying to create a program who check if a specific key in keyboard is pressed and return a boolean value into a while loop

something like this:

int main(int argc, char** argv){

   std::cout << "Press the spacebar to exit loop";
   while (true){
      if (IsKeyPressed("space")){
         break;
      }
   }
   return 0;
}

I use Linux if this matters.

Marcus Müller
  • 34,677
  • 4
  • 53
  • 94
  • 1
    https://stackoverflow.com/search?q=%5Bc%2B%2B%5D+detect+key+press -- please do this before asking yet another question. As a new user here, please also take the [tour] and read [ask]. – Ulrich Eckhardt Nov 08 '21 at 19:47
  • Hello! Your English is excellent! I've removed your apology, we can understand what you mean well enough :) – Marcus Müller Nov 08 '21 at 19:47

1 Answers1

0

The C++ standard alone does not have any functionality for reading input. You have to use an external library if you want to read input. Any game development library will have features for this, such as SFML.

With SFML:

#include <SFML/Window.hpp>
int main(int argc, char** argv){

   std::cout << "Press the spacebar to exit loop";
   while (true){
      if (sf::Keyboard::isKeyPressed(sf::Keyboard::Space)){
         break;
      }
   }
   return 0;
}
JensB
  • 839
  • 4
  • 19