PDA

View Full Version : Press any key to continue


fragmer
2006.05.10, 03:54 AM
Hello;
I'm writing an extremely simple console utility in C. I need to implement a simple "press any key to continue" pause, which I usually did with kbhit(), getch() or getche() back in the Windows days. But none of that, of course, works on MacOS.

Is there a way to make such a "press any key" pause without having to mess with unbuffered input modes and obscure libraries?

Thanks in advance!

Fenris
2006.05.10, 08:30 AM
scanf(""); ?

Hog
2006.05.10, 09:33 AM
something like (maybe do more error checking):

#include <stdio.h>
#include <termios.h>
#include <unistd.h>

...

struct termios tattributes,rawtattributes;

/* get console attributes */

tcgetattr(STDIN_FILENO,&tattributes);
rawtattributes = tattributes;

/* set the console to raw mode */
cfmakeraw(&rawtattributes);
tcsetattr(STDIN_FILENO,TCSANOW,&rawtattributes);

...
getchar();
...

/* set the console to normal mode */
tcsetattr(STDIN_FILENO,TCSANOW,&tattributes);


however this doesn't work in the xcode "run log"

akb825
2006.05.10, 02:22 PM
scanf(""); ?
Either that or getc(stdin);

SOUCHAN
2006.05.14, 05:22 AM
Neither scanf("") nor getchar() (a.k.a. getc(stdin)) will work here.

Using a scanf("") will will return immediately since it has nothing to read. Using getchar() is little better, since it only returns once you press the return key.

I haven't tried Hog's method, but it looks promising. You basically just want to do a blocked read() from stdin, so if you can set stdin to block on reads and then try a read() on it that should work, too.