3

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. ?

ApprenticeHacker
  • 21,351
  • 27
  • 103
  • 153
  • http://www.cprogramming.com/fod/kbhit.html – kenny Sep 18 '11 at 11:29
  • Writing multi-language source files is difficult. I suggest you keep each source file in only a single, distinct language – pmg Sep 18 '11 at 11:37
  • @pmg I don't think that's what his intention was. – Paul Manta Sep 18 '11 at 11:44
  • @Paul: the question is tagged both `C` and `C++`. What I mean is for the OP to study the answers and maintain the `C` solution in its own source file and the `C++` in another source file. If I misunderstood the question and what the OP wants is a single solution that works both in `C` and `C++` I suspect he won't get many answers :-) – pmg Sep 18 '11 at 11:50
  • @pmg Sorry I guess I was the one who misunderstood your comment. :) – ApprenticeHacker Sep 18 '11 at 11:57
  • Standard C++ has no such solution, but then again. Standard Pascal doesn't have `readkey` either. – MSalters Sep 19 '11 at 08:50

5 Answers5

6

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)
pmg
  • 106,608
  • 13
  • 126
  • 198
2

As far as I know there is no portable solution for your item. In windows, you can use the header <conio.h> which has a function called getch() for getting a char directly from the console. If you are in Linux, then you can use the library ncurses.

Mythli
  • 5,995
  • 2
  • 24
  • 31
  • So as you are saying that `getch()` isn't portable across other platforms. So how does `getch()` function behaves if I'm running my C program on Linux/unix. I believe `` header file is part of C standard library which will be available across other platforms too or that is an incorrect assumption? – RBT Sep 21 '16 at 02:58
  • @RBT `conio.h` is not a part of the standard C. It is not available in *nix. Better follow this thread: [Where is the header file on Linux? Why can't I find ?](https://stackoverflow.com/q/8792317) – brc-dd May 07 '21 at 16:09
1

If you're in a Windws run-time environment, you can use the non-standard C function kbhit( ). And there's a C-language Linux equivalent for Windows kbhit( ). The function does just what you want: it will tell you if a keyboard character has been typed without reading the character; or, alternatively, will read and deliver to you one character iff one has been typed. You can find it over here:

http://pwilson.net/sample.html

Scroll down to the paragraph headed "Single-character keyboard input for Linux and Unix"

HTH

Pete Wilson
  • 8,610
  • 6
  • 39
  • 51
0

Simple:

getchar();

Literally the equivalent for readkey() in C#.

Lori
  • 1,392
  • 1
  • 21
  • 29
0

Considered _getch ?

Claus Jørgensen
  • 25,882
  • 9
  • 87
  • 150