read a single character from the keyboard without waiting for RETURN

read a single character from the keyboard without waiting for RETURN

Post by Moti Gind » Sun, 14 Feb 1999 04:00:00



Does anyone know how EXACTLLY can I read a single character from the
keyboard without waiting for the RETURN key in UNIX?
thanks.
moti.
 
 
 

read a single character from the keyboard without waiting for RETURN

Post by Andrew Giert » Sun, 14 Feb 1999 04:00:00


 Moti> Does anyone know how EXACTLLY can I read a single character
 Moti> from the keyboard without waiting for the RETURN key in UNIX?

The information you need is in the FAQ.

--
Andrew.

comp.unix.programmer FAQ: see <URL: http://www.erlenstar.demon.co.uk/unix/>
                           or <URL: http://www.whitefang.com/unix/>

 
 
 

read a single character from the keyboard without waiting for RETURN

Post by Manish Ahuj » Tue, 16 Feb 1999 04:00:00


Well You could put ur terminal in raw mode and get a character by
character. Look up richard stevens or some other book about raw and
cooked modes, or man page about ioctl().

> Does anyone know how EXACTLLY can I read a single character from the
> keyboard without waiting for the RETURN key in UNIX?
> thanks.
> moti.

 
 
 

read a single character from the keyboard without waiting for RETURN

Post by Binand Raj » Wed, 17 Feb 1999 04:00:00


Quote:> Does anyone know how EXACTLLY can I read a single character from the
> keyboard without waiting for the RETURN key in UNIX?
> thanks.
> moti.

The FAQ has this question, but only gives a solution which uses
the system() function. Here is a version using ioctl's and it works
on the two systems I tested it: a linux 2.0.32 (gcc 2.8.1) and a
DEC Alpha machine with the native C compiler. I would like someone to
comment on the portability of this code (on Unix systems, of course).

/* code to read one character without waiting for return to be pressed */
#include <stdio.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <termios.h>

int
main (void)
{
   struct termios old, new;
   int fd, c;
   fd = fileno (stdin); /* is fd = 0; better? */
   ioctl (fd, TCGETS, &old);
   ioctl (fd, TCGETS, &new);
   new.c_lflag &= ~ICANON;
   printf ("Press any key to continue... ");
   ioctl (fd, TCSETS, &new);
   c = fgetc (stdin);
   ioctl (fd, TCSETS, &old);
   printf ("\nYou pressed the key %c to continue.\n", c);
   return 0;

Quote:}

 
 
 

read a single character from the keyboard without waiting for RETURN

Post by Andrew Giert » Wed, 17 Feb 1999 04:00:00


 >> Does anyone know how EXACTLLY can I read a single character from
 >> the keyboard without waiting for the RETURN key in UNIX?

 Binand> The FAQ has this question, but only gives a solution which
 Binand> uses the system() function.

Nonsense.

 Binand> Here is a version using ioctl's

forget ioctls. tcgetattr() and tcsetattr() are the right way to do this.

(Especially, *NEVER EVER* use ioctls in combination with "struct termios";
that's not even minimally portable.)

--
Andrew.

comp.unix.programmer FAQ: see <URL: http://www.erlenstar.demon.co.uk/unix/>
                           or <URL: http://www.whitefang.com/unix/>

 
 
 

1. Need help reading single character without carriage return.

I am looking for a faq or docs that detail reading a single character
from stdin without echoing the character and without waiting for a carriage
return. I am using gcc compiler and lib that came with Linux distribution.
Any assistance would be appreciated. Email would be preferable, I will
post the resolution.

Thank you.
Email

2. Old Monitor Specs - Samsung

3. Need to read a single character from the keyboard without a CR

4. Adaptec 2940 PCI 32-bit

5. Reading a key press without waiting for a carriage return?

6. Q: Windows Fonts?

7. reading a single char without waiting for ENTER...

8. LILO hangs at "L" solution

9. Reading a key press without waiting for a carriage return?

10. How to read a single character from the keyboard?

11. Getting a keystroke without waiting for RETURN

12. get character without waiting

13. How do you test stdin for waiting characters without blocking?