Unbuffered keyboard reading

Hi,

I have a question about unbuffered reading from the keyboard. Is it possible
to read what key the user hit on the keyboard (as ASCII) without having to
wait for a carriage return? (in QNX 6.1)

I have tried the following:

char buffer;
int mConsole = open( ttyname(0), O_RDONLY );
read(mConsole, &buffer, 1);



It doesn’t work if the user don’t hit the enter button.

Does anyone know of a way that works. I have looked at the tc… functions.
I can get information about how many characters can be read, and i can even
insert a character. The problem is that the buffer is cleared when i do that
(insert a carriage return or flush the stream).

Thanks,
Antoine.

Antoine Haddad <anthad@foi.se> wrote:

char buffer;
int mConsole = open( ttyname(0), O_RDONLY );
read(mConsole, &buffer, 1);



It doesn’t work if the user don’t hit the enter button.

This should work:

raw(mConsole);
read(mConsole, &buffer, 1);
unraw(mConsole);


‘raw()’ and ‘unraw()’ follow:


static struct termios termios_p_old;
static int termios_p_old_ok = 0;

int raw( int fd )
{
struct termios termios_p;

if( tcgetattr( fd, &termios_p_old ) )
return( -1 );
termios_p_old_ok = 1;

memcpy (&termios_p, &termios_p_old, sizeof (termios_p ));

termios_p.c_cc[VMIN] = 1;
termios_p.c_cc[VTIME] = 0;
termios_p.c_lflag &= ~( ECHO|ICANON|ISIG|
ECHOE|ECHOK|ECHONL );
termios_p.c_oflag &= ~( OPOST );
return( tcsetattr( fd, TCSADRAIN, &termios_p ) );
}

int unraw( int fd )
{
if ( termios_p_old_ok )
return( tcsetattr( fd, TCSADRAIN, &termios_p_old ) );

return ( -1 );
}



HTH,


Karsten.


| / | __ ) | Karsten.Hoffmann@mbs-software.de MBS-GmbH
| |/| | _ _
\ Phone : +49-2151-7294-38 Karsten Hoffmann
| | | | |
) |__) | Fax : +49-2151-7294-50 Roemerstrasse 15
|| ||// Mobile: +49-172-3812373 D-47809 Krefeld