Is there an equivelant for Pascal's readkey; in C or C++?

ApprenticeHacker picture ApprenticeHacker · Sep 18, 2011 · Viewed 11.3k times · Source

I am looking for a Console function that waits for the user to press a key. I want it to be like Pascal's readkey; as in a Console Only solution. No GUI library / Graphics Library / Windowing Library / WinApi Calls (Windows). It should be cross-platform and (preferably) part of the C std library or C++ Class Library. So is there any such function / class method etc. ?

Answer

pmg picture pmg · Sep 18, 2011

The C Standard library has no notion of a "keyboard buffer". input/output is mostly line-based (triggered when ENTER is pressed).

You have a few options:

  • use an external library like ncurses
  • change the terminal buffering strategy with setvbuf() and use fgetc() (and wait for ENTER if you didn't change the buffering strategy)