Created
September 28, 2013 17:26
-
-
Save rscarvalho/6744351 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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