c++ - Ncurses 不检测击键

标签 c++ c ncurses

我正在尝试使用 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/

相关文章:

c++ - 检查 BSTR 是否为空

c - 哪种方法更好?

c - 在C中按字母顺序添加节点

python - 使用 pythoncurses 更改输入文本的颜色

c++ - 无法编译使用 ncurses 的 C/C++ 代码

Objective-c 中的 C++ 矩阵

c++ - 操作迭代器列表+指针

c++ - 替换对象 vector 的元素

c++ - 带有 ncurses 的子窗口

C 指针与二维数组