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


Groups > comp.lang.python > #98296

Re: raw_input and break

From Peter Otten <__peter__@web.de>
Newsgroups comp.lang.python
Subject Re: raw_input and break
Date 2015-11-05 12:38 +0100
Organization None
Message-ID <mailman.51.1446723514.16136.python-list@python.org> (permalink)
References <mailman.50.1446717724.16136.python-list@python.org> <rmG_x.48746$kZ5.30605@fe42.am1>

Show all headers | View raw


input/ldompeling@casema.nl wrote:

>  >   choices = raw_input("letter s to stop:")
> 
> Oh no, this is not what I want. Now it is waiting for input when its go
> further with the script. Because I have an while True: so I want that the
> script go's continue only when I press a key then it must stop the script.

Solutions to that problem are OS-dependent. For Unix terminals

https://docs.python.org/2/faq/library.html#how-do-i-get-a-single-keypress-at-a-time

shows one way which I used to write the contextmanager below:

$ cat capture_key.py
import termios, fcntl, sys, os

from contextlib import contextmanager


@contextmanager
def nonblocking(stdin=None):
    if stdin is None:
        stdin = sys.stdin

    fd = sys.stdin.fileno()

    oldterm = termios.tcgetattr(fd)
    newattr = termios.tcgetattr(fd)
    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)
    def read(n):
        try:
            return stdin.read(n)
        except IOError:
            return ""
    try:
        yield read
    finally:
        termios.tcsetattr(fd, termios.TCSAFLUSH, oldterm)
        fcntl.fcntl(fd, fcntl.F_SETFL, oldflags)


Put the capture_key.py module into the same folder as your script and use it 
like in the demo below:

from capture_key import nonblocking

# put your code here

with nonblocking() as read:
    print("letter s to stop:")
    while True:
        enable_servo()
        servo(90)
        mindist = 80
        choices = read(1)
        if choices == 's':
            print("stop")
            break
if mindist > us_dist(15):
    bwd()
    print("backward 1x")

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


Thread

raw_input and break input/ldompeling@casema.nl - 2015-11-04 21:44 +0000
  Re: raw_input and break Ian Kelly <ian.g.kelly@gmail.com> - 2015-11-04 14:54 -0700
  Re: raw_input and break Joel Goldstick <joel.goldstick@gmail.com> - 2015-11-04 16:56 -0500
    Re: raw_input and break input/ldompeling@casema.nl - 2015-11-04 22:37 +0000
      Re: raw_input and break Steven D'Aprano <steve@pearwood.info> - 2015-11-05 11:44 +1100
  Re: raw_input and break tian.su.yale@gmail.com - 2015-11-04 21:39 -0800
    Re: raw_input and break input/ldompeling@casema.nl - 2015-11-05 09:22 +0000
      Re: raw_input and break Peter Otten <__peter__@web.de> - 2015-11-05 11:01 +0100
        Re: raw_input and break input/ldompeling@casema.nl - 2015-11-05 10:40 +0000
          Re: raw_input and break Peter Otten <__peter__@web.de> - 2015-11-05 12:38 +0100
            Re: raw_input and break input/ldompeling@casema.nl - 2015-11-05 12:59 +0000
              Re: raw_input and break Peter Otten <__peter__@web.de> - 2015-11-05 15:16 +0100
                Re: raw_input and break input/ldompeling@casema.nl - 2015-11-05 14:34 +0000
                Re: raw_input and break Peter Otten <__peter__@web.de> - 2015-11-05 18:06 +0100
                Re: raw_input and break input/ldompeling@casema.nl - 2015-11-05 17:28 +0000
                Re: raw_input and break Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2015-11-05 20:09 -0500
                Re: raw_input and break input/ldompeling@casema.nl - 2015-11-06 05:08 +0000
                Re: raw_input and break input/ldompeling@casema.nl - 2015-11-06 11:50 +0000
                Re: raw_input and break Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2015-11-06 08:25 -0500

csiph-web