Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to get KeyPressed() or kbhit() in ncurses?

3,951 views
Skip to first unread message

Ian R Matters

unread,
Nov 1, 1994, 4:37:15 PM11/1/94
to
I'm trying to write some basic interactive routines using and need a way
of checking the input buffer to see if anything is there prior to using a
getchar() or whatever. I cannot find something like KeyPressed() or
kbhit() in the documentation. How should I do it?

Thanks,
Ian Matters (Ian.M...@anu.edu.au)

Rasmus Lerdorf

unread,
Nov 2, 1994, 5:58:01 AM11/2/94
to
irm...@huxley.anu.edu.au (Ian R Matters) writes:

>I'm trying to write some basic interactive routines using and need a way
>of checking the input buffer to see if anything is there prior to using a
>getchar() or whatever. I cannot find something like KeyPressed() or
>kbhit() in the documentation. How should I do it?

Look at the source to ncurses. In particular libgetch.c. You will
find that ncurses uses a 32 byte FIFO and that there are 3 queue numbers
that will help you figure out how many chars are in the queue. _fifohead,
_fifotail and _fifopeek. You will need to "extern" the SP structure in
your program and then you can simply check SP->_fifohead and SP->_fifotail
to see if there is anything in the type-ahead buffer. If SP->_fifohead is
-1 it is empty.

Note that ncurses1.8.6 is now available at ftp.netcom.com:/pub/zmbenhal/ncurses.

*Rasmus*

Bjorn Ekwall

unread,
Nov 2, 1994, 9:49:23 PM11/2/94
to

> Thanks,
> Ian Matters (Ian.M...@anu.edu.au)


Try this one...

==============================================================================
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>

/*
* Return 1 (true) if there are any unread characters, 0 (false) otherwise
*/
int
kbhit(void)
{
struct timeval timeout;
fd_set readfds;
int how;

/* look only at stdin (fd = 0) */
FD_ZERO(&readfds);
FD_SET(0, &readfds);

/* poll: return immediately */
timeout.tv_sec = 0L;
timeout.tv_usec = 0L;

how = select(1, &readfds, (fd_set *)NULL, (fd_set *)NULL, &timeout);
/* Change "&timeout" above to "(struct timeval *)0" ^^^^^^^^
* if you want to wait until a key is hit
*/

if ((how > 0) && FD_ISSET(0, &readfds))
return 1;
else
return 0;
}
=============================================================================

Bjorn Ekwall == bj...@blox.se

Zeyd M. Ben-Halim

unread,
Nov 2, 1994, 10:39:43 PM11/2/94
to
In article <397rbp$h...@ionews.io.org>, Rasmus Lerdorf <ras...@io.org> wrote:
>irm...@huxley.anu.edu.au (Ian R Matters) writes:
>
>>I'm trying to write some basic interactive routines using and need a way
>>of checking the input buffer to see if anything is there prior to using a
>>getchar() or whatever. I cannot find something like KeyPressed() or
>>kbhit() in the documentation. How should I do it?
>
>Look at the source to ncurses. In particular libgetch.c. You will
>find that ncurses uses a 32 byte FIFO and that there are 3 queue numbers
>that will help you figure out how many chars are in the queue. _fifohead,
>_fifotail and _fifopeek. You will need to "extern" the SP structure in
>your program and then you can simply check SP->_fifohead and SP->_fifotail
>to see if there is anything in the type-ahead buffer. If SP->_fifohead is
>-1 it is empty.

GACK, DON'T. man curs_inopts and lookup nodelay(). Also you might want to
read through doc/manual.doc

>Note that ncurses1.8.6 is now available at ftp.netcom.com:/pub/zmbenhal/ncurses.

OOPS, the cat is out of the bag :-) I'll announce as soon I've set the mailing
list up.

Zeyd

--
---
Zeyd M. Ben-Halim zmbe...@netcom.com
NCURSES is available from ftp.netcom.com:pub/zmbenhal/ncurses
Current version is 1.8.5

Ruurd Pels

unread,
Nov 3, 1994, 7:00:44 PM11/3/94
to
In article <Cynwo...@pe1chl.ampr.org>, Rob Janssen <pe1...@rabo.nl> wrote:

>>>I'm trying to write some basic interactive routines using and need a way
>>>of checking the input buffer to see if anything is there prior to using a
>>>getchar() or whatever. I cannot find something like KeyPressed() or
>>>kbhit() in the documentation. How should I do it?

>>Look at the source to ncurses. In particular libgetch.c. You will
>>find that ncurses uses a 32 byte FIFO and that there are 3 queue numbers
>>that will help you figure out how many chars are in the queue. _fifohead,
>>_fifotail and _fifopeek. You will need to "extern" the SP structure in
>>your program and then you can simply check SP->_fifohead and SP->_fifotail
>>to see if there is anything in the type-ahead buffer. If SP->_fifohead is
>>-1 it is empty.

Ugh!!!

>You are not going to tell us that this is really the way to do it,
>aren't you??

Here's the trick I use. It's not exactly kbhit(), but it insures that any
process keeps running. Initialization:

.
.
halfdelay(10); // <================= !!!!
.
.

No flames! This is not yet production ripe code. I'll explain. We did set
halfdelay to 10, which amounts to 1 second. What happens inside the
curses library is that the process is put to sleep for a maximum time
of one second or until a key is pressed. Then, if we receive an escape
character, we turn down the delay to zero, and immediately look if there
is another key coming. If it's another <Esc>, then return <Esc> as the
key pressed, else, you're in Meta-Sends-Esc-Prefix mode, so, we convert
the second key received into a meta-key by turning on bit 7.

int Application::GetACharacter()
{
int firstchar;
int nextchar;

if ((firstchar = getch()) == 0x1b)
{
halfdelay(0);
nextchar = getch();
if (nextchar != 0x1b)
{
// This is not a single escape character! So, this
// is meta as Esc-prefix...
firstchar = nextchar | METABIT;
}
halfdelay(10);
}
return firstchar;
}

In my main loop, there is a switch block as follows:

void Application::GetInputEvent(InputEvent& event)
{
int thechar;

if (!quitting && !terminate_self)
{
thechar = GetACharacter();

switch (thechar)
{
case ERR:
{
Idle(); // <=========== !!!
break;
}
.
.

case KEY_IC:
case KEY_IL:
{
if (insmode)
{
insmode = False;
curs_set(2);
}
else
{
insmode = True;
curs_set(1);
}
titlebar->SetModeIndicator(insmode);
break;
}
.
.
}

Update();

event.key = thechar;
event.insmode = insmode;

.
.

}
--
Grtz, RFP ;-)

|o| Ruurd Pels, Kamgras 187, 8935 EJ Leeuwarden, The Netherlands |o|
|o| ru...@autpels.nl EU.net!sun4nl!wtrlnd!maxine!autpels!ruurd |o|

Jim Harkins

unread,
Nov 10, 1994, 3:26:00 PM11/10/94
to
In article <39btjc$1...@bar.autpels.nl>,

Ruurd Pels <ru...@bar.autpels.nl> wrote:
>Here's the trick I use.
> .
> halfdelay(10); // <================= !!!!

Having spent more time than I care to admit porting DOS code to Unix
machines I must say that people who use C++ comments in C code should
be taken out and shot.

jim

--
"I want to die peacefully in my sleep like my grandfather. Not screaming
in terror like his passengers."

Jim Harkins jhar...@netcom.com
San Diego, CA.

Zeyd M. Ben-Halim

unread,
Nov 11, 1994, 2:16:06 AM11/11/94
to
In article <39btjc$1...@bar.autpels.nl>,
Ruurd Pels <ru...@bar.autpels.nl> wrote:
>In article <Cynwo...@pe1chl.ampr.org>, Rob Janssen <pe1...@rabo.nl> wrote:
>
>>>>I'm trying to write some basic interactive routines using and need a way
>>>>of checking the input buffer to see if anything is there prior to using a
>>>>getchar() or whatever. I cannot find something like KeyPressed() or
>>>>kbhit() in the documentation. How should I do it?
>
>>>Look at the source to ncurses. In particular libgetch.c. You will
>>>find that ncurses uses a 32 byte FIFO and that there are 3 queue numbers
>>>that will help you figure out how many chars are in the queue. _fifohead,
>>>_fifotail and _fifopeek. You will need to "extern" the SP structure in
>>>your program and then you can simply check SP->_fifohead and SP->_fifotail
>>>to see if there is anything in the type-ahead buffer. If SP->_fifohead is
>>>-1 it is empty.
>
>Ugh!!!
>
>>You are not going to tell us that this is really the way to do it,
>>aren't you??

It is definitely NOT the way to do it. Use nodelay(), it will return ERR if
no input is available.

Zeyd
--
---
Zeyd M. Ben-Halim zmbe...@netcom.com
NCURSES is available from ftp.netcom.com:pub/zmbenhal/ncurses

Current version is 1.8.6

Message has been deleted
0 new messages