Input without pressing enter?

L

LaBird

Dear all,

I would like to ask if there is any C function that accept one
keystroke as input, without printing out what the user presses
and without the need to press enter as delimiter. getchar()
can accept a character, but it needs user to press enter to
accept the input.

Thanks very much.

Regards,
LaBird (Benny).
 
A

Andreas Kahari

Dear all,

I would like to ask if there is any C function that accept one
keystroke as input, without printing out what the user presses
and without the need to press enter as delimiter. getchar()
can accept a character, but it needs user to press enter to
accept the input.

There is no such function in C. Please read the FAQ at

http://www.eskimo.com/~scs/C-faq/q19.1.html
 
A

Andreas Kahari

Andreas Kahari <[email protected]> scribbled the following: [cut]
There is no such function in C. Please read the FAQ at

It's not that easy to answer. It actually depends on your OS settings.
It is fully possible that getchar() can receive characters as you type
them from the keyboard. The point is that getchar() receives characters
as they appear in stdin. But the connection between stdin and the
keyboard is none of C's business.

Do you by "settings" mean "buffered or unbuffered"?

I believe that's what the parenthasis at the end addresses:

(As an aside, note that simply using setbuf or setvbuf to
set stdin to unbuffered will not generally serve to allow
character-at-a-time input.)
 
J

John L

Joona I Palaste said:
It's not that easy to answer. It actually depends on your OS settings.
It is fully possible that getchar() can receive characters as you type
them from the keyboard. The point is that getchar() receives characters
as they appear in stdin. But the connection between stdin and the
keyboard is none of C's business.

In other words, you can do it in C using OS-specific library calls
which are off-topic here. You need to ask in a newsgroup specific
to the OS and/or compiler you are using. Maybe comp.unix.programmer
or ... well, one of the others.

Or, as Joona Palaste says, you can use standard C functions *after*
using OS-specific commands to put your system into the appropriate
state. Again, these are off-topic here and you should ask in
an OS-specific newsgroup.

John.
 
L

LaBird

Dear Andreas,

Thanks a lot. I am programming in Linux. According to your guide,
now I can search from the web for the exact functions specific to
Linux that serves what I wanted.

Regards,
LaBird (Benny).
 
J

Joona I Palaste

Andreas Kahari said:
Andreas Kahari <[email protected]> scribbled the following: [cut]
There is no such function in C. Please read the FAQ at

It's not that easy to answer. It actually depends on your OS settings.
It is fully possible that getchar() can receive characters as you type
them from the keyboard. The point is that getchar() receives characters
as they appear in stdin. But the connection between stdin and the
keyboard is none of C's business.
Do you by "settings" mean "buffered or unbuffered"?
I believe that's what the parenthasis at the end addresses:
(As an aside, note that simply using setbuf or setvbuf to
set stdin to unbuffered will not generally serve to allow
character-at-a-time input.)

No, I mean different settings than setbuf or setvbuf. I mean settings
that affect the OS level, which goes deeper than stdin.
 

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

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,054
Latest member
LucyCarper

Latest Threads

Top