我正在尝试使用 ncurses 来获取非阻塞输入。
#include <iostream>
#include <ncurses.h>
int main()
{
char ch;
nodelay(stdscr, TRUE);
while(1)
{
ch= getch();
if (ch == ERR) {
printf("here \n");
usleep(100000);
}
else {
printf("---------------\n");
}
}
}
但是,当我运行此代码时,无论我按什么键,我总是只会打印“here”。
示例输出:
Latitude-E6430:~$ ./try
here
here
here
here
here
here
here
here
here
here
here
here
here
here
here
here
here
here
here
here
here
here
here
dhere
ddhere
dhere
here
根本没有检测到 d 和空格。
谁能告诉我为什么?
谢谢。
最佳答案
终于找到问题的答案了。
我需要执行 initscr();
之后我就可以正确打印了(尽管格式不符合预期)。
正确代码:
#include <iostream>
#include <ncurses.h>
int main()
{
char ch;
initscr();
nodelay(stdscr, TRUE);
while(1)
{
ch= getch();
if (ch == ' ') {
// printf("here \n");
usleep(100000);
}
else {
printf("---------------\n");
}
}
}
关于c++ - Ncurses 不检测击键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17552892/