Solved: Re: What to use for conio?

Lars Eighner luvbeastie at larseighner.com
Sun Sep 16 00:37:50 PDT 2007


[Also posted the news group]

The problem was to capture key presses from a vtty (including such function
keys as your keymap will allow) so they do not echo - as you might want to
do in developing a full-screen text-mode interface or a simple console-type
game.

Here is my solution in demo form,  It turns out this highly system
dependent.  Any real application would probably need to do some signal
handling, and for some things (such as entering text in a box) translation
to a more human readable form would be desirable.  This seems to work or
seems to be subject to being made to work for the printable and control
characters in an Xterm, but is hopeless with function keys in X.  I have
not, and probably will not further investigate that because my project deals
with text-mode terminals.  With -lc, this compiles without warnings in
-Wall -pedantic.  However, it uses %p format in printf which seems to be
undocumented in the man.

I have no idea what I am doing.

#include <stdio.h>
#include <termios.h>
#include <sys/types.h>
#include <sys/uio.h>
#include <unistd.h>

char *k;
int getkey (void);
ssize_t
read(int d, void *buf, size_t nbytes);

int main (void)
{
int knum;

knum = getkey();
printf ("knum is %i \n",knum);
printf ("value is %p \n",k);
return 0;
}

int getkey (void)
{
struct termios unwhacked, whacked;
int knoc;
fflush(0);
tcgetattr(0,&unwhacked);           /* get terminal state */
whacked = unwhacked;               /* save state for restore */
whacked.c_lflag &= ~ICANON;        /* turn off cannical input */ 
whacked.c_lflag &= ~ECHO;          /* turn off echoing */ 
whacked.c_cc[VMIN] = 1;            /* capture at least 1 character */
whacked.c_cc[VTIME] = 1;           /* and of them that come quick */
tcsetattr(0,TCSANOW,&whacked);     /* whack the terminal with new flags now */
knoc = read (0,&k,6);
tcsetattr(0,TCSANOW,&unwhacked);   /* unwhack the terminal */
return knoc;
}

-- 
Lars Eighner
http://www.larseighner.com/index.html
8800 N IH35 APT 1191 AUSTIN TX 78753-5266



More information about the freebsd-questions mailing list