how to get a character from stdin without waiting for user to put it? [duplicate]

徘徊边缘 提交于 2019-11-29 05:02:34

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;
}

I believe there's an answer to this in the comp.lang.c fAQ. I can't load the site at the moment, but look in the "System dependencies" section.

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).

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!