getting a key from stdin in UNIX

Wade Guthrie evil at arcturus.UUCP
Thu Feb 8 03:40:15 AEST 1990


Gary Mathews writes:

>	I want to be able to press a key and respond to that key without
>pressing the <ENTER> key, such as getc on TurboC...

Chris Torek writes:

[...]
>It should therefore not appear in this newsgroup.

Sorry to disagree with you on this one, Chris, but what about those of us 
that can answer the question in a non-machine specific way?  

Try using curses, a public domain package that exists on many systems including 
Unix, VMS (albeit in a bass-ackwards sort of way), and MS-DOS.  By putting the 
terminal into 'raw' mode, you can do what is being asked.  In fact, I end up
using curses a lot to handle portable menus and such.

Your code should look something like:

	#include <curses.h>

	main(argc, argv)
	...
		initscr();	/* starts curses */
		...
		raw();		/* puts the input mode to raw */
		c = getch();	/* in raw mode, this just gets the char -- no
				 * newline required */
		noraw();	/* returns to cooked mode */
		...
		endwin();
	}

Or some such.  Check out the curses manual for details.


Wade Guthrie
evil at arcturus.UUCP
Rockwell International; Anaheim, CA

(Rockwell doesn't necessarily believe / stand by what I'm saying; how could
they when *I* don't even know what I'm talking about???)



More information about the Comp.lang.c mailing list