Skip to content

Instantly share code, notes, and snippets.

@rscarvalho
Created September 28, 2013 17:26
Show Gist options
  • Select an option

  • Save rscarvalho/6744351 to your computer and use it in GitHub Desktop.

Select an option

Save rscarvalho/6744351 to your computer and use it in GitHub Desktop.
def getch():
is_windows = True
try:
import msvcrt
def g_windows():
return msvcrt.getch()
impl = g_windows
except ImportError:
is_windows = False
def g_unix():
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
impl = g_unix
return impl()
from getch import getch
# Supposing 'q' will end the program
ch = None
STOP = 'q'
while True:
ch = getch()
if ch.lower() == STOP:
break
# Execute your logic here...
print ch
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment