Search code examples
pythoncurses

python Curses getkey returns no input when I used direction keys


I made a game using the terminal and pythons curses module. I steer the snake using the kbinput function. This works great for the number pad and the standard letters, however, when I try to use the direction keys it throws the exception _cureses_error: no input If I hold the keys down eventually one of the commands will get through eventually. I have it in full blocking mode, so it makes no sense to get the no input exception.

I tried a quick test to see if having it running in a thread was a problem, but in my test case the direction keys work fine.

relevant functions are below:

def game():
    screen=curses.initscr()
    curses.start_color()
    curses.raw()
    curses.cbreak()
    curses.noecho()
    screen.keypad(True)
    try:
        gtime=time.time()
        b=Board(screen)
        s=Snake(b)
        #thread.start_new_thread(steps,(s,b))
        thread.start_new_thread(kbinput,(s,b,screen))
        ....

def kbinput(snake,b,screen):
    while snake.alive==True:
        key=""
        key=screen.getkey()
        b.key=key
        if key=="q":
            snake.alive=False
            break
        elif (key == "8" or key == "i" or key == 'KEY_UP') and snake.direction != 3: snake.direction=1
        elif (key == "6" or key == "l" or key == 'KEY_RIGHT') and snake.direction != 4: snake.direction=2
        elif (key == "4" or key == "j" or key == 'KEY_LEFT') and snake.direction != 2: snake.direction=4
        elif (key == "5" or key == "k" or key == 'KEY_DOWN') and snake.direction != 1: snake.direction=3
    return

EDIT: Thomas was correct that the problem seems to be with threading the kbinput. To fix the problem I made these changes to the functions:

def game():
    screen=curses.initscr()
    curses.start_color()
    curses.raw()
    curses.cbreak()
    curses.noecho()
    screen.keypad(True)
    curses.halfdelay(1)
    curses.init_pair(1,curses.COLOR_RED,curses.COLOR_BLACK)
    curses.init_pair(2,curses.COLOR_GREEN,curses.COLOR_BLACK)
    try:
        gtime=time.time()
        b=Board(screen)
        s=Snake(b)
        #thread.start_new_thread(kbinput,(s,b,screen))
        while s.alive and s.x != 0 and s.x != b.width-1 and s.y !=1 and  s.y != b.height-2:
            kbinput(s,b,screen)
            gtime=time.time()
            update(s,b,screen)
            if snake_check(s) == True: break

def kbinput(snake,b,screen):
    #while snake.alive==True:
        key=""
        key2=screen.getch()
        if key2==-1: return
        try:key=chr(key2)
        except:pass
        if key=="q":
            snake.alive=False
            #break
        elif (key == "8" or key == "i" or key2 == curses.KEY_UP) and snake.direction != 3: snake.direction=1
        elif (key == "6" or key == "l" or key2 == curses.KEY_RIGHT) and snake.direction != 4: snake.direction=2
        elif (key == "4" or key == "j" or key2 == curses.KEY_LEFT) and snake.direction != 2: snake.direction=4
        elif (key == "5" or key == "k" or key2 == curses.KEY_DOWN) and snake.direction != 1: snake.direction=3
        return

Solution

  • Rather than look for a string-literal

    key == 'KEY_UP')
    

    look for the defined-constant

    key == curses.KEY_UP)
    

    although as noted in a comment, that requires the getch method.

    However, this line indicates a more serious problem:

    thread.start_new_thread(kbinput,(s,b,screen))
    

    Curses in general is not thread-safe (and python's curses-binding is subject to that limitation):