473,883 Members | 3,134 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How, exactly, does kbhit( ) work?

June 28, 2004
I'm interested in finding a way to test the keyboard buffer
for MT/Not MT. Tried kbhit() in system.h but it didn't do
what I thought it would from reading the literature.
Samples: Compiler Docs said,
"Detects whether a keypress is available for reading."

Herbert Schildt said,
"If the user has pressed a key, this function returns
true(non-0), but does not read the character.
If no keystroke is pending, kbhit() returns false (0)."

Here is the test code. kbhit() executes between two
getchar() calls.
#include <stdio.h>
#include <system.h>
void main() {
int ig1=0, ig2=0, ik=-1;
ig1 = getchar();
ik = kbhit();
ig2 = getchar();
getchar();
printf("ig1=%d, ik=%d, ig2=%d", ig1,ik,ig2);
return;
}

At runtime, I pressed aAEnter, so that 'A' and '\n'
were in buffer when kbhit() executed, yet ik returned
as 0.
Where else would a keystroke be "waiting" or "available"
except in the buffer? If kbhit() does not look at the
buffer, what does it look at?
Nov 14 '05
17 18043
kal
rl*@hoekstra-uitgeverij.nl (Richard Bos) wrote in message news:<40******* *********@news. individual.net> ...
MT generally stands for Machine Translation.
Thank you! I would have never thought of that.
The OP must be trying to determine whether the buffer
contains language, or half-grammatical tripe.
Tripe can be nutritious (vegetarians please try rock-tripe.)
Here, in good ole Confederate Country, we are fond of chitlins.
Actually, _really_ modern compilers consider such crudities
as <conio.h> to be beneath them.
Of course, of course! BENEATH me is Mother Earth and seems to
be none the worse for it. In fact, I am rather fond of Her.
It's only those that try to be deceptively
almost-compatible with old compilers that support _kbhit().
True, true! But some of us have to make do with these
dastardly deceptive denizens of almost-compatible land.
It's completely wrong.
Tres bon. COMPLETELY wrong is as good as completely right.
_kbhit() is actually part of the Improved Luser Education
Extensions (and therefore can be found in <ilee.h>),
You don't say! Now, who would have thunk it?
and what it stands for is Killfile Bozo and Hit it.
How clever! One would have thought it would be "_kbhiti".
What it does is to automatically put the user who invokes
it in the company killfile, and then hit him sharply over
the head with the robot arm mallet found in the ILEE
hardware option pack.
No doubt the "option pack" is a marketing ploy to extract
more money from the populi. Alas, there are some of us
who can ill afford such luxuries.
There is a related function called _kbshock(),
kbs hock? How interesting!
Reminds me of ham hock, perhaps a side effect of chitlins.
which can be used to put bozos who post long, off-topic
posts to comp.lang.c in the Usenet killfile, and then punish
them in a similar way involving the keyboard and 5000 volts;
Merci bien! I am gald it is only volts (may Alessandro
Volta forgive me.) One is used to many times that opening
car doors on wintry days. Were it current (pardonnez moi,
André-Marie Ampère) it could be dangerous.
you should be grateful it hasn't been compiled into
Google Groups yet.
Sum, sum. I am very grateful. But I do have this nagging
feeling that "its" are LINKED into rather than COMPILED
into. No doubt I am quite mistaken as usual.
Indeed. For example, Mr. Schildt may have assumed that
his readers can't distinguish "void" from "int".
I do not know. I have never had the pleasure of accosting
Herr. Schildt with that or any other inquiry.
You could also learn to program portably instead of
obsoletely.
We will, we will! Unfortunately, at present, some of our
customers are using non-portable computers.
Richard


M. Richard, I deeply regret for offending your exquisite
sensibilites. Kindly accept my sincere condolences.
Nov 14 '05 #11
[snips]

On Wed, 30 Jun 2004 15:42:36 +0000, Dan Pop wrote:
You must be using an older compiler version. In newer ones
the function is "_kbhit()" and it is declared in <conio.h>.


Funny... I'm running a very recent version of gcc, and it doesn't have a
kbhit at all... nor, apparently, a conio.h.


In your humble opinion, what does the [OT] tag in the subject line mean?


That the post probably doesn't belong here _at all_... but since it is
here, and contains definitively incorrect information - that "newer
compiler versions" - such as gcc 3.x - actually do have _kbhit() defined
in <conio.g> - it should, perforce, be corrected.

Yes, and?
Nov 14 '05 #12
Da*****@cern.ch (Dan Pop) wrote in message news:<cb******* ***@sunnews.cer n.ch>...
In <Pi************ *************** *******@unix49. andrew.cmu.edu> "Arthur J. O'Dwyer" <aj*@nospam.and rew.cmu.edu> writes:

On Wed, 30 Jun 2004, Dan Pop wrote:

Kelsey Bjarnason <ke*****@xxnosp amyy.lightspeed .bc.ca> writes:
>On Tue, 29 Jun 2004 17:09:46 -0700, kal wrote:
>> You must be using an older compiler version. In newer ones
>> the function is "_kbhit()" and it is declared in <conio.h>.
>
>Funny... I'm running a very recent version of gcc, and it doesn't have a
>kbhit at all... nor, apparently, a conio.h.

In your humble opinion, what does the [OT] tag in the subject line mean?
Last I heard, it meant "Off Topic." But kal's statement was not
merely off-topic; it was WRONG WRONG WRONG.


Nope, it was right in an off topic context. The context of the
implementation the poster was talking about (whatever that was).
If you're suggesting everyone simply ignore wrong advice posted
under an 'OT' flag, that's a defensible position.


I'm merely suggesting that information posted under the [OT] tag should
be treated as such. It makes no sense to interpret it in the context of
the c.l.c newsgroup (otherwise it wouldn't be off topic in the first
place).
<OT>


Since the whole thread is flagged as off topic, does your <OT> stand for
"on topic"? ;-)
*My* libc has <conio.h>, and the function on *my* system is
called 'kbhit'. And they don't get any "newer" than this. :)


What makes you think that the OP was talking about *your* libc? ;-)

The inventor of kbhit and friends was Microsoft. Many of the Microsoft
extensions were already prefixed with an underscore, to make it obvious
that they were not standard features. Later, they wisely decided to
extend this practice to more of their extensions. To avoid breaking
existing code, they provided the non-prefixed names in a compatibility
library, something like oldnames.lib or so. I guess this is what kal
was talking about and he was not wrong, as long as [OT] is interpreted
the usual way: implementation/platform specific discussions or stuff that
doesn't have anything to do with C.

Dan

hugo27 July 1, 2004
What talk! I was going to thank you for your help,
but now cann't tell what's help.
My question(the subject) is clearly OT, but my problem
is not. I was assuming, perhaps unawares, that there
was one buffer at issue, but I think now there are two.
One is made by C in memory, and is connected with stdin,
or is stdin. The other buffer is internal to the keyboard
control system, and it is this buffer that kbhit looks at.

Do these two buffers interact or do they operate independently?
Either way, in my test code, by the time kbhit executed
the internal buffer was empty(MT) or never received the data
(it went directly to stdin). That's why kbhit returned 0. My problem, then, is to test stdin for empty/not empty.
Is this possible in standard C?

hugo

Nov 14 '05 #13
In <pa************ *************** *@xxnospamyy.li ghtspeed.bc.ca> Kelsey Bjarnason <ke*****@xxnosp amyy.lightspeed .bc.ca> writes:
[snips]

On Wed, 30 Jun 2004 15:42:36 +0000, Dan Pop wrote:
You must be using an older compiler version. In newer ones
the function is "_kbhit()" and it is declared in <conio.h>.

Funny... I'm running a very recent version of gcc, and it doesn't have a
kbhit at all... nor, apparently, a conio.h.


In your humble opinion, what does the [OT] tag in the subject line mean?


That the post probably doesn't belong here _at all_... but since it is
here, and contains definitively incorrect information - that "newer
compiler versions" - such as gcc 3.x - actually do have _kbhit() defined
in <conio.g> - it should, perforce, be corrected.

Yes, and?


If it contained accurate technical information, in the context of the
newsgroup, it wouldn't have been off topic in the first place. As it was,
it contained accurate technical information in a different context, hence
the [OT] tag.

It was *your* mistake it to extrapolate it to a different context than
the one intended by the poster.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #14
[snips]

Dan Pop wrote:
It was *your* mistake it to extrapolate it to a different context than
the one intended by the poster.


You mean, lke, say, the context of _C_, which is exactly what we discuss
here? :)
Nov 14 '05 #15
k_*****@yahoo.c om (kal) wrote in message news:<a5******* *************** ****@posting.go ogle.com>...
rl*@hoekstra-uitgeverij.nl (Richard Bos) wrote in message news:<40******* *********@news. individual.net> ...
MT generally stands for Machine Translation.


Thank you! I would have never thought of that.
The OP must be trying to determine whether the buffer
contains language, or half-grammatical tripe.


Tripe can be nutritious (vegetarians please try rock-tripe.)
Here, in good ole Confederate Country, we are fond of chitlins.
Actually, _really_ modern compilers consider such crudities
as <conio.h> to be beneath them.


Of course, of course! BENEATH me is Mother Earth and seems to
be none the worse for it. In fact, I am rather fond of Her.
It's only those that try to be deceptively
almost-compatible with old compilers that support _kbhit().


True, true! But some of us have to make do with these
dastardly deceptive denizens of almost-compatible land.
It's completely wrong.


Tres bon. COMPLETELY wrong is as good as completely right.
_kbhit() is actually part of the Improved Luser Education
Extensions (and therefore can be found in <ilee.h>),


You don't say! Now, who would have thunk it?
and what it stands for is Killfile Bozo and Hit it.


How clever! One would have thought it would be "_kbhiti".
What it does is to automatically put the user who invokes
it in the company killfile, and then hit him sharply over
the head with the robot arm mallet found in the ILEE
hardware option pack.


No doubt the "option pack" is a marketing ploy to extract
more money from the populi. Alas, there are some of us
who can ill afford such luxuries.
There is a related function called _kbshock(),


kbs hock? How interesting!
Reminds me of ham hock, perhaps a side effect of chitlins.
which can be used to put bozos who post long, off-topic
posts to comp.lang.c in the Usenet killfile, and then punish
them in a similar way involving the keyboard and 5000 volts;


Merci bien! I am gald it is only volts (may Alessandro
Volta forgive me.) One is used to many times that opening
car doors on wintry days. Were it current (pardonnez moi,
André-Marie Ampère) it could be dangerous.
you should be grateful it hasn't been compiled into
Google Groups yet.


Sum, sum. I am very grateful. But I do have this nagging
feeling that "its" are LINKED into rather than COMPILED
into. No doubt I am quite mistaken as usual.
Indeed. For example, Mr. Schildt may have assumed that
his readers can't distinguish "void" from "int".


I do not know. I have never had the pleasure of accosting
Herr. Schildt with that or any other inquiry.
You could also learn to program portably instead of
obsoletely.


We will, we will! Unfortunately, at present, some of our
customers are using non-portable computers.
Richard


M. Richard, I deeply regret for offending your exquisite
sensibilites. Kindly accept my sincere condolences.
hugo27 July 1, 2004

What talk! I was going to thank you for your help,
but now cann't tell what's help.
My question(the subject) is clearly OT(i see now), but my problem
is not. I was assuming, perhaps unawares, that there
was one buffer at issue, but I think now there are two.
One is made by C in memory, and is connected with stdin,
or is stdin. The other buffer is internal to the keyboard
control system, and it is this buffer that kbhit looks at.

Do these two buffers interact or do they operate independently?
Either way, in my test code, by the time kbhit executed
the internal buffer was empty(MT) or never received the data
(it went directly to stdin). That's why kbhit returned 0.
My problem, then, is to test stdin for empty/not empty.
Is this possible in standard C?

hugo

Nov 14 '05 #16
ob****@yahoo.co m (hugo27) writes:
[...]

hugo:

When you reply to a message, please don't prefix your own new text
with '>' characters. It makes your entire article look like it's just
quoting a previous article, and is likely to cause it to be ignored.

It's also a good idea to trim most of the article to which you're
replying, leaving just enough to make the context clear.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #17
In <rv************ @loki.silversap phire.com> Kelsey Bjarnason <ke*****@lights peed.ca> writes:
[snips]

Dan Pop wrote:
It was *your* mistake it to extrapolate it to a different context than
the one intended by the poster.


You mean, lke, say, the context of _C_, which is exactly what we discuss
here? :)


If this was the intended context, then why the [OT] tag? ;-)

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #18

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
6482
by: Peter | last post by:
L.S. I am developing a PHP-login script (on Lycos Tripod) that uses Session to pass on variables. Below is the entire (stripped) structure that I use. It opens a page where you can Set and Read the session variable BUT ... It doesn't work!!! It seems that both set- and readlink open their own private session. How can I get the read-link to access the proper session variable?
3
4084
by: cv | last post by:
Hello All, I have used MultipartRequest like the following to upload images. MultipartRequest multi = new MultipartRequest(request, "../webapps/coreprogram/dealerlogos", 1024 * 1024); It works fine. But When a user accesses through Internet, this does not work.
4
4510
by: Field | last post by:
Hi, the following snippet shows once executed this output: 2 2 I'd have rather expected this output: 2 10
4
2895
by: Das | last post by:
Hi, I have made an application in ASP.net with C#. The application works fine with localhost. I have uploaded the site. I'm using web user controls in the form. but some of the button do not work when they are clicked. One thing that I have found that they are nested user controls. which do not work. I'm unable to understand what is the problem with that. thanks in advance. Das
1
3139
by: Newbie in ChiTown | last post by:
Here's my code: I am using MS Access and I am trying to update a table (InvoiceDetails) with data input by the user on a form. However, it does not update nor does it give me an error message. Code listed below. Also, can you recommend a book on SQL. Thanks. Dim con As Object Dim rs As ADODB.Recordset
11
3405
by: Jim | last post by:
Hi, I want to schedule a Python program that reads the command line for input. However, when adding an argument to the command line Python will not pick it up when using Windows scheduled tasks. How do I get it to work? Thanks, Jim
0
9944
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9797
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11154
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10762
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10422
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7136
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
6005
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4622
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4228
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.