Search code examples
pythoncursesgetch

curses getch() returns different values for the main window and pads in python


The getch() method returns different values for the main window and pads if the key pressed is non-ASCII. For example, pressing the arrow keys I get the expected KEY_UP, KEY_DOWN etc in the main window, but in the pad I get 65 for the up arrow and 66 for the down arrow. Why is this, and is there a way to get larger than 255 values for special keys in a pad?

I am using Python 2.6.5.

The following code demonstrates the issue:

import curses

def main(stdscr):
    c = None
    while c != curses.KEY_RIGHT:
        c = stdscr.getch()
        stdscr.addstr(0, 0, "%3d" % c)
        stdscr.refresh()
    pad = curses.newpad(20, 20)
    while True:
        c = pad.getch()
        pad.addstr(0, 0, "%3d" % c)
        pad.refresh(0, 0, 1, 0, 20, 20)

if __name__ == '__main__':
    curses.wrapper(main)

Solution

  • Did you try pad.keypad(1)

    This is the first time I need to deal with curses in Python, and I had the same problem this morning.