Is there a way to check for a keypress every time through a loop without forcing a key to be pressed. ie. if you use scanf, the loop will not continue unless a key is pressed. I'd like to say "if a key was pressed, handle it. If not, keep going"

Thanks!

Dave

using the curses library would probably be the most portable way to do it. Check ncurses for *nix and pdcurses for MS-Windows. If you are not concerned about portability and using an MS-Windows compiler then your compiler MIGHT support the functions in conio.h -- mainly _kbhit().

hmm I looked at ncurses (very cool - i've always wanted to be able to use the arrow keys and function keys). I saw getch(), but nothing that doesn't stop the execution( ie. no kbhit() function).

Am I just missing it?

If you compiled the pdcurses win32 libraryt and demo program, the demo file newdemo.c contains the following code that appears to be doing what you want.

int WaitForUser(void)
{
	chtype ch;

	nodelay(stdscr, TRUE);
	halfdelay(50);

	ch = getch();

	nodelay(stdscr, FALSE);
	nocbreak();		/* Reset the halfdelay() value */
	cbreak();

	return (ch == '\033') ? ch : 0;
}
This article has been dead for over six months. Start a new discussion instead.