Created
July 15, 2015 19:24
-
-
Save joepreludian/62ba2201df222941b2fd to your computer and use it in GitHub Desktop.
Coletando chars sob demanda em Python
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
| 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() | |
| if __name__ == '__main__': | |
| print 'Hello, sir! Try type something ;) - Write ";" ends our conversation. ;)' | |
| may_i_continue = True | |
| string_query = '' | |
| while may_i_continue: | |
| char_data = getch() | |
| string_query += char_data | |
| print "String So far: %s" % string_query | |
| may_i_continue = False if char_data == ';' else True | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment