Search code examples
keyboardterminalpythonncurses

Unexpected output from directional keypress in MacOS


I'm using MacOS 10.14.3 and the Python 3.7 curses module to print the values returned when I press the up or down directional keys:

import curses

def main(stdscr):
    win = curses.newwin(24, 80, 0, 0)
    while True:
        ch = win.getch()
        win.addstr(str(ch) + '\n')

curses.wrapper(main)

When I press up, this prints:

27
91
65

When I press down, it prints:

27
91
66

From the docs, getch should be returning 258 (curses.KEY_UP) or 259 (curses.KEY_DOWN), respectively. Any idea what could be causing this behavior? Is my terminal misconfigured?


Solution

  • Use the keypad function, e.g.,

    win.keypad(1);
    

    before the loop (and after newwin).