"""

cross platform, non blocking way of reading a key

https://stackoverflow.com/questions/510357/how-to-read-a-single-character-from-the-user

hmmm also does not work on MacOS....

"""
import sys,tty,os,termios
def getkey():
    old_settings = termios.tcgetattr(sys.stdin)
    tty.setcbreak(sys.stdin.fileno())
    try:
        while True:
            b = os.read(sys.stdin.fileno(), 3).decode()
            if len(b) == 3:
                k = ord(b[2])
            else:
                k = ord(b)
            key_mapping = {
                127: 'backspace',
                10: 'return',
                32: 'space',
                9: 'tab',
                27: 'esc',
                65: 'up',
                66: 'down',
                67: 'right',
                68: 'left'
            }
            return key_mapping.get(k, chr(k))
    finally:
        termios.tcsetattr(sys.stdin, termios.TCSADRAIN, old_settings)


if __name__ == "__main__":
    try:
        while True:
            k = getkey()
            if k == 'esc':
                quit()
            else:
                print(k)
    except (KeyboardInterrupt, SystemExit):
        os.system('stty sane')
        print('stopping.')