# File: curses1.py # # A sample program to demonstrate use of # the curses module. # # See documentation in: # https://docs.python.org/3.4/howto/curses.html import curses print("Press arrow keys, or 'q' to quit.") input("Press enter to continue.") # start curses stdscr = curses.initscr() # don't print to screen curses.noecho() # don't wait for 'enter' to be pressed curses.cbreak() # map special keys to curses constants stdscr.keypad(1) # infinite loop until Boolean flag # done is set to True. done = False while not done: # get a character c = stdscr.getch() # check direction keys or 'q' for quit if c == curses.KEY_LEFT : print("left\r") # print \r for carriage return elif c == curses.KEY_RIGHT : print("right\r") elif c == curses.KEY_UP : print("up\r") elif c == curses.KEY_DOWN : print("down\r") elif c == ord('q'): done = True else: print(".\r") # end of while not done # restore things to normal stdscr.keypad(0) curses.nocbreak() curses.echo() curses.endwin()