Topic: kbhit and C++
Author: Christopher Eltschka <celtschk@physik.tu-muenchen.de>
Date: 1997/04/14 Raw View
Steve Clamage wrote:
>
> In article 10E8@nowhere.com, Michael Hudson <sorry.no.email@nowhere.com> writes:
> >Steve Clamage wrote:
> >>
> >> "Sean L. Palmer" <seanpalmer@mindspring.com> writes:
> >>
> >> >> Is there an equivalant function of kbhit() in C++? If there isn't,
> >> >> how do I determine when a key on the keyboard has been pressed.
> >
> >I have used cin.rdbuf()->in_avail() to see if there are any keypresses I
> >haven't found out about yet, but I have no idea if it's portable. (It
> >worked fine for me - I used cin.ignore(cin.rdbuf()->in_avail()) to clear
> >the buffer, although there must be a better way).
>
> I wonder why you included my name in the attributions without including
> anything I said. In the part you elminated, I explained that kbhit
> and in_avail are completely different functions. I'll explain again in
> more detail.
>
> The in_avail function tells you how many characters are currently in
> the stream's internal buffer. There might be others characters that would
> be fetched by a call to underflow, or there might have been characters
> typed at the keyboard that haven't yet been delivered to the process
> if the keyboard driver is waiting for a "Return" to be typed. If in_avail
> reports that characters are available, then there indeed are unprocessed
> characters; otherwise, you don't know whether there are any unprocessed
> characters.
>
> Upon looking at the many comments about kbhit in recent articles,
> it was clear to me that no two people were thinking of the same
> definition for kbhit -- I'm sure some were thinking of possible
> implementations that would be reasonable or interesting, but
> which probably don't exist.
>
> The only kbhit I am familiar with was on MSDOS systems. The function
> is not and never was part of standard C or C++. (The FAQ for comp.lang.c
> has a lot to say about kbhit.) That function makes the assumption that
> only one process is running, and the computer has one and only one
> physical keyboad attached to the process. It has no relationship to any
> stdio FILE or iostream. It bypasses all FILE and stream information and
> looks specifically at the physical keyboard driver. It answers the
> question, "Has any key been pressed but not yet read by the process?"
> It is an OS-level function for particular types of systems.
>
> The entire purpose of stdio and iostreams is to provide device-
> independent I/O -- to isolate you from peculiarities of specific
> systems. The kbhit function does not reflect the stream model of I/O
> that these libraries support. If you don't want general-purpose
> I/O, but to deal with special-purpose devices, you need to use
> some other technique.
BTW, the situation that characters may arrive in the future but have
not arrived yet is not special to a keyboard, but applies in general
to about every input stream which does not come directly from a file
like pipes, serial ports, etc.
I think there should be a standard way of "there *might* me another
char in the future, but it can't be delivered yet". The call should
be non-blocking. If the special input source doesn't allow
non-blocking reads, rdbuf->inavail() could be called. This would
give the function (let's call it charavail) the following semantics:
If charavail() returns true, a following call to get() is guaranteed
to return a char immediatly. If charavail() returns false, a following
call to charavail *might* have to wait for input (BTW, you can't
guarantee this wait anyway, as a character might have arrived just
between the call to charavail() ant the call to get()).
Especially if rdbuf()->inavail() is true, charavail() is true.
This charavail would need support by the streambuf as well. This
could be achieved by a streambuf function charavail(), which either
checks the source to get the result (for cin this would be the analog
to kbhit), or simply returns false, if the source doesn't allow to
check for it. The only demand for it is, that *if* it returns true,
the analog guarantee as from istream::charavail() is given.
The return value false is allowed in any case (but it is encouraged
not to be given, if the opposite can be determined).
The streambuf::charavail should be virtual and be given a standard
implementation to return "false".
The istream::charavail() could be implemented as
bool istream::charavail()
{
return rdbuf()->inavail() || rdbuf()->charavail();
}
To find out if charavail() checking is supported by the streambuf
(i.e. the charavail() function may give true at some call), there
should also be a (virtual) streambuf member to check this, say
availchecked(), which should also have a forwarder in istream.
This is to allow programs loops like
while(!cin.charavail())
do_something();
for streams that don't support charavail() (at the streambuf level;
the istream::charavail still may return true if there are characters
in the buffer), which would get an endless loop there (if the buffer
is empty).
I think this should work on any system that supports streams (because
of the possibility of simply returning "false" if the source doesn't
allow non-blocking checks if data is available, together with
being able to check if this option was chosen).
---
[ comp.std.c++ is moderated. To submit articles: Try just posting with your
newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu
]
Author: Stephen.Clamage@eng.sun.com (Steve Clamage)
Date: 1997/03/26 Raw View
In article 10E8@nowhere.com, Michael Hudson <sorry.no.email@nowhere.com> writes:
>Steve Clamage wrote:
>>
>> "Sean L. Palmer" <seanpalmer@mindspring.com> writes:
>>
>> >> Is there an equivalant function of kbhit() in C++? If there isn't,
>> >> how do I determine when a key on the keyboard has been pressed.
>
>I have used cin.rdbuf()->in_avail() to see if there are any keypresses I
>haven't found out about yet, but I have no idea if it's portable. (It
>worked fine for me - I used cin.ignore(cin.rdbuf()->in_avail()) to clear
>the buffer, although there must be a better way).
I wonder why you included my name in the attributions without including
anything I said. In the part you elminated, I explained that kbhit
and in_avail are completely different functions. I'll explain again in
more detail.
The in_avail function tells you how many characters are currently in
the stream's internal buffer. There might be others characters that would
be fetched by a call to underflow, or there might have been characters
typed at the keyboard that haven't yet been delivered to the process
if the keyboard driver is waiting for a "Return" to be typed. If in_avail
reports that characters are available, then there indeed are unprocessed
characters; otherwise, you don't know whether there are any unprocessed
characters.
Upon looking at the many comments about kbhit in recent articles,
it was clear to me that no two people were thinking of the same
definition for kbhit -- I'm sure some were thinking of possible
implementations that would be reasonable or interesting, but
which probably don't exist.
The only kbhit I am familiar with was on MSDOS systems. The function
is not and never was part of standard C or C++. (The FAQ for comp.lang.c
has a lot to say about kbhit.) That function makes the assumption that
only one process is running, and the computer has one and only one
physical keyboad attached to the process. It has no relationship to any
stdio FILE or iostream. It bypasses all FILE and stream information and
looks specifically at the physical keyboard driver. It answers the
question, "Has any key been pressed but not yet read by the process?"
It is an OS-level function for particular types of systems.
The entire purpose of stdio and iostreams is to provide device-
independent I/O -- to isolate you from peculiarities of specific
systems. The kbhit function does not reflect the stream model of I/O
that these libraries support. If you don't want general-purpose
I/O, but to deal with special-purpose devices, you need to use
some other technique.
---
Steve Clamage, stephen.clamage@eng.sun.com
---
[ comp.std.c++ is moderated. To submit articles: try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]
Author: "Sean L. Palmer" <seanpalmer@mindspring.com>
Date: 1997/03/16 Raw View
> > >> Is there an equivalant function of kbhit() in C++? If there isn't,
> > >> how do I determine when a key on the keyboard has been pressed.
>
> I have used cin.rdbuf()->in_avail() to see if there are any keypresses I
> haven't found out about yet, but I have no idea if it's portable. (It
> worked fine for me - I used cin.ignore(cin.rdbuf()->in_avail()) to clear
> the buffer, although there must be a better way).
I think it's ridiculous that the stream itself doesn't have a function for
this simple thing, and you have to bypass and go to the streambuf for this
purpose.
For the input stream, when it indeed is connected to the keyboard and not
redirected, there is no such thing as eof really, and if there is it's
non-portable, so I think eof should mean that there are no characters
currently waiting to be read (nothing has been typed)
Then again I'm biased against the batch-process unix-style pipe redirection
view of i/o. Most programs that the user must interact with require more
information about what's going on at the keyboard. It's simple to check
whether the stream has any characters that haven't been read yet. There is
a hack for it above. So why not make a simple standard easy way to get
that same information? Without having to go to the streambuf?
---
[ comp.std.c++ is moderated. To submit articles: try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]
Author: Michael Hudson <sorry.no.email@nowhere.com>
Date: 1997/03/13 Raw View
Steve Clamage wrote:
>
> "Sean L. Palmer" <seanpalmer@mindspring.com> writes:
>
> >> Is there an equivalant function of kbhit() in C++? If there isn't,
> >> how do I determine when a key on the keyboard has been pressed.
I have used cin.rdbuf()->in_avail() to see if there are any keypresses I
haven't found out about yet, but I have no idea if it's portable. (It
worked fine for me - I used cin.ignore(cin.rdbuf()->in_avail()) to clear
the buffer, although there must be a better way).
--
Regards,
Michael Hudson
Please don't email this address - it's not mine.
---
[ comp.std.c++ is moderated. To submit articles: try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]
Author: Stephen.Clamage@eng.sun.com (Steve Clamage)
Date: 1997/03/12 Raw View
"Sean L. Palmer" <seanpalmer@mindspring.com> writes:
>> Is there an equivalant function of kbhit() in C++? If there isn't, how
>> do I determine when a key on the keyboard has been pressed.
>Try istream::peek() for starters. I myself haven't gotten this to work
>right, but I believe the upcoming standard intends this function for that
>general purpose: detecting whether input is available.
No, peek isn't the same. If kbhit is supported (it is non-standard
on any platform), it reports whether a character has been typed at
the keyboard and not yet read. (Note "the" keyboard, which assumes there
is a physical keyboard attached to the computer, and not more than one.)
The kbit function always returns immediately.
The peek function tells you what character the particular istream
will next return, but leaves the character unextracted. The istream
need not be connected to a keyboard, and if no character is currently
available, the peek function will not return until one becomes
available, or until eof is detected -- whichever comes first.
Neither function is a substitute for the other.
---
Steve Clamage, stephen.clamage@eng.sun.com
---
[ comp.std.c++ is moderated. To submit articles: try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]
Author: "Sean L. Palmer" <seanpalmer@mindspring.com>
Date: 1997/03/06 Raw View
> Is there an equivalant function of kbhit() in C++? If there isn't, how
> do I determine when a key on the keyboard has been pressed.
Try istream::peek() for starters. I myself haven't gotten this to work
right, but I believe the upcoming standard intends this function for that
general purpose: detecting whether input is available.
---
[ comp.std.c++ is moderated. To submit articles: Try just posting with your
newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu
]
Author: James Kanze <james-albert.kanze@vx.cit.alcatel.fr>
Date: 1997/03/07 Raw View
"Sean L. Palmer" <seanpalmer@mindspring.com> writes:
|> > Is there an equivalant function of kbhit() in C++? If there isn't, how
|> > do I determine when a key on the keyboard has been pressed.
|>
|> Try istream::peek() for starters. I myself haven't gotten this to work
|> right, but I believe the upcoming standard intends this function for that
|> general purpose: detecting whether input is available.
I don't think so. My interpretation is that it is to look at the next
character available, i.e.: a simplistic implementation of operator>>(
istream& , char* ) might be:
while( i.peek() != EOF && ! isspace( i.peek() ) )
*p ++ = i.get() ;
This is, at least, the behavior of this function in the existing
implementations I have used.
--
James Kanze home: kanze@gabi-soft.fr +33 (0)1 39 55 85 62
office: kanze@vx.cit.alcatel.fr +33 (0)1 69 63 14 54
GABI Software, Sarl., 22 rue Jacques-Lemercier, F-78000 Versailles France
-- Conseils en informatique industrielle --
---
[ comp.std.c++ is moderated. To submit articles: try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]
Author: getntds@sierra.net
Date: 1997/02/20 Raw View
{ I've suggested that this be added to the FAQ. -mod}
Is there an equivalant function of kbhit() in C++? If there isn't, how
do I determine when a key on the keyboard has been pressed.
John Vasquez
[ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
[ about comp.lang.c++.moderated. First time posters: do this! ]
Author: Marcelo Cantos <marcelo@mds.rmit.edu.au>
Date: 1997/02/21 Raw View
getntds@sierra.net wrote:
>
> { I've suggested that this be added to the FAQ. -mod}
>
> Is there an equivalant function of kbhit() in C++? If there isn't, how
> do I determine when a key on the keyboard has been pressed.
kbhit() is a DOS feature which examines the keyboard. This is not
a standard C or C++ feature and you will not find it on Unix, X,
Windows platforms in general. In fact Windows completely changes
the metaphor such that you receive messages telling you that
keyboard events have occurred rather than calling a function to
find out. X/Windows is the same.
In an attempt to be more helpful: what exactly are you trying to
achieve? This would help someone give a more useful reply.
--
___________________________________________________________________
Marcelo Cantos, Research Assistant marcelo@mds.rmit.edu.au
Multimedia Database Systems Group __/_ _ Tel 61-3-9282-2497
723 Swanston St, Carlton VIC 3053, Aus/ralia ><_>Fax 61-3-9282-2490
/
[ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
[ about comp.lang.c++.moderated. First time posters: do this! ]