Search code examples
c++cinputncursescurses

How can I block certain keys in C++ with NCurses


I'm new to C/C++ and I'm making a simple text user interface with NCurses.

Whenever I scroll up/down with the mouse wheel, or press arrow keys, the console echos characters like:

"[[A^[[C^[[B^[[D" 

to show me that I've pressed the keys.

I would like to stop these from echoing and only echo basic keys (punctuation and letters).

Here is my main loop. I basically want it to be my own console with commands that I create.

string input;
char inputArr[80];
while (input != "q" && input != "quit" && input != "exit" && input != "leave") {
    printw(" > ");
    refresh();
    getstr(inputArr);
    input = inputArr;
    if (input.substr(0, 3) != "someCommand") {
        printw("\n ~ %s\n\n", inputArr);
        refresh();
    } else
        execCmd();
}
quit();

For the most part I believe it is C++ but I do have a C function (that uses libCurl).

Also, is there no cleaner way to read in strings with NCurses? I dont really like using char arrays (I'm used to Java).


Solution

  • Call noecho() somewhere close to initscr().

    This will avoid clobbering your screen with unwanted input. If you want your users to see what they type later on, you will need to call echo() before, however.