if key == 27:
    quit()
if key == 0:
    print('up')
elif key == 1:
    print('down')
elif key == 2:
    print('left')
elif key == 3:
    print('right')
elif key != 255:
    pass
Finding the Values of the Arrow Keys in Python: Why are they triples?