Skip to content

Instantly share code, notes, and snippets.

@pandada8
Last active October 4, 2016 11:11
Show Gist options
  • Save pandada8/cc7563a61e08403e066312bcd8447e43 to your computer and use it in GitHub Desktop.
Save pandada8/cc7563a61e08403e066312bcd8447e43 to your computer and use it in GitHub Desktop.
getch in terminal
import sys, tty, termios
def getch():
fd = sys.stdin.fileno()
old_settings = termios.tcgetattr(fd)
try:
tty.setraw(sys.stdin.fileno())
ch = sys.stdin.read(1)
if ch == '\x1b':
ch += sys.stdin.read(2)
# todo: remove magic number
finally:
termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
return ch
while True:
a = getch()
print(repr(a))
if a == "\x04":
print("Quiting.....")
break
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment