[Subject Prev][Subject Next][Thread Prev][Thread Next][Subject Index][Thread Index]
Re: [LI] Help: C code ...
Hi,
>> [3] How do I get input from the kbd; getchar() echoes the char
>> to the terminal (Something I don't want) and with extended keys, it looks
>> ugly (^[[[A for F1 for example).
>> >> getpasswdchar(), else curses supports non-echoeing input.
>If I am not wrong there should be some IOCTL call or whatever to disable
>echoing.
To get unechoed & unbuffered characters without using the curses library,
you could use the termios functions:
#include <termios.h>
#include <unistd.h>
struct termios attrib; // A place to store the current terminal attributes
int fd;
fd = fileno(stdin); // Get integer descriptor for stream stdin
if (isatty(fd)) // if stdin is not a tty -(redirected I/O), you might want
to skip this
{
tcgetattr(fd,&attrib); // Save current attributes for terminal
attrib.c_lflag &= ~(ECHO+ICANON); // Turn off echo & buffering
// Alternately you can use the cfmakeraw() function to set the tty to raw
mode
// But this is not required if you just want unbuffered&unechoed chars
tcsetattr(fd,TCSANOW,&attrib); // Set the new attributes
// Do your stuff here ....getchar() will now return immediately with a
character without // echo. You'll need to handle Backspace, newline,Del
etc by yourselves
//Restore old attributes when done
attrib |= ECHO+ICANON;
tcsetattr(fd,TCSANOW,&attrib);
}
Check the man page termios(3) for more details.
Kala
--------------------------------------------------------------------
The Linux India Mailing List Archives are now available. Please search
the archive at http://lists.linux-india.org/ before posting your question
to avoid repetition and save bandwidth.