Curses & Keypress

Lee Harr lee at
Fri Nov 11 23:51:36 CET 2005

On 2005-11-11, ale.of.ginger at <ale.of.ginger at> wrote:
> Now that I have gotoxy() down for moving the cursor around, I want that
> to be a result of keypresses (namely from the numpad -- 7 = NorthWest,
> 8 = North, 9 = NE, etc...).  I have little clue how to do this.  After
> searching google, I've come upon this; include:
> import curses
> in the header.  However, I've found various methods of actually reading
> the keyboard press (getch(), raw_input, etc.).  What is the best method
> to use for reading keypress, so I can have something like:
> if (keypressvariable == 'numpad7'):
>      WConio.gotoxy(x-1,y+1)

Here is a little snip that shows how I do this:

import curses

class TestScreen:
    def __init__(self, scr):
        self.scr = scr
        self.scr.move(10, 5)
        self.scr.addstr('Press q to QUIT', curses.A_BOLD)

    def loop(self):
        k = None
        while k != 'q':
            k = self.scr.getkey()
                o = ord(k)
                o = '????'
            #Error.elog('test:', k, o)

def test_keys(scr):
    screen = TestScreen(scr)

if __name__ == '__main__':

Not on windows, though. So I do not know if this
will work for you at all.

The .elog thing is a function I wrote for testing
curses apps. Since the console is busy, you can't
usefully print to stdout. So .elog just feeds its
arguments to a log file.

I found the trickiest part is that the keyboard
scan codes are all over the place. Two systems
that are identical in every other way might give
two different results when pressing a key.

More information about the Python-list mailing list