moonbutt74
moonbutt74

Reputation: 115

NCURSES printw Getting correct output of keycodes for RETURN and SPACE

Okay the code I'm working with below works great with the exception of printing out the correct information for the keys RETURN and SPACE. I have tried many approaches but this one seems the closest.

UPDATE: as per recommend by Thomas Dickey,

#include <ncurses.h>

int main()
{
    initscr();
    cbreak(); /* as per recommend Thomas Dickey */
    noecho(); /* as per recommend Thomas Dickey */

    int c, SPACE=32, RETURN=10; /* i know this is wrong but..? */

/* with changes return key is 10 , yet name for keys SPACE && RETURN
    do not display, which is the gist of my question. 
    What am i missing? */

    printw("Write something (ESC to escape): ");
    move(2,0);
    while((c=getch())!=27)
    {
        //move(2,0);
        printw("Keycode: %d, and the character: %c\n", c, c);       
        refresh();
    }
    endwin();
    return 0;
}

This is the terminal output: after update,

Write something (ESC to escape):

Keycode: 32, and the character: <--spacebar,- no label
Keycode: 10, and the character: <--enter/return key - no label

Keycode: 121, and the character: y
Keycode: 97, and the character: a
Keycode: 109, and the character: m
Keycode: 115, and the character: s

I yam still at a loss.... xD.

I am following this tutorial from youtube user thecplusplusguy though it isn't actually C++.

To be clear, the output above is what I want, with the exception of the "missing" labels for spacebar and return keys.

Thank you.

This is all leading to a terminal based reader for project gutenberg releases. I needed to do something new. Android is annoying.

Upvotes: 0

Views: 1243

Answers (1)

Thomas Dickey
Thomas Dickey

Reputation: 54524

This line

while((c=getchar())!=27)

is using the standard I/O input function getchar. The corresponding curses function is getch.

Additionally, the ncurses manual page Initialization section notes:

To get character-at-a-time input without echoing (most interactive, screen oriented programs want this), the following sequence should be used:

initscr(); cbreak(); noecho();

The printw function ultimately uses addch, which (see manual page):

Carriage return moves the cursor to the window left margin on the current line.

The question refers to RETURN as "31", but does not explain why. The given example appears to show OP entering other text before the input line was read.

The cited tutorial is missing the initialization to run in single-character mode.

OP's program will not work without the suggested fixes from this answer.

OP is interested in how to render space and return so that something will show up in the example program. Without some special treatment, those characters will be rendered without any noticeable text: space will be a blank. You can see that better by changing the printw call to something like this:

printw("Keycode: %d, and the character: \"%c\"\n", c, c);

The return is a different problem. First, it is normally translated from an ASCII controlM (^M) to a "newline" (actually an ASCII ^J). For most characters, the curses function keyname (which returns a pointer to a string) shows these characters in printable form. So you could use this call instead:

printw("Keycode: %d, and the character: \"%s\"\n", c, keyname(c));

The resulting program would show you that return is read as ^J. If you want it to be ^M, you would call raw() rather than cbreak() (but then you could not stop the program using a ^C).

Upvotes: 1

Related Questions