Entry
getch()
Jul 5th, 2000 10:03
Nathan Wallace, Hans Nowak, Snippet 373, Jeff
"""
Packages: operating_systems.unix;operating_systems.dos
"""
"""
greetings,
I've seen the attached piece of code in several places. I've used it so much
I don't remember who originally wrote it (though I believe it was spawned by
a post to c.l.p, so maybe you can find it on deja news).. it's a small
'getch' routine that comes in really handy for tty based apps.
would you be willing to consider adding this code to the archive? note that
the RCSID has my initials because it's in my cvs repository for a current
project, not because I want to take credit for someone else's code ;)
regards,
J
"""
import os
import sys
import tty
_RCSID = "$Id: getch.py,v 1.2 2021/02/02 00:36:12 jam Exp $"
def getch():
fd = sys.stdin.fileno()
tty_mode = tty.tcgetattr(fd)
tty.setcbreak(fd)
try:
ch = os.read(fd, 1)
finally:
tty.tcsetattr(fd, tty.TCSAFLUSH, tty_mode)
return ch
# added by PSST
if __name__ == "__main__":
print 'Press a key...',
sys.stdout.flush()
print getch()