2

I'm trying to make a c++ snake game in linux using ansi escape codes but i can't figure out a way to get async keyboard input. Im a beginner and i'm currently reading a c++ book, so i didn't cover multithreading yet. Is there any chance to achieve this result in a "simple" way.(and standard if it's possible, i saw the kbhit solution) How to use kbhit and getch (C programming) . Can i solve the problem reading from the /dev/input event files?

Thank you guys.

  • 1
    There's no standard way. `/dev/input` is linux-specific, so it's not even remotely standard. – HolyBlackCat Jun 26 '20 at 14:33
  • Are you asking about asynchronous keyboard input or per-key-stroke keyboard input? You can perform asynchronous keyboard input via `std::async` or `std::thread` but that doesn't look it's what you're actually asking. – François Andrieux Jun 26 '20 at 14:58

1 Answers1

1

C++ does not offer a standard way to asynchronously read the keyboard status.

boost asio, as in read from keyboard using boost async_read and posix::stream_descriptor is one way. Some people consider boost like a "standard" or "normal" C++ extension.

Another way is to use a game library like SDL or SFML. You include their headers and you link with their libs.

Jeffrey
  • 11,063
  • 1
  • 21
  • 42