getting a key from stdin in UNIX

Gary Mathews gm at cunixd.cc.columbia.edu
Mon Feb 5 06:56:31 AEST 1990


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

If I use the curses library the getch command works fine under cbreak mode,
but I want to use it without using an entire library for one command.

the macro for getc in stdio.h is as follows:
#define	getc(p)		--(p)->_cnt>=0? ((int)*(p)->_ptr++):_filbuf(p))

I've used variations of getc and the following, but I can't seem to get
the optimum results...

  fgets(s1, 1, stdin);
  c = fread(&buf, sizeof(char), 1, stdin);
  scanf("%c", c);
  c = read(0, buf, sizeof(char));

How do you get a key without pressing enter?
Does anyone have a solution for this?

-------------------------------------------------------------------------------
Gary Jason Mathews      | gm at cunixd.cc.columbia.edu
Columbia University     | Death is life's way of telling you you've been fired.
------------------------+ CPU time flies when you have a lot of bugs



More information about the Comp.lang.c mailing list