Nathan Renegar
Nathan Renegar

Reputation: 16

Python Curses Backspace detection works in main window but not in subwindows

I've written this function that takes user input of a specified screen for a specified length:

def get_str(scr, max_len):

    curses.echo()
    curses.curs_set(2)

    usr_in = ''
    cur_len = 1;
    tmp = scr.getkey()   

    while(tmp != '\n' and cur_len < max_len):
        if tmp == 'KEY_BACKSPACE' and cur_len > 1:
            cur_len -= 1                
            usr_in = usr_in[:-1]
            curs_pos = scr.getyx()
            scr.addstr(curs_pos[0], curs_pos[1], " ")
            scr.move(curs_pos[0], curs_pos[1])
        else:
            usr_in += tmp
            cur_len += 1
        tmp = scr.getkey()

    if(cur_len == max_len):
        usr_in += tmp

    curses.noecho()
    curses.curs_set(0)

    return usr_in 

I'm using curses.wrapper with a main function that sets up all of the windows. When I call get_str in the main window given by wrapper, the function works as intended. Hitting backspace takes the last character off of the screen and moves the cursor backwards. However, when I call this in a subwindow of the main window, '^?' is displayed and does not trigger the if tmp == 'KEY_BACKSPACE' statement. This is how I set up a subwindow:`

def main(stdscr):

    lines = curses.LINES - 1
    cols = curses.COLS - 1

    board = stdscr.subwin(curses.LINES, int(3*(curses.COLS/5)), 0, int(curses.COLS/5) + 1)
    board.border()`

Upvotes: 0

Views: 282

Answers (1)

Thomas Dickey
Thomas Dickey

Reputation: 54525

Subwindows don't inherit the keypad setting. When you create a window, you'll have to set that, if you want to read keys that send "any" of the named KEY_ symbols.

Upvotes: 1

Related Questions