how to stop a loop with ESC key? [newbie]

M

mo

Can somebody explain how to stop a WHILE loop in running program by pressing
ESC key?
mo
 
F

Fredrik Lundh

mo said:
Can somebody explain how to stop a WHILE loop in running program by pressing
ESC key?

depends on the platform.

on windows, you can use the mscvrt module:

import msvcrt

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

also see the second example on this page:

http://effbot.org/tag/python.msvcrt

</F>
 
L

Leif K-Brooks

mo said:
Can somebody explain how to stop a WHILE loop in running program by pressing
ESC key?

On Unix-like systems try:

import termios, fcntl, sys, os
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)

i = 0
try:
while 1:
print i
i += 1
try:
char = sys.stdin.read(1)
if char == '\x1b':
print "Bye!"
break
except IOError:
pass
finally:
termios.tcsetattr(fd, termios.TCSAFLUSH, oldterm)
fcntl.fcntl(fd, fcntl.F_SETFL, oldflags)
 
M

mo

Thanks,
I tryed your example:

import msvcrt
while 1:
print '.'
if msvcrt.kbhit() and msvcrt.getch() == chr(27):
break

but it doesn't work. It is running (Win2000), there is no messages about
errors but there is no effect when pressing ESC key. What I am doing wrong?
mo
 
F

Fredrik Lundh

mo said:
I tryed your example:

import msvcrt
while 1:
print '.'
if msvcrt.kbhit() and msvcrt.getch() == chr(27):
break

but it doesn't work. It is running (Win2000), there is no messages about
errors but there is no effect when pressing ESC key. What I am doing wrong?

works for me, when running it from a stock CPython interpreter in a windows
console window, with focus set to that window.

what environment are you using?

</F>
 
M

Mikael Olofsson

Fredrik said:
works for me, when running it from a stock CPython interpreter in a windows
console window, with focus set to that window.

what environment are you using?

Could be IDLE. The code Fredrik proposed works well for me in the Python
console window, but not in IDLE (thats under XP). I guess that IDLE
makes use of msvcrt itself. Let's remember that the OP describes himself
as a newbie in the subject, so he might not know the difference between
those two interactive environments.

/MiO
 
M

mo

Fredrik Lundh" said:
works for me, when running it from a stock CPython interpreter in a windows
console window, with focus set to that window.
what environment are you using?

I use IDLE 1.0.3, Python 2.3.4
The same problem is when running in a win console.
mo
 
P

Peter Hansen

mo said:
I use IDLE 1.0.3, Python 2.3.4
The same problem is when running in a win console.

And do you see the "." printing in the console window?

What do you get if you change the code so you print the getch() value?

e.g.

if msvcrt.kbhit():
c = msvcrt.getch()
print 'key', ord(c)

Does that ever print anything?

-Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top