-
-
Save kragniz/310f6754bc43855863d0 to your computer and use it in GitHub Desktop.
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 read_char(): | |
''' | |
Read a single character from a unix terminal and immediately return it. | |
''' | |
# grab a file descriptor | |
fd = sys.stdin.fileno() | |
old_settings = termios.tcgetattr(fd) | |
try: | |
# get naughty and set that terminal into raw mode | |
tty.setraw(sys.stdin.fileno()) | |
# get acquainted with a lonesome character | |
ch = sys.stdin.read(1) | |
finally: | |
# pretend we were never here | |
termios.tcsetattr(fd, termios.TCSADRAIN, old_settings) | |
return ch | |
if __name__ == '__main__': | |
while True: | |
# read in dat char | |
c = read_char() | |
# WOOP WOOP EMERGENCY ESCAPE | |
if c == 'q': | |
# make a fast getaway | |
sys.exit() | |
else: | |
# tell it like it is | |
print 'you entered:', c |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment