473,403 Members | 2,293 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

Character pressed

How can I know if any character has been typed without using the kbhit
of <conio.h>. The function should not block the program.
Jul 23 '05 #1
5 1324
* DiegoC:
How can I know if any character has been typed without using the kbhit
of <conio.h>. The function should not block the program.


This is not covered by the standard language or library; use other
libraries (e.g. your operating system's API).

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 23 '05 #2
Alf P. Steinbach wrote:
* DiegoC:
How can I know if any character has been typed without using the kbhit
of <conio.h>. The function should not block the program.


This is not covered by the standard language or library; use other
libraries (e.g. your operating system's API).


Well, there is std::cin.rdbuf()->in_avail() tocheck how many characters are
available for reading, but it's not guaranteed to work correctly on all
implementations.

Jul 23 '05 #3
* Rolf Magnus:
Alf P. Steinbach wrote:
* DiegoC:
How can I know if any character has been typed without using the kbhit
of <conio.h>. The function should not block the program.
This is not covered by the standard language or library; use other
libraries (e.g. your operating system's API).


Well, there is std::cin.rdbuf()->in_avail() tocheck how many characters are
available for reading,


AFAIK in_avail() is not required to check lower level buffers.

And when it does not, relying on it to check for interactive keypresses
may cause an infinite loop.

Or other bad things; I've not bothered to check out the finer details
of the modern iostreams since they're not usable for serious work (in
particular, reading anything but pure text has Undefined Behavior since
the conversion defers to sscanf which has UB for nonconforming input,
but to not scare newbies needlessly I don't usually mention that...).

but it's not guaranteed to work correctly on all implementations.


Nothing is guaranteed to work correctly on all implementations, but
everything is guaranteed to work correctly on correct implementations.

Correct does not in this case, AFAICS, extend to provide the OP's
functionality.

Is there something I may have overlooked?

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 23 '05 #4

"Rolf Magnus" <ra******@t-online.de> wrote in message news:d4*************@news.t-online.com...
| Alf P. Steinbach wrote:
|
| > * DiegoC:
| >> How can I know if any character has been typed without using the kbhit
| >> of <conio.h>. The function should not block the program.
| >
| > This is not covered by the standard language or library; use other
| > libraries (e.g. your operating system's API).
|
| Well, there is std::cin.rdbuf()->in_avail() tocheck how many characters are
| available for reading, but it's not guaranteed to work correctly on all
| implementations.

That's what I hear too - It's a pity.
Having said that, it has always done the job for me,
even for the compiler implementations that have been
accused of having problems with it.

Cheers,
Chris Val
Jul 23 '05 #5
Alf P. Steinbach wrote:
* Rolf Magnus:
Alf P. Steinbach wrote:
> * DiegoC:
>> How can I know if any character has been typed without using the kbhit
>> of <conio.h>. The function should not block the program.
>
> This is not covered by the standard language or library; use other
> libraries (e.g. your operating system's API).
Well, there is std::cin.rdbuf()->in_avail() tocheck how many characters
are available for reading,


AFAIK in_avail() is not required to check lower level buffers.

And when it does not, relying on it to check for interactive keypresses
may cause an infinite loop.

Or other bad things; I've not bothered to check out the finer details
of the modern iostreams since they're not usable for serious work (in
particular, reading anything but pure text has Undefined Behavior since
the conversion defers to sscanf which has UB for nonconforming input,
but to not scare newbies needlessly I don't usually mention that...).

but it's not guaranteed to work correctly on all implementations.


Nothing is guaranteed to work correctly on all implementations, but
everything is guaranteed to work correctly on correct implementations.


If it's not a correct implementation, it's not an implementation ;-)
What I meant by "work correctly" is "do what the OP wants".
Correct does not in this case, AFAICS, extend to provide the OP's
functionality.

Is there something I may have overlooked?


That's what Stroustrup has to say about it in TC++PL:

<quote>
A call to in_avail() is used to see how many characters are available in the
buffer. This can be used to avoid waiting for input. When reading from a
stream connected to a keyboard, cin.get(c) might wait until the user comes
back from lunch. On some systems and for some applications, it can be
worthwile taking that into account when reading. For example:

if (cin.rdbuf()->in_avail()) { // get() will not block
cin.get(c);
// do something
}
else { // get() might block
// do something else
}

Note that some systems, it can be hard to determine if input is available.
Thus, in_avail() might be (poorly) implemented to return 0 in cases where
an input operation would succeed.
</quote>

This pretty much means that you are right in that using in_avail() might
result in an endless loop that never reads input, because in_avail() always
returns 0.

Jul 23 '05 #6

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

Similar topics

3
by: Noah | last post by:
I have a text field in a form. I want the user to be able to click a DELETE button and have the character at the cursor position deleted. This would be just as if the user had pressed the Back...
5
by: Chris Williams | last post by:
Hi all, I am becoming a little stressed with the age-old problem of detecting when a user types (for example) a quote character ("). On a UK keyboard, I can look for a 2 + shift mask; on US,...
5
by: Johnsy Joseph | last post by:
Hello Everybody, I am trying to write a program that reads a single character, but I always need to press the enter key too. Is there any way to read just a single character without waiting for...
0
by: kurotsuke | last post by:
I need to convert a sequence of keys presses on the keyboard into the corresponding character code (UNICODE). I'm intercepting the KeyUp event (using an external hooking library) and need to get...
2
by: Mike P | last post by:
How would I create a button which when pressed outputs a non-English character (ä, ö etc) to a text box? I've seen a website where it was done, but I don't remember the name of the site. Any...
4
by: Ricky W. Hunt | last post by:
Is there is a way to "change" what character the user typed? For instance, is there anything I can put in a Keypress handler that if the user pressed the <Enter> key it make it appear to the...
25
by: ehabaziz2001 | last post by:
Why I can not begin my subscript of character arrrays with 0. In this program I can not do : do { na=getchar(); i++; na=getchar(); } while (na!='\n');
3
by: Jeff | last post by:
I have had this function work perfectly in IE and am trying to get it to work in Firefox. I have seen plenty of questions and answers on the web for how to get and check the key pressed but nothing...
1
by: Abhishek Bhatt | last post by:
Need ASCII of the character user entered e.g. if user pressed ALT+128 then the character dispalyed is Ç. How to get the number 128 from this character? Using ASC function gives me 199
4
by: Jules | last post by:
I am trying to understand the behavior of a short program. Hopefully someone can tell me what's going on. The program I wrote is: #include <stdio.h> main() { char c; while(1)
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.