diff options
| author | jquast <contact@jeffquast.com> | 2013-09-22 17:17:46 -0700 |
|---|---|---|
| committer | jquast <contact@jeffquast.com> | 2013-09-22 17:17:46 -0700 |
| commit | 9f800f2834fbdfb638ebd5ab4429a3a95c86b799 (patch) | |
| tree | 96bc1273444d889af33aee4b9c22d66b3311ba6a /tests/getch.py | |
| parent | 6ddd51b5ca625b1bf881ef8faf827a0c462f03cd (diff) | |
| download | pexpect-9f800f2834fbdfb638ebd5ab4429a3a95c86b799.tar.gz | |
a better getch() implementation
notably, it has a sentinel exit value (NUL, ctrl+'@' or ctrl+' ')
it removes the uhh strange range(127) that wans't really used right
and it doesn't execute in the global space, but within a __main__()
Diffstat (limited to 'tests/getch.py')
| -rwxr-xr-x | tests/getch.py | 23 |
1 files changed, 11 insertions, 12 deletions
diff --git a/tests/getch.py b/tests/getch.py index 53ff2c7..feef197 100755 --- a/tests/getch.py +++ b/tests/getch.py @@ -19,21 +19,20 @@ PEXPECT LICENSE ''' import sys, tty, termios -def getch(): + +def main(): + while True: + val = ord(sys.stdin.read(1)) + sys.stdout.write('%i\r\n' % (val,)) + if val == 0: + # StopIteration equivalent is ctrl+' ' (\x00, NUL) + break + +if __name__ == '__main__': fd = sys.stdin.fileno() old_settings = termios.tcgetattr(fd) try: tty.setraw(sys.stdin.fileno()) - ch = sys.stdin.read(1) + main() finally: termios.tcsetattr(fd, termios.TCSADRAIN, old_settings) - return ch - -#for i in range(256): -# Current Python unicode support was too hard to figure out. -# This only tests the true ASCII characters now: -for i in range(126): - c = getch() - a = ord(c) # chr(a) - print a - |
