polling keyboard

cinsk@nownuri.net cinsk@nownuri.net
Fri Oct 1 00:00:00 GMT 1999


lostin_space wrote:

> I would like to get character input from either the keyboard or serial
> port. I want to poll the device and if there is something available to
> input, then input it. In Borland or MS C compilers there is a
> "keypressed" function that you can use as follows:
>
> if ( kbhit () )
>    char_in = getc (stdin);
>
> Is threre anything similar in one of the libraries with gcc ? I am
> runnin linux ( RH6.0 ).

If you have some experience with curses library (in Linux, use ncurses
package),
you can perform kbhit() operation by calling nodelay() and wgetch().

wgetch() hangs until user press any key, but after calling nodelay(),
wgetch()
returns immediately with ERR if no character is waiting in the input
queue.

But this method force you to use curses library.

Another method is using select(2) function. Unfortunately, I don't how to
make
kbhit() action using this one. But I know where the code with select(2)
is.

Contact your favorite bookstore and find the book titled,
"Beginning Linux Programming" written by Richard Stones published by
Wrox Press.





More information about the Gcc-help mailing list