Skip site navigation (1)Skip section navigation (2)
Date:      Sun, 16 Sep 2007 02:37:30 -0500 (CDT)
From:      Lars Eighner <luvbeastie@larseighner.com>
To:        freebsd-questions@freebsd.org
Subject:   Solved: Re: What to use for conio?
Message-ID:  <20070916023155.B60299@qroenaqrq.6qbyyneqvnyhc.pbz>
In-Reply-To: <20070915120057.GA16@saltmine.radix.net>
References:  <20070915025950.T53308@qroenaqrq.6qbyyneqvnyhc.pbz> <20070915120057.GA16@saltmine.radix.net>

next in thread | previous in thread | raw e-mail | index | archive | help

[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




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20070916023155.B60299>