Operating System - HP-UX
1830898 Members
1830 Online
110017 Solutions
New Discussion

How to read a single char from keyboard without hitting ENTER

 
SOLVED
Go to solution
Max_4
Frequent Advisor

How to read a single char from keyboard without hitting ENTER

Hi all,

In C programming language, how can I read a single character from the keyboard without waiting for the RETURN key? Also, how can I stop characters from being echoed on the screen as they're typed (Not using stty -echo)?

Thanks to all....
10 REPLIES 10
harry d brown jr
Honored Contributor

Re: How to read a single char from keyboard without hitting ENTER

http://www.mkssoftware.com/docs/man3/getchar.3.asp

live free or die
harry
Live Free or Die
Max_4
Frequent Advisor

Re: How to read a single char from keyboard without hitting ENTER

Thanks harry,

but actually when you use getchar(),
getc(), these functions wait until you hit ENTER, even though they read char by char from the stream, they don't start reading until you hit ENTER... you could try it in a simple code:

#include

int main(void)
{
int c;

while ((c = getchar()) != EOF)
putchar(c);

return 0;
}
A. Clay Stephenson
Acclaimed Contributor
Solution

Re: How to read a single char from keyboard without hitting ENTER

I threw together a 2 minute C example from memory. Man termio for details.

Warning: make sure that you reset the terminal before exiting the program.



If it ain't broke, I can fix that.
Max_4
Frequent Advisor

Re: How to read a single char from keyboard without hitting ENTER

Thank you very much Clay!

That's exactly what I was looking for...
I have two additional questions about your code:
1) you didn't include ?
2) Would you mind explaining me those casts to (void) before some function calls? What are they for?

Again I really appreciate your help...

Thanks a lot...
A. Clay Stephenson
Acclaimed Contributor

Re: How to read a single char from keyboard without hitting ENTER

You certainly could include stdio.h. This was just a quickie example. The (void) cast simply says that I know this function returns a result but I choose to ignore it. One other thing that you may want to do is add ISIG so that
t.c_lflag &= ~(ICANON | IECHO);
becomes
t.c_lflag &= ~(ICANON | IECHO | ISIG);

this will have the effect that the normal interrupt, quit, ... characters have no effect
and allow you to actually read Cntl-C as a character. By the way, when you understand what that rather cryptic-lookings staements do, you are well on your way to using bitwise operators.

If I can find one handy, I'm sure that I have a finished version of this snippit that does buffer flushing and optional lower/upper case conversion. I'll post that version if I can find it.

If it ain't broke, I can fix that.
Max_4
Frequent Advisor

Re: How to read a single char from keyboard without hitting ENTER

Thanks again Clay for your
great help...
A. Clay Stephenson
Acclaimed Contributor

Re: How to read a single char from keyboard without hitting ENTER

I dug up a more robust version of this that I sometimes used in shell scripts:

e.g.
CH=$(onechar -L)
STAT=${?}
case ${STAT} in
0) echo "You pressed ${CH}"
;;
255) echo "This is an error" >&2
;;
*) echo "The ASCII value pressed is \c"
echo "${STAT} (dec)"
;;
esac

The idea is if 0 is returned, a printable character is output on stdout, a return of 255 indicates an error, any other returns indicates that a non-pritable ASCII character was received and the return value is the decimal equivalent and no character is output to stdout.

If you need to recognize function keys or arrow-keys then you really need to use curses. Man curses for details.


This is onechar.c. Compile it like this:
cc onechar.c -o onechar
If it ain't broke, I can fix that.
A. Clay Stephenson
Acclaimed Contributor

Re: How to read a single char from keyboard without hitting ENTER

I spent less than 1 minute making the changes to convert this to K&R C for those who lack a development compiler; this version will compile on the bundled compiler.

cc onechar2.c -o onechar

You can invoke either version as onechar -u for full usage.

Regards, Clay
If it ain't broke, I can fix that.
rewtme
Occasional Advisor

Re: How to read a single char from keyboard without hitting ENTER

You can do this without having to write anything in C. Here's an example:

print -n "Enter a character --> "
stty raw
readchar=`dd if=/dev/tty bs=1 count=1 2>/dev/null`
stty -raw
print "\nCharacter is $readchar"
Satish Y
Trusted Contributor

Re: How to read a single char from keyboard without hitting ENTER

In a simple way,

#include

and use function "getch()" instead of "getchar()".

Hope it will solve ur prob...

Cheers...
Satish.
Difference between good and the best is only a little effort