[Python] How to prevent keyboard even delay in python terminal app

Discussão em 'Python' iniciado por Stack, Maio 4, 2025.

  1. Stack

    Stack Membro Participativo

    I'm making a python game with a terminal UI, using the blessed library. When I process a key held down, I receive the first key immediately, but then there's a delay between the first and subsequent keys received. Here is my simplified code:

    from blessed import Terminal

    def point_generator(width, height):
    while True:
    for y in range(height):
    for x in range(width):
    yield (x, y)


    def main() -> None:
    term = Terminal()
    with term.fullscreen(), term.cbreak(), term.hidden_cursor():
    print(term.home + term.clear)
    point_gen = point_generator(term.width, term.height)
    while True:
    if(term.kbhit(0)):
    key = term.getch()
    point = next(point_gen)
    print(term.move_xy(*point) + "0", end="", flush=True)


    if __name__ == "__main__":
    main()


    This code writes the "0" character to the screen in left-to-right, top-to-bottom order. When a key is held down, you can see the delay between the first and subsequent characters.

    I am running this inside a vs code linux devcontainer on Windows 11 on python 3.13.3.

    Continue reading...

Compartilhe esta Página