开发者

How do I use getch from curses without clearing the screen?

开发者 https://www.devze.com 2023-02-06 10:09 出处:网络
I\'m learning to program in C and want to be able to type characters into the terminal 开发者_如何学Cwhile my code is running without pressing return. My program works, however when I call initscr(),

I'm learning to program in C and want to be able to type characters into the terminal 开发者_如何学Cwhile 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.


You would see your screen cleared in a curses application for one of these reasons:

  • your program calls initscr (which clears the screen) or newterm without first calling filter, or
  • the terminal initialization clears the screen (or makes it appear to clear, by switching to the alternate screen).

In the latter case, you can suppress the alternate screen feature in ncurses by resetting the enter_ca_mode and exit_ca_mode pointers to NULL as done in dialog. Better yet, choose a terminal description which does what you want.

Further reading:

  • Why doesn't the screen clear when running vi? (xterm FAQ)


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.

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

#define CONTROL(x)  ((x) & 0x1F)

int main(void)
{
    FILE *fp = fopen("x", "w");
    if (fp == 0)
        return(-1);
    SCREEN *s = newterm(NULL, stdin, stdout);
    if (s == 0)
        return(-1);
    cbreak();
    noecho();
    keypad(stdscr, TRUE);

    int ch;
    while ((ch = getch()) != EOF && ch != CONTROL('d'))
        fprintf(fp, "%d\n", ch);

    endwin();

    return 0;
}


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.


Use newterm() instead of initscr(), you should be fine then. And don't forget about delscreen() if you follow this advice.

0

精彩评论

暂无评论...
验证码 换一张
取 消