From owner-freebsd-questions@FreeBSD.ORG Sun Sep 16 07:37:50 2007 Return-Path: Delivered-To: freebsd-questions@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id BF3FC16A417 for ; Sun, 16 Sep 2007 07:37:50 +0000 (UTC) (envelope-from lars@larseighner.com) Received: from mail.team1internet.com (mail.team1internet.com [216.110.13.10]) by mx1.freebsd.org (Postfix) with ESMTP id 88ED113C461 for ; Sun, 16 Sep 2007 07:37:50 +0000 (UTC) (envelope-from lars@larseighner.com) Received: by mail.team1internet.com (Postfix, from userid 12346) id 10F7B16B540; Sun, 16 Sep 2007 02:37:50 -0500 (CDT) Received: from larseighner.com (unknown [216.110.13.71]) by mail.team1internet.com (Postfix) with SMTP id 38A3F16B53F for ; Sun, 16 Sep 2007 02:37:48 -0500 (CDT) Received: by larseighner.com (nbSMTP-1.00) for uid 1001 lars@larseighner.com; Sun, 16 Sep 2007 02:37:31 -0500 (CDT) Date: Sun, 16 Sep 2007 02:37:30 -0500 (CDT) From: Lars Eighner X-X-Sender: lars@debranded.6dollardialup.com To: freebsd-questions@freebsd.org In-Reply-To: <20070915120057.GA16@saltmine.radix.net> Message-ID: <20070916023155.B60299@qroenaqrq.6qbyyneqvnyhc.pbz> References: <20070915025950.T53308@qroenaqrq.6qbyyneqvnyhc.pbz> <20070915120057.GA16@saltmine.radix.net> MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII; format=flowed X-Sanitizer: Anomy and SpamAssassin mail filter - see http://www.6dollardialup.com/support/spaminfo.html X-Spam-Status: No, hits=-0.8 required=10.0 tests=IN_REP_TO,J_CHICKENPOX_43,OACYS_SINGLE,REFERENCES, SIGNATURE_SHORT_DENSE,TW_VT version=2.43 X-Spam-Level: Subject: Solved: Re: What to use for conio? X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sun, 16 Sep 2007 07:37:50 -0000 [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 #include #include #include #include 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