Skip to content

Instantly share code, notes, and snippets.

@glaslos
Created April 11, 2016 11:11
Show Gist options
  • Save glaslos/600a112c4db8b2560c6029169f96a05c to your computer and use it in GitHub Desktop.
Save glaslos/600a112c4db8b2560c6029169f96a05c to your computer and use it in GitHub Desktop.
import sys
import tty
import termios
fd = sys.stdin.fileno()
# keep original terminal settings
old_settings = termios.tcgetattr(fd)
def readone():
try:
# set terminal to raw
tty.setraw(sys.stdin.fileno())
ch = sys.stdin.read(1)
except Exception as e:
print(e)
finally:
# resetting terminal for printing
termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
return ch
while True:
ch = readone()
chk = ord(ch)
if chk in [3, 4]:
# ctrl+c and ctrl+d
break
print(repr(ch))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment