"""Test using keyboard for input."""
import sys
import tty
import termios

def main():
    print('hello world -- press some keys, esc to quit')
    while 1:
        k = sys.stdin.read(1)
        print('you pressed %d 0x%x' % (ord(k), ord(k)))
        if ord(k) == 0x1b:
            break
    print('goodbye world')

# Need to set stdin to cbreak mode to get characters one-by-one.
# Must be sure to reset it because exiting the program will not.

orig_settings = termios.tcgetattr(sys.stdin)
try:
    tty.setcbreak(sys.stdin)
    main()
finally:
    termios.tcsetattr(sys.stdin, termios.TCSADRAIN, orig_settings)
    print('finally')

