gpt4 book ai didi

c - Ncurses 和关闭输入接收

转载 作者:行者123 更新时间:2023-11-30 17:42:07 26 4
gpt4 key购买 nike

在我的程序启动过程的开始,有一种简短的介绍,其中字符在移动,盒子慢慢出现等等。之后,正确的功能正在激活,等待用户的操作输入(getstr(提示);)。但是,如果我在加载介绍时按任意键,输入会自动传送到提示字符串,这是不可取的。如何关闭从输入读取直到 getstr(prompt); 之前的一行,然后激活它?或者也许有不同的方法来解决这个问题?我的想法是使用这样的阻塞函数(不确定它是否有效):

timeout (1);
while (intro == 1)
{
continue;
}
timeout (-1);

但我认为一直检查这个论点并不是处理问题的优雅方式。

最佳答案

我认为你想要的答案是flushinp()。

来自手册页

The flushinp routine throws away any typeahead that has been typed by the user and has not yet been read by the program.

https://developer.apple.com/library/ios/documentation/System/Conceptual/ManPages_iPhoneOS/man3/flushinp.3x.html

这是一个可能适合您的用法的示例

#include <stdlib.h>
#include <curses.h>

void atexit_cb(void) {
endwin();
}

int main(void)
{
// initialize curses
initscr();
raw();
keypad(stdscr, TRUE);
noecho();
nodelay(stdscr, FALSE);
atexit(atexit_cb);

// show an intro for 3 seconds
for (int i = 0; i < 3; i++) {
mvprintw(i, 0, "...Intro text...");
refresh();
napms(1000);
move(i, 0);
clrtoeol();
}

// flush typeahead
flushinp();

// now get some new input
printw("Press a key...");
echo();
getch();
return 0;
}

关于c - Ncurses 和关闭输入接收,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20818865/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com