Skip to content

Instantly share code, notes, and snippets.

@zhasm
Created March 18, 2014 06:15
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 zhasm/9614482 to your computer and use it in GitHub Desktop.
Save zhasm/9614482 to your computer and use it in GitHub Desktop.
get keypress without enter
def getkey():
"get key press without Enter"
import termios, sys, os
fd = sys.stdin.fileno()
old = termios.tcgetattr(fd)
new = termios.tcgetattr(fd)
new[3] = new[3] & ~TERMIOS.ICANON & ~TERMIOS.ECHO
new[6][TERMIOS.VMIN] = 1
new[6][TERMIOS.VTIME] = 0
termios.tcsetattr(fd, TERMIOS.TCSANOW, new)
c = None
try:
c = os.read(fd, 1)
finally:
termios.tcsetattr(fd, TERMIOS.TCSAFLUSH, old)
return c
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment