2.9 Using Getkey

The getkey function reads one character at a time with the read system call, unless TERM is not set. Normally, read does not return any characters until a RETURN is typed. Thus, getkey does not receive any characters until a RETURN is typed. For getkey to work properly, when TERM is defined, the ttyinit function calls ioctl (see ioctl(2)) to allow typed characters to be returned immediately. This mode causes each character to be sent as it is typed, and getkey can return a value each time a key is pressed. Also, ttyinit turns off character echoing so that escape sequences sent by the terminal are not echoed back to the terminal. It is then the responsibility of the your program to echo typed characters back to the screen. The following code could be used to do this.

#include <ctype.h>

c = getkey();

/* echo printable characters */

if (isascii (c) && isprint (c))

putchar (c);

If TERM is not set, getkey uses getch which reads a single character without echoing it back to the screen.