How do I capture just a single keystroke?

Chris Spencer clspence at one.net
Wed Sep 11 17:17:10 EDT 2002


Nevermind...I found the answer.  For those who are interested, this is it.  It
should work on both Unix and Windows...
def getch():
	try:
		import msvcrt
		return msvcrt.getch()
	except:
		import tty,sys,termios
		fd = sys.stdin.fileno()
		old_settings = termios.tcgetattr(fd)
		try:
			tty.setraw(sys.stdin.fileno())
			ch = sys.stdin.read(1)
		finally:
			termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
		return ch

Chris.



More information about the Python-list mailing list