strange getch+kbhit behaviour

S

simnav

In the following code something strange happens ! If I keep pressed any
of ALT+Arrow, keys, they are extracted two times from buffer then getch
seems to stop; if I release and press again ALT+arrow nothing changes:
the only way to exit from this condition is press another key a single
time.
What seems to happen is that kbhit say some keys are present in buffer
but for getch don't see any data and waits for a key. Why this happen ?
If I remove the delay all is working correctly. I've inserted a delay
to reproduce thi problem that I've found on another bigger project
where loop delay is 155ms.
Last thing: I've verified this problem only on newer motherboards !

Please help me !!
Thank you.
Simone


#include <stdio.h>
#include <dpmi.h>

int Handle()
{
int vv;

int lo, hi;

__dpmi_regs reg;

reg.x.ax = 0x10 << 8; /* shift 10h into AH */
__dpmi_int( 0x16, &reg);

vv= reg.x.ax ;
lo = vv & 0X00FF;
hi= ( vv & 0XFF00) >> 8;
vv=( ((lo == 0)|(lo == 224)) ? hi+256 : lo);

return vv;

} // Handle


int main(void)
{
int i;

do
{
i=0;
while(kbhit())
{
fprintf(stdout,"i=%d\n",i++);
fprintf(stdout,"%c\n",getch());
}

fprintf(stdout,"%d\n",Handle());
delay(155);

} while(1);
}
 
J

jacob navia

simnav said:
In the following code something strange happens ! If I keep pressed any
of ALT+Arrow, keys, they are extracted two times from buffer then getch
seems to stop; if I release and press again ALT+arrow nothing changes:
the only way to exit from this condition is press another key a single
time.
What seems to happen is that kbhit say some keys are present in buffer
but for getch don't see any data and waits for a key. Why this happen ?
If I remove the delay all is working correctly. I've inserted a delay
to reproduce thi problem that I've found on another bigger project
where loop delay is 155ms.
Last thing: I've verified this problem only on newer motherboards !

Please help me !!
Thank you.
Simone


#include <stdio.h>
#include <dpmi.h>

int Handle()
{
int vv;

int lo, hi;

__dpmi_regs reg;

reg.x.ax = 0x10 << 8; /* shift 10h into AH */
__dpmi_int( 0x16, &reg);

vv= reg.x.ax ;
lo = vv & 0X00FF;
hi= ( vv & 0XFF00) >> 8;
vv=( ((lo == 0)|(lo == 224)) ? hi+256 : lo);

return vv;

} // Handle


int main(void)
{
int i;

do
{
i=0;
while(kbhit())
{
fprintf(stdout,"i=%d\n",i++);
fprintf(stdout,"%c\n",getch());
}

fprintf(stdout,"%d\n",Handle());
delay(155);

} while(1);
}

getch() probably expects an ASCII character, not just some
keyboard key
 
B

Ben Pfaff

simnav said:
In the following code something strange happens ! If I keep pressed any
of ALT+Arrow, keys, they are extracted two times from buffer then getch
seems to stop; if I release and press again ALT+arrow nothing changes:
the only way to exit from this condition is press another key a single
time.

Your article was off-topic the first three times you posted it.
It is still off-topic. Please take it to a group that discusses
getch and kbhit. I suspect you want an MS-DOS programming
newsgroup.
 
R

Richard Heathfield

jacob navia said:
simnav wrote:
getch() probably expects an ASCII character, not just some
keyboard key

That incorrect answer is a great example of why you should ask
platform-specific questions on a newsgroup devoted to the appropriate
platform.
 
T

Tak-Shing Chan

jacob navia said:



That incorrect answer is a great example of why you should ask
platform-specific questions on a newsgroup devoted to the appropriate
platform.

How could a sentence with the adverb ``probably'' be
incorrect? :) FWIW, ``probably'' probably means ``with
likelihood (though not with certainty)'' (OED 1989, 1).

Tak-Shing
 
R

Richard Heathfield

Tak-Shing Chan said:
How could a sentence with the adverb ``probably'' be
incorrect? :) FWIW, ``probably'' probably means ``with
likelihood (though not with certainty)'' (OED 1989, 1).

The probability is so low as to be not worth considering. To find out why,
ask in a newsgroup where getch() is topical.
 
T

Tak-Shing Chan

Tak-Shing Chan said:


The probability is so low as to be not worth considering. To find out why,
ask in a newsgroup where getch() is topical.

Given that "conio.h" is not included in the OP's code, it
might very well behave differently from the getch() you know of.

Tak-Shing
 
R

Richard Heathfield

Tak-Shing Chan said:
On Thu, 21 Sep 2006, Richard Heathfield wrote:


Given that "conio.h" is not included in the OP's code, it
might very well behave differently from the getch() you know of.

Were it topical so to do, I would ask which one you are referring to, since
I know of several. But since it isn't, I won't. I suggest you take this up
in a newsgroup where it's topical.
 
T

Tak-Shing Chan

Tak-Shing Chan said:


Were it topical so to do, I would ask which one you are referring to, since
I know of several. But since it isn't, I won't. I suggest you take this up
in a newsgroup where it's topical.

You have completely missed my point, which is that the OP
could have been using a userland kbhit() and getch(), not the
ones provided by the implementation.

Tak-Shing
 
C

Coos Haak

Op Thu, 21 Sep 2006 18:28:15 +0100 schreef Tak-Shing Chan:
Given that "conio.h" is not included in the OP's code, it
might very well behave differently from the getch() you know of.

Tak-Shing

Would the following not be off-topic?
#include <dpmi.h> Non-standard header.
int Handle()
{
int vv;

int lo, hi;

__dpmi_regs reg;
Use of underscores is not recommended to non-implementers.
reg.x.ax = 0x10 << 8; /* shift 10h into AH */
C does not know anything about registers. I do, but not in this news group.
__dpmi_int( 0x16, &reg);
Nor about interrupts.
 
C

CBFalconer

Tak-Shing Chan said:
. snip ...

You have completely missed my point, which is that the OP
could have been using a userland kbhit() and getch(), not the
ones provided by the implementation.

You have completely missed Richards point, which is that those
things are off-topic here. The only way to make them on-topic is
to include the actual code for kbhit() and getch(), written in ISO
standard C. The OP did not so do. You compounded the error by
discussing them instead of referring the OP to a suitable group, or
advising him of the non-topicality involved.

--
Some informative links:
http://www.geocities.com/nnqweb/
http://www.catb.org/~esr/faqs/smart-questions.html
http://www.caliburn.nl/topposting.html
http://www.netmeister.org/news/learn2quote.html
 
T

Tak-Shing Chan

Tak-Shing Chan said:



No, I haven't. You just think I have.

My point is that there is a possibility that getch() has
nothing to do with the OP's platform, in which case ``a newsgroup
where it's topical'' does not exist. You have certainly missed
this point.

Tak-Shing
 
T

Tak-Shing Chan

Op Thu, 21 Sep 2006 18:28:15 +0100 schreef Tak-Shing Chan:
[snip]

Given that "conio.h" is not included in the OP's code, it
might very well behave differently from the getch() you know of.

Tak-Shing

Would the following not be off-topic?
#include <dpmi.h>
Non-standard header.

I did not write this. Learn how to quote!

[snip]

Tak-Shing

P.S. I am aware that the OP is off-topic. However, my point was
that getch() is not a reserved identifier in ISO C (which is
topical enough).
 
R

Richard Heathfield

Tak-Shing Chan said:
My point is that there is a possibility that getch() has
nothing to do with the OP's platform,

Yes, but it's a possibility having low probability, as you well know.
 
T

Tak-Shing Chan

You have completely missed Richards point, which is that those
things are off-topic here. The only way to make them on-topic is
to include the actual code for kbhit() and getch(), written in ISO
standard C. The OP did not so do. You compounded the error by
discussing them instead of referring the OP to a suitable group, or
advising him of the non-topicality involved.

Where did I say that the OP is on-topic? In fact, I wrote
``the OP is off-topic'' in this very thread! My main point was
that getch is not a reserved identifier in ISO C (but my verbiage
was perhaps a bit convoluted).

Tak-Shing
 
S

simnav

Richard Heathfield wrote:

+AD4- jacob navia said:
+AD4- +AD4- getch() probably expects an ASCII character, not just some
+AD4- +AD4- keyboard key
+AD4-
+AD4- That incorrect answer is a great example of why you should ask
+AD4- platform-specific questions on a newsgroup devoted to the
appropriate
+AD4- platform.

Please accept my apologies for the two mistakes I did: incorrect
newsgroup and multi post (my newsreader is driven crazy and sent it too
many times...)
Anyway Jacob response was wrong in ANSI context but seems to be correct
on dos/djgpp platform: getkey() resolved the problem.

Thank you,
Simone
 
R

Richard Heathfield

simnav said:
Richard Heathfield wrote:

+AD4- jacob navia said:
+AD4- +AD4- getch() probably expects an ASCII character, not just some
+AD4- +AD4- keyboard key
+AD4-
+AD4- That incorrect answer is a great example of why you should ask
+AD4- platform-specific questions on a newsgroup devoted to the
appropriate
+AD4- platform.

Please accept my apologies for the two mistakes I did: incorrect
newsgroup and multi post (my newsreader is driven crazy and sent it too
many times...)
Anyway Jacob response was wrong in ANSI context but seems to be correct
on dos/djgpp platform:

Not even correct in /that/ context, because Delorie followed Borland's lead
with getch, and Navia's response was incompatible with Borland C's
behaviour and therefore incompatible with DJGPP's behaviour. You'd be
better off finding out how to use DJGPP's getch in a DOS programmers'
newsgroup.
getkey() resolved the problem.

....but didn't teach you why getch() didn't behave as you expected. You might
want to chase that down, in the interests of understanding what on earth is
going on.
 
K

Kenneth Brody

Tak-Shing Chan said:
jacob navia said: [...]
getch() probably expects an ASCII character, not just some
keyboard key

That incorrect answer is a great example of why you should ask
platform-specific questions on a newsgroup devoted to the appropriate
platform.

How could a sentence with the adverb ``probably'' be
incorrect? :) FWIW, ``probably'' probably means ``with
likelihood (though not with certainty)'' (OED 1989, 1).

"The sky will probably be green tomorrow."

"Passing random values to free() will probably not cause any problems."

"The next C standard will probably add the Windows API as part of the
standard library."

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:[email protected]>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top