Skip to content

Instantly share code, notes, and snippets.

@mscalora
Created June 20, 2016 11:40
Show Gist options
  • Save mscalora/470949d173d79f0bb01212f64ce62d95 to your computer and use it in GitHub Desktop.
Save mscalora/470949d173d79f0bb01212f64ce62d95 to your computer and use it in GitHub Desktop.
Gets a single character from standard input. Does not echo to the screen
class _Getch:
"""Gets a single character from standard input. Does not echo to the screen."""
def __init__(self):
try:
self.impl = _GetchWindows()
except ImportError:
self.impl = _GetchUnix()
def __call__(self): return self.impl()
class _GetchUnix:
def __init__(self):
import tty, sys
def __call__(self):
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
class _GetchWindows:
def __init__(self):
import msvcrt
def __call__(self):
import msvcrt
return msvcrt.getch()
getch = _Getch()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment