EDIT: Guido made the edit for me on the accepted answer, so this answer is no longer necessary.
The accepted answer is no longer working because of a change Muhammed made. I have tried to submit a correction, but it keeps getting rejected, so I will post it as a separate answer. My code is almost identical to his, just 1 tiny change:
import sys
import select
import tty
import termios
from curses import ascii
def isData():
return select.select([sys.stdin], [], [], 0) == ([sys.stdin], [], [])
old_settings = termios.tcgetattr(sys.stdin)
try:
tty.setcbreak(sys.stdin.fileno())
i = 0
while 1:
print i
i += 1
if isData():
c = sys.stdin.read(1)
if c == chr(ascii.ESC):
break
finally:
termios.tcsetattr(sys.stdin, termios.TCSADRAIN, old_settings)
The only difference is instead of "c == ascii.ESC" I changed it to "c == chr(ascii.ESC). I and 1 other developer both have tested and confirmed that this change is necessary and that otherwise the program will not work correctly.
The program is supposed to show bigger and bigger numbers until you hit ESC, and then exit. But without the chr() around the ascii.ESC, it will not detect your ESC keypress.