[PS: this is off-topic for both linux-kernel and linux-net. Try
linux-admin@vger, or [EMAIL PROTECTED]]

shannon loi wrote:

> I'm writing an application using C language, in which
> I have a while loop that run infinitely, only stop if
> user hit 'q'on the keyboard. 
> Does Linux C library has a function to detect keyboard
> hit (like function _kbhit in Windows)?

No. Unix boxes historically haven't tended to have directly-attached
keyboards like PCs. Instead, they relied upon serial terminals for
user interaction. Consequently, "PC" Unices such as Linux tend to use
the keyboard and display to emulate one or more serial terminals.

The default setting for the virtual terminals is "canonical" mode. In
canonical mode, input is line-buffered; nothing is actually sent to
the application which is reading from the terminal until the user
presses Return. This allows the user to edit the line (with Backspace,
^U and ^W) without any help from the application.

Also, if you use the ANSI stdio functions, these will perform
additional buffering. By default, stdin and stdout are line buffered
if they are associated with a terminal device and fully buffered
(block buffered) otherwise. You have to make stdin unbuffered if you
want getchar() etc to read directly from the underlying descriptor.

If you want to be able to read each character as it is typed, you have
to:

1. disable line buffering in the terminal, and
2. disable line buffering in the stdio library,

with something like:

int set_raw_mode(void)
{
        int fd = STDIN_FILENO;
        struct termios t;

        if (tcgetattr(fd, &t) < 0)
        {
                perror("tcgetattr");
                return -1;
        }

        t.c_lflag &= ~ICANON;

        if (tcsetattr(fd, TCSANOW, &t) < 0)
        {
                perror("tcsetattr");
                return -1;
        }

        setbuf(stdin, NULL);

        return 0;
}

As for checking whether any input is available, you can use select()
or ioctl(FIONREAD).

If you are planning on doing much programming on Linux, it would be
worthwhile getting a decent book on Unix programming. It's quite
different to DOS.

-- 
Glynn Clements <[EMAIL PROTECTED]>
-
To unsubscribe from this list: send the line "unsubscribe linux-net" in
the body of a message to [EMAIL PROTECTED]

Reply via email to