0

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

3
Contributors
5
Replies
6
Views
9 Years
Discussion Span
Last Post by Ancient Dragon
0

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

0

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

Ancient Dragon - I'll look into those.

~Dave

0

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?

0

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 topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.