Python Curses window.getch() returns wrong value

3.3k views Asked by At

Why when I run this code the box.getch() returns a wrong value and when I change box.getch() into screen.getch() it returns the right value? I've been looking on internet and there is no one saying that getch() works only with screens. If you press one of the arrows it returns 27 which is the chr of ESC. (This code should print the character untill the user presses ESC...)

import curses
screen = curses.initscr()
curses.noecho()
curses.cbreak()
curses.start_color()
screen.keypad( 1 )
curses.init_pair(1,curses.COLOR_BLACK, curses.COLOR_CYAN)
highlightText = curses.color_pair( 1 )
normalText = curses.A_NORMAL
screen.border( 0 )
curses.curs_set( 0 )
box = curses.newwin( 22, 64, 1, 1 )
box.box()
box.addstr( 14, 3, "YOU HAVE PRESSED: ")

screen.refresh()
box.refresh()

x = box.getch()
while x != 27:
    box.erase()
    box.addstr( 14, 3, "YOU HAVE PRESSED: " + str(x) )
    screen.border( 0 )
    box.border( 0 )
    screen.refresh()
    box.refresh()
    x = box.getch()

curses.endwin()
exit()
2

There are 2 answers

0
Thomas Dickey On BEST ANSWER

The answer (see Bug with refresh in python curses) is to add box.keypad(1). There are a few lines which are unnecessary - those are marked in the example:

    import curses
    screen = curses.initscr()
    curses.noecho()
    curses.cbreak()
    curses.start_color()
    screen.keypad( 1 )    # delete this line
    curses.init_pair(1,curses.COLOR_BLACK, curses.COLOR_CYAN)
    highlightText = curses.color_pair( 1 )
    normalText = curses.A_NORMAL
    screen.border( 0 )
    curses.curs_set( 0 )
    box = curses.newwin( 22, 64, 1, 1 )
    box.keypad( 1 )
    box.box()
    box.addstr( 14, 3, "YOU HAVE PRESSED: ")

    screen.refresh()    # delete this line
    box.refresh()

    x = box.getch()
    while x != 27:
        box.erase()
        box.addstr( 14, 3, "YOU HAVE PRESSED: " + str(x) )
        screen.border( 0 )
        box.border( 0 )
        screen.refresh()  # delete this line
        box.refresh()     # delete this line
        x = box.getch()

    curses.endwin()
    exit()
0
dotbit On
#!/usr/bin/python3
import          curses
screen        = curses.initscr(      )
screen.border ( 0                    )
e             = curses.noecho(       )
e             = curses.cbreak(       )
e             = curses.start_color(  )
e             = curses.init_pair(  1 , curses.COLOR_GREEN, curses.COLOR_CYAN)
e             = curses.curs_set(   0 )
highlightText = curses.color_pair( 1 )
normalText    = curses.A_NORMAL
box           = curses.newwin(  20, 60, 1, 1 )
box.keypad( 1 )
box.box(      )
box.refresh(  )
e =   x  =  1
try:
  while x != 50 :            #     27  ESC
    box.erase(       )
    cc="WTF?"
    try:     cc=str(chr(x))
    except:  cc="ERR"
    box.addstr( 8, 8, "key 2 to quit, you pressed" + str(x) + "  '" + cc + "'" )
    screen.border( 0 )
    box.border(    0 )
    x = box.getch(   )
except : print("oh my!")
finally: curses.endwin() # avoids messed up console!