Goto XY

Chris F.A. Johnson cfajohnson at
Wed Nov 9 04:33:47 CET 2005

On 2005-11-09, ale.of.ginger at wrote:
> Is there some command in python so that I can read a key's input and
> then use a gotoxy() function to move the cursor on screen?  e.g.:
> (psuedo-code)

    You can use curses, but that may be more trouble than it's worth.

    If you don't mind limiting your program to an ANSI-type terminals
    (vt100, xterm, rxvt, linux, putty, etc....), then you can just use
    the codes to position the cursor:

ESC = '\033'
CSI = ESC + "["

def printat(row,col,arg=""):
    sys.stdout.write( CSI + str(row) + ";" + str(col) + 'H' + str(arg))

> When the right arrow is pushed, cursor gotoxy(x+1,y)

     To read a single keystroke, see Claudio Grondi's post in the
     thread "python without OO" from last January.

     Function and cursor keys return more than a single character, so
     more work is required to decode them. The principle is outlined in
     the code there is for the shell, but translating them to python
     should be straightforward. I'll probably do it myself when I have
     the time or the motivation.

  Chris F.A. Johnson                   | Author:
  <>          |      Shell Scripting Recipes:
  Any code in this post is released    |  A Problem-Solution Approach,
  under the GNU General Public Licence |                 2005, Apress

More information about the Python-list mailing list