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.

2
  • 1
    There's no standard way. /dev/input is linux-specific, so it's not even remotely standard. Commented Jun 26, 2020 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. Commented Jun 26, 2020 at 14:58

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.