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


Groups > comp.lang.python > #11791

Re: Wait for a keypress before continuing?

From Nobody <nobody@nowhere.com>
Subject Re: Wait for a keypress before continuing?
Date 2011-08-18 18:58 +0100
Message-Id <pan.2011.08.18.17.58.40.391000@nowhere.com>
Newsgroups comp.lang.python
References <4e3f2827$0$5826$c3e8da3$12bcf670@news.astraweb.com> <2670f4ee-0ab4-42ba-a472-42a4c7669ce8@p5g2000vbl.googlegroups.com> <4e4bcd37$0$23918$e4fe514c@news2.news.xs4all.nl> <4b383bcd-c58c-4e67-895a-d70fdf82f9c0@n35g2000yqf.googlegroups.com>
Organization Zen Internet

Show all headers | View raw


On Thu, 18 Aug 2011 01:24:30 -0700, peter wrote:

> This is very similar to my solution, which was to use stty turn off
> keyboard echo, then repeatedly read sys.stdin.read(1) until a unique
> keystroke had been defined. For example, the 'Insert' key seems to
> return a sequence of four codes, namely 27, 91, 50, 126.  It works but
> has two disadvantages which I have managed to live with:-
> 
> 1. As character 27 is used to signal the start of a 'special' key
> sequence, it cannot detect a single press of the Esc key. The
> workaround is to detect a double press instead.
> 
> 2. Some keys seem to return different sets of codes depending on the
> circumstances.  For example, F1 returns 27,91,91,65 from the command
> line, and 27,79,80 from a GUI window. I suspect there may be
> variations between flavours of Linux too. The solution is to detect
> all possibilities - so far I haven't found any overlaps.

Escape sequences vary between terminals. A given key may have different
escape sequences on the Linux console, xterm, a vt220, etc. The
termcap and terminfo databases exist for this purpose (terminfo is newer).

Some escape sequences vary by "mode". E.g. the cursor keys typically
produce different sequences depending upon whether the terminal is in
"keypad" mode.

The normal solution for distinguishing the escape key from an escape
sequence is a timeout. Unfortunately, it needs to be quite long in order
to reliably support network (ssh, telnet, etc) connections.

> Not pretty, but as I said it works. I know now not to spend time
> looking further. Whilst there may be a historical reason for the
> current situation, it would be a great convenience for amateur coders
> like mayself if the gurus could devise a platform independent version
> of mscvrt.

The platform-independent high-level terminal interface is called "curses".

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


Thread

Wait for a keypress before continuing? John Doe <jdoe@usenetlove.invalid> - 2011-08-08 00:04 +0000
  Re: Wait for a keypress before continuing? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-08-08 10:27 +1000
    Re: Wait for a keypress before continuing? John Doe <jdoe@usenetlove.invalid> - 2011-08-08 02:44 +0000
      Re: Wait for a keypress before continuing? Gelonida N <gelonida@gmail.com> - 2011-08-08 09:41 +0200
  Re: Wait for a keypress before continuing? John Doe <jdoe@usenetlove.invalid> - 2011-08-16 18:42 +0000
    Re: Wait for a keypress before continuing? John Doe <jdoe@usenetlove.invalid> - 2011-08-16 18:48 +0000
  Re: Wait for a keypress before continuing? John Doe <jdoe@usenetlove.invalid> - 2011-08-16 20:16 +0000
    Re: Wait for a keypress before continuing? Tim Roberts <timr@probo.com> - 2011-08-16 14:50 -0700
      Re: Wait for a keypress before continuing? John Doe <jdoe@usenetlove.invalid> - 2011-08-16 21:59 +0000
        Re: Wait for a keypress before continuing? Jerry Hill <malaclypse2@gmail.com> - 2011-08-16 20:11 -0400
          Re: Wait for a keypress before continuing? John Doe <jdoe@usenetlove.invalid> - 2011-08-17 03:52 +0000
            Re: Wait for a keypress before continuing? Seebs <usenet-nospam@seebs.net> - 2011-08-17 05:06 +0000
              Re: Wait for a keypress before continuing? John Doe <jdoe@usenetlove.invalid> - 2011-08-17 05:49 +0000
                Re: Wait for a keypress before continuing? Seebs <usenet-nospam@seebs.net> - 2011-08-17 06:12 +0000
                Re: Wait for a keypress before continuing? Chris Angelico <rosuav@gmail.com> - 2011-08-17 08:19 +0100
                Re: Wait for a keypress before continuing? John Doe <jdoe@usenetlove.invalid> - 2011-08-17 07:23 +0000
                Re: Wait for a keypress before continuing? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-08-17 18:28 +1000
                Re: Wait for a keypress before continuing? Seebs <usenet-nospam@seebs.net> - 2011-08-17 16:33 +0000
                Re: Wait for a keypress before continuing? Ethan Furman <ethan@stoneleaf.us> - 2011-08-17 10:37 -0700
                Re: Wait for a keypress before continuing? Ethan Furman <ethan@stoneleaf.us> - 2011-08-17 06:21 -0700
        Re: Wait for a keypress before continuing? Tim Roberts <timr@probo.com> - 2011-08-21 19:02 -0700
  Re: Wait for a keypress before continuing? John Doe <jdoe@usenetlove.invalid> - 2011-08-16 23:46 +0000
  Re: Wait for a keypress before continuing? peter <peter.mosley@talk21.com> - 2011-08-17 01:03 -0700
    Re: Wait for a keypress before continuing? Hans Mulder <hansmu@xs4all.nl> - 2011-08-17 16:16 +0200
      Re: Wait for a keypress before continuing? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-08-18 02:36 +1000
      Re: Wait for a keypress before continuing? peter <peter.mosley@talk21.com> - 2011-08-18 01:24 -0700
        Re: Wait for a keypress before continuing? Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2011-08-18 09:50 -0700
        Re: Wait for a keypress before continuing? Nobody <nobody@nowhere.com> - 2011-08-18 18:58 +0100
    Re: Wait for a keypress before continuing? Seebs <usenet-nospam@seebs.net> - 2011-08-17 16:33 +0000
      Re: Wait for a keypress before continuing? Terry Reedy <tjreedy@udel.edu> - 2011-08-17 13:02 -0400
        Re: Wait for a keypress before continuing? Seebs <usenet-nospam@seebs.net> - 2011-08-17 17:05 +0000
        Re: Wait for a keypress before continuing? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-08-18 04:29 +1000
          Re: Wait for a keypress before continuing? Chris Angelico <rosuav@gmail.com> - 2011-08-17 23:10 +0100

csiph-web