Skip to content

Instantly share code, notes, and snippets.

@kragniz
Created July 28, 2014 14:51
Show Gist options
  • Save kragniz/310f6754bc43855863d0 to your computer and use it in GitHub Desktop.
Save kragniz/310f6754bc43855863d0 to your computer and use it in GitHub Desktop.
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