# File: curses2.py # # In this example, the while loop # continues to execute even if the # user does not press any keys on the # keyboard. # import curses # Import the time module for the sleep() function. import time 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) # don't wait for keypress stdscr.nodelay(True) done = False while not done: # get a character c = stdscr.getch() if c == curses.KEY_LEFT : print("left\r") 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") # wait for 0.2 seconds time.sleep(.2) # end of while not done # restore things to normal stdscr.nodelay(False) stdscr.keypad(0) curses.nocbreak() curses.echo() curses.endwin()