Skip to content Skip to sidebar Skip to footer

How To Accept Keypress In Command Line Python?

Possible Duplicate: Python read a single character from the user I am looking to be able to control a robot with the arrow keys using python. And my idea was to implement code t

Solution 1:

A simple curses example. See the docs for the curses module for details.

import curses
stdscr = curses.initscr()
curses.cbreak()
stdscr.keypad(1)

stdscr.addstr(0,10,"Hit 'q' to quit")
stdscr.refresh()

key = ''
while key != ord('q'):
    key = stdscr.getch()
    stdscr.addch(20,25,key)
    stdscr.refresh()
    if key == curses.KEY_UP: 
        stdscr.addstr(2, 20, "Up")
    elif key == curses.KEY_DOWN: 
        stdscr.addstr(3, 20, "Down")

curses.endwin()

Post a Comment for "How To Accept Keypress In Command Line Python?"