how to stop a loop with ESC key? [newbie]
Leif K-Brooks
eurleif at ecritters.biz
Tue Nov 8 03:13:02 EST 2005
mo wrote:
> 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)
More information about the Python-list
mailing list