Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.mixmin.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'finally:': 0.05; 'correct.': 0.07; 'exit': 0.07; 'puts': 0.07; 'returned.': 0.07; 'sys,': 0.07; 'key)': 0.09; 'pressing': 0.09; 'restored': 0.09; 'def': 0.14; 'explicitly': 0.15; 'weird': 0.15; '(key,': 0.16; 'curses': 0.16; 'finishes': 0.16; 'michael,': 0.16; 'mode)': 0.16; 'pressed': 0.16; 'to:name:python list': 0.16; 'true:': 0.16; 'tty': 0.16; 'wrote:': 0.16; 'typing': 0.18; 'fix': 0.21; 'keys': 0.22; 'try:': 0.22; 'posted': 0.23; '2015': 0.23; 'module': 0.23; 'originally': 0.23; 'import': 0.24; 'header:In-Reply-To:1': 0.24; '(most': 0.24; 'linux': 0.26; 'raw': 0.27; 'message- id:@mail.gmail.com': 0.28; "i'm": 0.29; 'looks': 0.29; 'mode.': 0.29; 'character': 0.29; 'code:': 0.29; 'function': 0.30; 'mode': 0.31; 'code': 0.31; "can't": 0.32; 'usually': 0.33; 'impression': 0.33; 'joined': 0.33; 'previous': 0.34; 'received:google.com': 0.34; 'to:addr:python-list': 0.35; 'something': 0.35; 'but': 0.36; 'being': 0.36; 'subject:: ': 0.37; 'doing': 0.38; 'mean': 0.38; 'thank': 0.39; 'goes': 0.39; 'to:addr:python.org': 0.39; 'sure': 0.40; 'where': 0.40; 'skip:t 20': 0.40; 'your': 0.60; 'john': 0.61; 'implications': 0.84; 'oscar': 0.84; 'post,': 0.84; 'replies.': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type; bh=wougbgBB/yDVdPa5vlFCQeRzM7JpFEvFPIx5qyohClk=; b=A9Zwq6h2wLWxuE0GA1GJM01aT6ALz/KSbSzLKEbhKi/jWiSLAUqjXZHaYultkYspo+ dvHL+HoTQesCj/tZFoeIHwNRH1RUv0M7g3Vg35W2w6BjFydi8R7khdiYU+rly22Qa44E VUz4mVy6z8r/GFavK/A3zs/694e1CyRPLq1BdpnAr5I57ZuLXikbKYPVEkGWGNosC8RM 1oPtDUF7GGkWVTOogmwptXgf+jQcsnOMjZ5LfuTW6pOeR72m/N/hw/os55LSM8IiRlO4 7g9WYlFnF/3z7MBFJzTwEq9fVqD/rVyvrhe/OC3eCLpO7VEAylcj4JLyeaEpQ1YbMCe1 9/pw== X-Received: by 10.194.184.174 with SMTP id ev14mr49793497wjc.95.1434367354212; Mon, 15 Jun 2015 04:22:34 -0700 (PDT) MIME-Version: 1.0 In-Reply-To: References: From: Oscar Benjamin Date: Mon, 15 Jun 2015 12:22:13 +0100 Subject: Re: Keypress Input To: Python List Content-Type: text/plain; charset=UTF-8 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 56 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1434367364 news.xs4all.nl 2827 [2001:888:2000:d::a6]:38321 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:92626 On 15 June 2015 at 06:23, John McKenzie wrote: > > Thank to the others who joined in and posted replies. > > Michael, your assumption is correct. To quote my original post, "and I > want this working on a Raspberry Pi." Doing a superficial look at curses > and getch it looks excessively complicated. I was under the impression it > was not multi-platform and Linux was excluded. Searching for getch and > raspberry pi on the web I see it is not and is available for Raspian. I'm not sure what you mean but you can write a getch function for Unixy systems using the tty module (I can't remember where I originally borrowed this code from): import sys, tty, termios def getch(): fd = sys.stdin.fileno() oldsettings = termios.tcgetattr(fd) try: tty.setraw(sys.stdin.fileno()) c = sys.stdin.read(1) finally: termios.tcsetattr(fd, termios.TCSADRAIN, oldsettings) return c while True: key = getch() print("key: '%c' code: '%d'" % (key, ord(key))) if key == 'q': break This puts the terminal in "raw mode" and waits for a key-press. Once a key is pressed the terminal is restored to it's previous mode (most likely not raw mode) and the character is returned. This is important because once your program finishes you don't want the terminal to be in raw mode. If the terminal goes weird you can usually fix it by typing "reset" and pressing enter. Note that going into raw mode has other implications such as not being able to exit your program with ctrl-c or suspend with ctrl-z etc. You can explicitly process those kinds of contrl keys with something like: while True: key = getch() if 1 <= ord(key) <= 26: ctrl_key = chr(ord(key) + 64) print("ctrl-%c" % ctrl_key) if ctrl_key == 'C': break else: print("key: '%c'" % key) -- Oscar