使用 curses,如何更新屏幕或等待按键?

Using curses, how do I update the screen or wait for a key?

如何同时更新屏幕或等待按键?我在 Python 上使用 unicurses,但我想我在 C 中也会遇到同样的问题。 这是我想用伪代码做的事情:

function startScreen(){
   stdscr = initscr()
   while True{
      - Update screen using a variable that is constantly changing (probably by a thread, right?)
      - Get a key with getch() - to close or interact with the screen
   }
}

我的问题是除非发生某些事情,例如调整屏幕大小或按下某个键,否则屏幕不会更新。我正在考虑使用 while 循环(和 time.sleep(1)?)来更新屏幕和线程来等待键。那可能吗?我对线程了解不多,这就是我问的原因。有没有更简单的方法?

谢谢。

这可以在没有任何复杂的多线程的情况下完成。有一个函数 curses.halfdelay 也可以在您使用的 unicurses 库中找到。等待直到继续需要十分之几秒。 https://docs.python.org/3/library/curses.html#curses.halfdelay

这是一个示例代码,它每半秒刷新一次,除非有一个按钮被按下,在这种情况下它会立即更新。

import curses
scr = curses.initscr()
curses.halfdelay(5)           # How many tenths of a second are waited, from 1 to 255
curses.noecho()               # Wont print the input
while True:
    char = scr.getch()        # This blocks (waits) until the time has elapsed,
                              # or there is input to be handled
    scr.clear()               # Clears the screen
    if char != curses.ERR:    # This is true if the user pressed something
        scr.addstr(0, 0, chr(char))
    else:
        scr.addstr(0, 0, "Waiting")