exist loop by pressing esc

Wildman best_lay at yahoo.com
Mon Oct 24 00:18:50 EDT 2016


On Sun, 23 Oct 2016 14:34:29 -0700, chris alindi wrote:

> simple while loop range(10) if user press esc exits loop

If I understand you correctly you want to exit a while loop
with the ESC key.  That can be done but it depends on the
platform.  For Windows use this: (not tested)

import msvcrt

while something:
    if msvcrt.kbhit() and msvcrt.getch() == chr(27):
        break


In Linux you need to install getch:

https://pypi.python.org/pypi/getch

Download it here:

https://pypi.python.org/pypi/getch#downloads

Extract the tar.gz file.  A directory called getch-1.0
will be created.  Open a terminal in that directory and
enter this...

sudo python setup.py install

Here is a code example: (tested)

import getch

while something:
    if getch.getch() == '\x1b':
        break

-- 
<Wildman> GNU/Linux user #557453
May the Source be with you.



More information about the Python-list mailing list