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

You can, but it depends on your OS and Compiler.

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().

Salem - I'm using linux and g++ or icc

Ancient Dragon - I'll look into those.

~Dave

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;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.