Skip to content

Instantly share code, notes, and snippets.

@swdunlop
Created July 19, 2010 23:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save swdunlop/482200 to your computer and use it in GitHub Desktop.
Save swdunlop/482200 to your computer and use it in GitHub Desktop.
def getch_unix( ):
old_settings = termios.tcgetattr( sys.stdin )
try:
tty.setraw( sys.stdin )
ch = sys.stdin.read( 1 )
finally:
termios.tcsetattr( sys.stdin, termios.TCSADRAIN, old_settings)
return ch
try:
import tty, sys, termios
getch = getch_unix
except ImportError:
import msvcrt
getch = getch_windows
def getch_win32( ):
return msvcrt.getch( )
@swdunlop
Copy link
Author

Just a brutally simple way to capture a terminal keypress in Python. Derived from:

http://code.activestate.com/recipes/134892-getch-like-unbuffered-character-reading-from-stdin/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment