Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #92626

Re: Keypress Input

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 <oscar.j.benjamin@gmail.com>
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 <Ojtfx.178941$Jv.137884@fx26.iad>
References <rBHbx.75089$MO7.40532@fx10.iad> <Ojtfx.178941$Jv.137884@fx26.iad>
From Oscar Benjamin <oscar.j.benjamin@gmail.com>
Date Mon, 15 Jun 2015 12:22:13 +0100
Subject Re: Keypress Input
To Python List <python-list@python.org>
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 <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.481.1434367364.13271.python-list@python.org> (permalink)
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

Show key headers only | View raw


On 15 June 2015 at 06:23, John McKenzie <davros@bellaliant.net> 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

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Keypress Input John McKenzie <davros@bellaliant.net> - 2015-06-03 18:22 +0000
  Re: Keypress Input Laura Creighton <lac@openend.se> - 2015-06-03 20:59 +0200
  Re: Keypress Input Gary Herron <gherron@digipen.edu> - 2015-06-03 12:15 -0700
  Re: Keypress Input Gary Herron <gherron@digipen.edu> - 2015-06-03 11:47 -0700
  Re: Keypress Input Laura Creighton <lac@openend.se> - 2015-06-04 12:50 +0200
  Re: Keypress Input John McKenzie <davros@bellaliant.net> - 2015-06-06 18:28 +0000
    Re: Keypress Input Laura Creighton <lac@openend.se> - 2015-06-06 22:52 +0200
    Re: Keypress Input Chris Angelico <rosuav@gmail.com> - 2015-06-07 07:20 +1000
    Re: Keypress Input Michael Torrie <torriem@gmail.com> - 2015-06-06 22:31 -0600
  Re: Keypress Input John McKenzie <davros@bellaliant.net> - 2015-06-15 05:15 +0000
    Re: Keypress Input John McKenzie <davros@bellaliant.net> - 2015-06-16 15:09 +0000
    Re: Keypress Input Christian Gollwitzer <auriocus@gmx.de> - 2015-06-19 07:20 +0200
      Re: Keypress Input John McKenzie <davros@bellaliant.net> - 2015-06-20 14:59 +0000
      Re: Keypress Input Rick Johnson <rantingrickjohnson@gmail.com> - 2015-07-15 18:03 -0700
        Re: Keypress Input Michael Torrie <torriem@gmail.com> - 2015-07-15 22:30 -0600
          Re: Keypress Input Rick Johnson <rantingrickjohnson@gmail.com> - 2015-07-16 10:22 -0700
            Re: Keypress Input Michael Torrie <torriem@gmail.com> - 2015-07-16 15:27 -0600
        Re: Keypress Input Terry Reedy <tjreedy@udel.edu> - 2015-07-16 02:08 -0400
          Re: Keypress Input Rick Johnson <rantingrickjohnson@gmail.com> - 2015-07-16 11:30 -0700
        Re: Keypress Input Terry Reedy <tjreedy@udel.edu> - 2015-07-16 03:10 -0400
        Re: Keypress Input Michael Torrie <torriem@gmail.com> - 2015-07-16 15:29 -0600
  Re: Keypress Input John McKenzie <davros@bellaliant.net> - 2015-06-15 05:23 +0000
    Re: Keypress Input Oscar Benjamin <oscar.j.benjamin@gmail.com> - 2015-06-15 12:22 +0100
      Re: Keypress Input Grant Edwards <invalid@invalid.invalid> - 2015-06-15 15:22 +0000
    Re: Keypress Input Michael Torrie <torriem@gmail.com> - 2015-06-16 11:15 -0600
      Re: Keypress Input John McKenzie <davros@bellaliant.net> - 2015-06-16 20:06 +0000
        Re: Keypress Input Grant Edwards <invalid@invalid.invalid> - 2015-06-16 20:49 +0000
          Re: Keypress Input Michael Torrie <torriem@gmail.com> - 2015-06-16 19:22 -0600
          Re: Keypress Input Michael Torrie <torriem@gmail.com> - 2015-06-18 16:42 -0600
      Re: Keypress Input John McKenzie <davros@bellaliant.net> - 2015-06-20 15:02 +0000
        Re: Keypress Input Michael Torrie <torriem@gmail.com> - 2015-06-20 10:30 -0600
  Re: Keypress Input Paul Rubin <no.email@nospam.invalid> - 2015-06-16 14:22 -0700
  Re: Keypress Input John McKenzie <davros@bellaliant.net> - 2015-07-15 19:05 +0000
    Re: Keypress Input Michael Torrie <torriem@gmail.com> - 2015-07-15 13:17 -0600
      Re: Keypress Input John McKenzie <davros@bellaliant.net> - 2015-08-16 19:45 +0000

csiph-web