Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #98304
| Path | csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail |
|---|---|
| From | Peter Otten <__peter__@web.de> |
| Newsgroups | comp.lang.python |
| Subject | Re: raw_input and break |
| Date | Thu, 05 Nov 2015 15:16:23 +0100 |
| Organization | None |
| Lines | 58 |
| Message-ID | <mailman.54.1446733002.16136.python-list@python.org> (permalink) |
| References | <mailman.51.1446723514.16136.python-list@python.org> <0pI_x.14172$aq1.6942@fe58.am1> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset="ISO-8859-1" |
| Content-Transfer-Encoding | 7Bit |
| X-Trace | news.uni-berlin.de 3AqVAWdVJS6038RJNJAdjQQRDqMHKFcFi3ZYsO+b+Lsw== |
| Return-Path | <python-python-list@m.gmane.org> |
| 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; 'url:pypi': 0.03; 'resulting': 0.04; 'none:': 0.05; 'sys,': 0.07; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'python': 0.10; 'python.': 0.11; 'def': 0.13; '(there': 0.16; 'caring': 0.16; 'contextlib': 0.16; 'curses': 0.16; 'prefer.': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:io': 0.16; 'received:plane.gmane.org': 0.16; 'received:psf.io': 0.16; 'received:t-ipconnect.de': 0.16; 'recipes': 0.16; 'stdin': 0.16; 'subject:break': 0.16; 'true:': 0.16; 'url:faq': 0.16; 'wrote:': 0.16; 'library,': 0.18; 'script.': 0.18; 'input': 0.18; 'bit': 0.23; 'wrote': 0.23; 'import': 0.24; 'unix': 0.24; 'module': 0.25; 'script': 0.25; 'header:User-Agent:1': 0.26; 'header:X-Complaints- To:1': 0.26; 'connected': 0.27; 'function': 0.28; 'cat': 0.29; 'board': 0.30; 'code': 0.30; 'problem': 0.33; 'url:python': 0.33; 'choices': 0.33; 'skip:~ 10': 0.33; 'asking': 0.35; 'reply.': 0.35; 'but': 0.36; 'there': 0.36; 'url:org': 0.36; 'to:addr :python-list': 0.36; 'subject:: ': 0.37; 'thanks': 0.37; 'received:org': 0.37; 'no,': 0.38; 'to:addr:python.org': 0.40; 'received:de': 0.40; 'easy': 0.60; 'waiting': 0.60; 'press': 0.61; 'further': 0.62; 'details': 0.62; 'reply': 0.68; 'below:': 0.71; 'ready-made': 0.84; 'robot': 0.91 |
| X-Injected-Via-Gmane | http://gmane.org/ |
| X-Gmane-NNTP-Posting-Host | p57bd897d.dip0.t-ipconnect.de |
| User-Agent | KNode/4.13.3 |
| 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> |
| Xref | csiph.com comp.lang.python:98304 |
Show key headers only | View raw
input/ldompeling@casema.nl wrote:
> In reply to "Peter Otten" who wrote the following:
>
>> 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)
> Thanks for the reply. Is there no easy way to do so in python ?
If you are asking for a ready-made function in the standard library, I don't
know one and I don't think there is one (there are recipes that work on top
of curses though).
> I am using the raspberry pi with Wheezy and for the robot the GoPiGo which
> is connected on the raspberry pi. The GoPiGo board has his own code like:
> fwd()="forward" bwd()="backward" right()="right" left="left" and so on. I
> am just a dummie with python.
The code in capture_key.py may look a bit scary, but just as I took it
without bothering the details you can take the resulting module without
caring about the code in it. Alternatively you can search
https://pypi.python.org
for a solution that you prefer.
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll 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