DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Cross Platform Getch() In Python

11.26.2005
| 24594 views |
  • submit to reddit
        For Unix, it uses sys, tty, termios modules.
import sys, tty, termios
fd = sys.stdin.fileno()
old_settings = termios.tcgetattr(fd)
tty.setraw(sys.stdin.fileno())
ch = sys.stdin.read(1)
For Windows, it uses msvcrt module.
import msvcrt
ch = msvcrt.getch()
See more details and OSX code in this <a href=http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/134892>recipe</a> by Danny Yoo.