非阻塞 getch()、ncurses [英] Non-blocking getch(), ncurses

查看:19
本文介绍了非阻塞 getch()、ncurses的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在阻止 ncurses 的 getch() 时遇到了一些问题.默认操作似乎是非阻塞的(或者我错过了一些初始化)?我希望它像 Windows 中的 getch() 一样工作.我试过各种版本的

I'm having some problems getting ncurses' getch() to block. Default operation seems to be non-blocking (or have I missed some initialization)? I would like it to work like getch() in Windows. I have tried various versions of

timeout(3000000);
nocbreak();
cbreak();
noraw();
etc...

(不是同时).如果可能,我宁愿不(明确地)使用任何 WINDOW.一个围绕 getch() 的 while 循环,检查特定的返回值也是可以的.

(not all at the same time). I would prefer to not (explicitly) use any WINDOW, if possible. A while loop around getch(), checking for a specific return value is OK too.

推荐答案

curses 库是一揽子交易.如果没有正确初始化库,您不能只抽出一个例程并希望获得最好的结果.这是在 getch() 上正确阻塞的代码:

The curses library is a package deal. You can't just pull out one routine and hope for the best without properly initializing the library. Here's a code that correctly blocks on getch():

#include <curses.h>

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

这篇关于非阻塞 getch()、ncurses的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆