Last active
October 4, 2016 11:11
-
-
Save pandada8/cc7563a61e08403e066312bcd8447e43 to your computer and use it in GitHub Desktop.
getch in terminal
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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