Input 获取一个不等待输入的字符
我正在使用ncurses.h为游戏创建菜单。在这一部分中,我希望屏幕上有一个闪烁的文本,并且只有当用户按下任何键时才退出该循环。在conio.h中,我使用了这个(不完美):Input 获取一个不等待输入的字符,input,char,ncurses,2d-games,conio,Input,Char,Ncurses,2d Games,Conio,我正在使用ncurses.h为游戏创建菜单。在这一部分中,我希望屏幕上有一个闪烁的文本,并且只有当用户按下任何键时才退出该循环。在conio.h中,我使用了这个(不完美): void start() { int i; 对于(i=0;i
void start()
{
int i;
对于(i=0;i<10;i++)
{
gotoxy(32,18-i);
printf(“1024”);
睡眠(200);
clrsc();
}
gotoxy(32,8);
printf(“1024”);
gotoxy(35,18);
而(!kbhit())
{
textcolor(15);
gotoxy(15,15);
printf(“按任意键继续…”);
如果(kbhit()!=0)
睡眠(1000);
textcolor(0);
gotoxy(15,15);
printf(“连续性的质量保证书…”);
如果(kbhit()!=0)
打破
睡眠(500);
如果(kbhit()!=0)
打破
}
textcolor(15);
fflush(stdin);
clrsc();
}
在ncurses.h中,我尝试做同样的事情,但是getch()似乎在等待用户输入,因此“暂停”执行
void start()
{
int i, ch = 0;
for (i = 0; i < 10; i++)
{
attron(COLOR_PAIR(2));
move(18 - i, 32);
printw("1024");
refresh();
usleep(200 * 1000);
clear();
refresh();
}
move(18, 35);
while (1)
{
if (getch() != 0)
break;
attron(COLOR_PAIR(2));
move(15, 15);
printw("Aperte qualquer tecla para continuar...");
refresh();
usleep(1000 * 1000);
clear();
refresh();
move(15, 15);
printw("Aperte qualquer tecla para continuar...");
usleep(500 * 1000);
}
attroff(COLOR_PAIR(2));
system("clear");
}
void start()
{
int i,ch=0;
对于(i=0;i<10;i++)
{
attron(颜色对(2));
动议(18-i,32);
printw(“1024”);
刷新();
usleep(200*1000);
清除();
刷新();
}
动议(18、35);
而(1)
{
如果(getch()!=0)
打破
attron(颜色对(2));
移动(15,15);
printw(“连续性的质量保证书…”);
刷新();
usleep(1000*1000);
清除();
刷新();
移动(15,15);
printw(“连续性的质量保证书…”);
usleep(500*1000);
}
attroff(颜色对(2));
系统(“清除”);
}
解决方案?
谢谢 使用ncurses(任何curses实现),您可以通过在初始化期间调用来获得单字符输入(请参阅上的手册页部分)
另外,我宁愿使用usleep,而不是usleep
由于您的程序正在使用getch
,希望只进行轮询,因此您可能希望使用或忽略getch
返回ERR
的情况。比如说
cbreak(); /* control/C will kill the program */
timeout(10); /* 10 milliseconds is a good compromise */
while (1)
{
if (getch() == ERR)
continue; /* ignore, timeout expired with nothing read */
嗯,对不起,但我不知道如何使用超时或节点延迟来解决我的具体问题。如果用户在x时间内没有按任何键,是否要退出该循环?如果,那么我仍然想尝试无限期地进入循环,直到用户按下某个按钮。。。谢谢你的回答!这是我现在得到的:
while(1){wattron(赢,颜色对(继续开始));mvwprintw(赢,18,25,“按任意键继续”);wrefresh(赢);napms(1000);wattron(赢,颜色对(继续开始关闭));mvwprintw(赢,18,25,“按任意键继续”);wrefresh(赢);napms(1000);timeout(10);if(getch()==ERR)break;}
在超时时中断将没有帮助,因为循环将在1秒后退出。
cbreak(); /* control/C will kill the program */
timeout(10); /* 10 milliseconds is a good compromise */
while (1)
{
if (getch() == ERR)
continue; /* ignore, timeout expired with nothing read */