How to accept keypress in command line python?

58,644

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()
Share:
58,644
Elmer
Author by

Elmer

I am currently a programmer/analyst. Hobbies include quadcopters, all sorts of tech, and hiking/biking/frisbee.

Updated on July 09, 2022

Comments

  • Elmer
    Elmer almost 2 years

    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 that looked something like this...

    #!/usr/bin/env python
    # control a robot using python
    exit = 0
    while exit == 0:
      keypress = ##get keypress, if no key is pressed, continue##
      if keypress == 'q':
        exit = 1
        break
      elif keypress == KEY_UP:
        ##robot move forward##
      elif keypress == KEY_DOWN:
        ##robot move backward##
    print "DONE"
    

    However the problem is that I do not know how to get the users input. And I cannot use a GUI based solution like pygame from what I have found because the robot does not use a display.

    Any help is very much appreciated!!

    • Junuxx
      Junuxx about 12 years
      Identical to this question, which has several solutions.
    • Gareth Latty
      Gareth Latty about 12 years
      You might want to look into curses.
    • Elmer
      Elmer about 12 years
      I was looking at that question, but could not figure out if it was what I was looking for or not because I am looking for a linux solution and that seemed really complicated because of the cross-platform needs. I looked at curses, but does anyone know of a good tutorial on how to use it? The best I could find was the Python Docs and they only went so far.
    • jdi
      jdi about 12 years
      There are a lot of answers to this question on SO. One of which explains that just trying to get a couple key press events makes curses a bit of overkill, and that you can simply read from stdin and interpret the keys. stackoverflow.com/a/7264312/496445
  • allyourcode
    allyourcode over 7 years
    Why is it that two letters get displayed when I hit a letter key once? It looks like getch moves cursor to the right of where addch puts it. Therefore, when I hit the next key, it gets displayed to the left of 20, 25, and then addch displays the same letter at 20, 25, leaving two characters on the screen. I guess getch does not prevent the letter to the right of 20, 25 from being displayed? How can I disable what I entered from being displayed?
  • Ian
    Ian almost 7 years
    allyourcode - add curses.noecho() (after the call to cbreak is good). Then curses won't autoprint the key, only the script will.
  • ilon
    ilon over 2 years
    Simpler solution is to use sshkeyboard. It requires less coding than curses, and it is a cross platform solution.