如何在不清除屏幕的情况下使用curses 中的getch?
我正在学习用 C 语言编程,并且希望能够在代码运行时在终端中键入字符,而无需按 return。我的程序可以运行,但是当我调用 initscr()
时,屏幕会被清除 - 即使在调用 filter()
之后也是如此。 filter
的文档建议它应该禁用清除 - 但对我来说情况并非如此。
#include <stdio.h>
#include <curses.h>
#include <term.h>
int main(void) {
int ch;
filter();
initscr();
cbreak();
noecho();
keypad(stdscr, TRUE);
while((ch = getch()) != EOF);
endwin();
return 0;
}
为什么上面的代码仍然使屏幕更清晰,可以采取什么措施来修复它?
我正在使用 Debian Lenny(稳定版)和 gnome-terminal(如果有帮助的话)。
I'm learning to program in C and want to be able to type characters into the terminal while my code is running without pressing return. My program works, however when I call initscr()
, the screen is cleared - even after calling filter()
. The documentation for filter
suggests it should disable clearing - however this is not the case for me.
#include <stdio.h>
#include <curses.h>
#include <term.h>
int main(void) {
int ch;
filter();
initscr();
cbreak();
noecho();
keypad(stdscr, TRUE);
while((ch = getch()) != EOF);
endwin();
return 0;
}
Why does the above code still clearr the screen, and what could be done to fix it?
I'm using Debian Lenny (stable) and gnome-terminal if that helps.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
由于以下原因之一,您会在诅咒应用程序中看到屏幕被清除:
initscr
(清除屏幕)或newterm
无需先调用filter
,或者在后一种情况下,您可以通过将
enter_ca_mode
和exit_ca_mode
指针重置为 NULL 如dialog
中所做的那样。更好的是,选择一个符合您要求的终端描述。进一步阅读:
You would see your screen cleared in a curses application for one of these reasons:
initscr
(which clears the screen) ornewterm
without first callingfilter
, orIn the latter case, you can suppress the alternate screen feature in ncurses by resetting the
enter_ca_mode
andexit_ca_mode
pointers to NULL as done indialog
. Better yet, choose a terminal description which does what you want.Further reading:
通过 mike.dld 扩展答案,这对我来说适用于 MacOS X 10.6.6 (GCC 4.5.2) 和系统诅咒库 - 无需清除屏幕。我添加了记录输入字符的功能(记录到文件“x”),以及输入 CONTROL-D 并停止程序而不是强制用户中断的功能。
Extending the answer by mike.dld, this works for me on MacOS X 10.6.6 (GCC 4.5.2) with the system curses library - without clearing the screen. I added the ability to record the characters typed (logged to a file "x"), and the ability to type CONTROL-D and stop the program rather than forcing the user to interrupt.
使用
newterm()
而不是initscr()
,那么你应该没问题。如果您遵循此建议,请不要忘记delscreen()
。Use
newterm()
instead ofinitscr()
, you should be fine then. And don't forget aboutdelscreen()
if you follow this advice.基本上,curses 旨在接管屏幕(或窗口,在窗口终端的情况下)。你不能真正将curses 与stdio 混合在一起,也不能真正使用curses 来输入或输出某些内容而不弄乱屏幕的其余部分。有部分解决方法,但您永远无法真正使其按照您想要的方式工作。对不起。
我建议要么重写你的程序以在整个程序中使用curses,要么研究像readline这样的替代方案。
Basically, curses is designed to take over the screen (or window, in the case of a windowed terminal). You can't really mix curses with stdio, and you can't really use curses to just input or output something without messing with the rest of the screen. There are partial workarounds, but you're never really going to be able to make it work the way that it sounds like you want to. Sorry.
I'd suggest either rewriting your program to use curses throughout, or investigating alternatives like readline.