From owner-freebsd-questions@FreeBSD.ORG Sat Sep 15 08:58:49 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 2DEDA16A419 for ; Sat, 15 Sep 2007 08:58:49 +0000 (UTC) (envelope-from cpghost@cordula.ws) Received: from fw.farid-hajji.net (fw.farid-hajji.net [213.146.115.42]) by mx1.freebsd.org (Postfix) with ESMTP id AB2CB13C459 for ; Sat, 15 Sep 2007 08:58:48 +0000 (UTC) (envelope-from cpghost@cordula.ws) Received: from epia-2.farid-hajji.net (epia-2 [192.168.254.11]) by fw.farid-hajji.net (Postfix) with ESMTP id 443BFDFB07; Sat, 15 Sep 2007 11:00:25 +0200 (CEST) Date: Sat, 15 Sep 2007 11:02:18 +0200 From: cpghost To: Lars Eighner Message-ID: <20070915110218.0230f2af@epia-2.farid-hajji.net> In-Reply-To: <20070915025950.T53308@qroenaqrq.6qbyyneqvnyhc.pbz> References: <20070915025950.T53308@qroenaqrq.6qbyyneqvnyhc.pbz> Organization: Cordula's Web X-Mailer: Claws Mail 3.0.0 (GTK+ 2.10.14; i386-portbld-freebsd6.2) Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Cc: freebsd-questions@freebsd.org Subject: 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: Sat, 15 Sep 2007 08:58:49 -0000 On Sat, 15 Sep 2007 03:11:54 -0500 (CDT) Lars Eighner wrote: > What I really want to do: capture keypresses (including function > keys) from a (virtual) terminal without their echoing or without > having to enter a new line (i.e. hit return). > > Why I do not want to use (n)curses: to use keypad in ncurses, I have > to initscr() and ncurses will then blank the screen and seize the > terminal. I do not want that to happen. I want to write ANSI > directly to the terminal and get non-echoing keypresses back. Check out tcgetattr(3) and tcsetattr(3) from , Here's an entry from the Python FAQ that you can adapt or retrofit to C: How do I get a single keypress at a time? ----------------------------------------- For Unix variants: There are several solutions. It's straightforward to do this using curses, but curses is a fairly large module to learn. Here's a solution without curses: import termios, fcntl, sys, os, select fd = sys.stdin.fileno() oldterm = termios.tcgetattr(fd) newattr = oldterm[:] newattr[3] = newattr[3] & ~termios.ICANON & ~termios.ECHO termios.tcsetattr(fd, termios.TCSANOW, newattr) oldflags = fcntl.fcntl(fd, fcntl.F_GETFL) fcntl.fcntl(fd, fcntl.F_SETFL, oldflags | os.O_NONBLOCK) try: while 1: r, w, e = select.select([fd], [], []) if r: c = sys.stdin.read(1) print "Got character", repr(c) if c == "q": break # quit finally: termios.tcsetattr(fd, termios.TCSAFLUSH, oldterm) fcntl.fcntl(fd, fcntl.F_SETFL, oldflags) You need the termios and the fcntl module for any of this to work, and I've only tried it on Linux, though it should work elsewhere. In this code, characters are read and printed one at a time, until the user presses `q' to quit. termios.tcsetattr() turns off stdin's echoing and disables canonical mode. fcntl.fnctl() is used to obtain stdin's file descriptor flags and modify them for non-blocking mode. The select module is then used to wait for incoming characters. ------------ END OF FAQ ENTRY ------------------------------------ > Why I think I need something like conio: I think I could get stdio > to do what I want if I had something like kbdhit from conio, but > conio doesn't exist pretty much anywhere outside of DOS. I'm pretty > sure the system conSio is not anything like what I want. > > So how can I get non-echoing keypress without turning my terminal > over to the tender mercies of ncurses? s. above. -- Cordula's Web. http://www.cordula.ws/