如何从标准输入获取字符而不等待用户输入?

发布于 2024-12-10 06:49:18 字数 1776 浏览 0 评论 0 原文

我正在编写一个 C 程序,使用 ncurses 在终端上打印一些内容。当用户按“s”时,它应该停止打印,并在按“s”时再次继续。如何在不等待用户按下按键的情况下从输入中读取按键?

我尝试了 getch()getchar() 但它们会等到按下一个键...

编辑

这是我的代码:

int main(void)
{
   initscr(); /* Start curses mode         */
   refresh(); /* Print it on to the real screen */
   int i = 0, j = 0;
   int state = 0;
   while (1)
   {
      cbreak();
      int c = getch(); /* Wait for user input */
      switch (c)
      {
         case 'q':
            endwin();
            return 0;
         case 'c':
            state = 1;
            break;
         case 's':
            state = 0;
            break;
         default:
            state = 1;
            break;
      }
      if(state)
      {
         move(i, j);
         i++;
         j++;
         printf("a");
         refresh();
      }
   }
   nocbreak();
   return 0;
}

编辑2 这效果很好。我得了100分:)

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

int main(void)
{
   initscr();
   noecho();
   cbreak();         // don't interrupt for user input
   timeout(500);     // wait 500ms for key press
   int c = 0;        // command: [c|q|s]
   int s = 1;        // state: 1= print, 0= don't print ;-)
   int i = 0, j = 0;
   while (c != 'q')
   {
      int c = getch();
      switch (c)
      {
         case 'q':
            endwin();
            return 0;
         case 'c':
            s = 1;
            break;
         case 's':
            s = 0;
            break;
         default:
            break;
      }
      if (s)
      {
         move(i, j);
         printw("a");
         i++;
         j++;
      }
   }
   endwin();
   nocbreak();
   return 0;
}

I'm writing a C program that prints something on terminal using ncurses. It should stop printing when user press 's' and continue again when press 's'. How can I read a key from input without waiting user to press the key?

I tried getch() and getchar() but they wait until a key is pressed...

Edit

This is my code:

int main(void)
{
   initscr(); /* Start curses mode         */
   refresh(); /* Print it on to the real screen */
   int i = 0, j = 0;
   int state = 0;
   while (1)
   {
      cbreak();
      int c = getch(); /* Wait for user input */
      switch (c)
      {
         case 'q':
            endwin();
            return 0;
         case 'c':
            state = 1;
            break;
         case 's':
            state = 0;
            break;
         default:
            state = 1;
            break;
      }
      if(state)
      {
         move(i, j);
         i++;
         j++;
         printf("a");
         refresh();
      }
   }
   nocbreak();
   return 0;
}

EDIT 2
This works well. I got 100 points :)

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

int main(void)
{
   initscr();
   noecho();
   cbreak();         // don't interrupt for user input
   timeout(500);     // wait 500ms for key press
   int c = 0;        // command: [c|q|s]
   int s = 1;        // state: 1= print, 0= don't print ;-)
   int i = 0, j = 0;
   while (c != 'q')
   {
      int c = getch();
      switch (c)
      {
         case 'q':
            endwin();
            return 0;
         case 'c':
            s = 1;
            break;
         case 's':
            s = 0;
            break;
         default:
            break;
      }
      if (s)
      {
         move(i, j);
         printw("a");
         i++;
         j++;
      }
   }
   endwin();
   nocbreak();
   return 0;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

爱冒险 2024-12-17 06:49:18

ncurses 有能力通过它自己的 getch() 函数来做到这一点。请参阅页面

#include <curses.h>

int main(void) {
  initscr();
  timeout(-1);
  int c = getch();
  endwin();
  printf ("%d %c\n", c, c);
  return 0;
}

ncurses has the capability to do this through it's own getch() function. See this page

#include <curses.h>

int main(void) {
  initscr();
  timeout(-1);
  int c = getch();
  endwin();
  printf ("%d %c\n", c, c);
  return 0;
}
书信已泛黄 2024-12-17 06:49:18

由于您使用的是 ncurses,因此首先调用 cbreak 来关闭行缓冲。然后,您将调用 nodelay 告诉它在返回之前不要等待 - getch 将始终立即返回。当它发生时,您将检查是否按下了某个键,如果按下了,那是什么键(并做出适当的反应)。

Since you're using ncurses, you start by calling cbreak to turn off line buffering. Then you'll call nodelay to tell it not to wait before returning -- getch will always return immediately. When it does, you'll check whether a key was pressed, and if so what key that was (and react appropriately).

独自←快乐 2024-12-17 06:49:18

comp.lang.c 常见问题解答中对此有答案。请参阅问题 19.1,“如何在不等待 RETURN 键的情况下从键盘读取单个字符?如何阻止字符在键入时在屏幕上回显?”。

在这里发帖有点长。

There's an answer to this in the comp.lang.c FAQ. See question 19.1, "How can I read a single character from the keyboard without waiting for the RETURN key? How can I stop characters from being echoed on the screen as they're typed?".

It's a bit long to post here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文