Skip to content

Instantly share code, notes, and snippets.

@Markbnj
Created August 26, 2015 17:05
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 Markbnj/6ec69512af4690072f57 to your computer and use it in GitHub Desktop.
Save Markbnj/6ec69512af4690072f57 to your computer and use it in GitHub Desktop.
Get a single key press from the terminal in python
import termios
import sys
import tty
def _getch():
fd = sys.stdin.fileno()
old_settings = termios.tcgetattr(fd)
try:
tty.setraw(fd)
ch = sys.stdin.read(1)
finally:
termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
return ch
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment