Skip to content

Instantly share code, notes, and snippets.

@antfu
Created November 10, 2016 13:03
Show Gist options
  • Save antfu/5ad91b18dbbaced34e184867b8d28d11 to your computer and use it in GitHub Desktop.
Save antfu/5ad91b18dbbaced34e184867b8d28d11 to your computer and use it in GitHub Desktop.
# Portable "getch"
try:
from msvcrt import getch
except ImportError:
def getch():
import sys, tty, termios
fd = sys.stdin.fileno()
old_settings = termios.tcgetattr(fd)
try:
tty.setraw(sys.stdin.fileno())
ch = sys.stdin.read(1)
finally:
termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
return ch
def getunich():
code = 0
while True:
chcode = ord(getch())
code *= 265
code += chcode
if chcode != 224:
return code
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment