Unexpected behavior of ncurses printw()

Hej!

I write a simple program using ncurses (that's the first time I use it), which should read the pressed key and print out the ASCII key code. First my loop which awaits the character looked simply like:

1
2
3
4
5
6
 while((c = getch()) != 27) {
    mvprintw((height / 2), (width / 2) - 12, "Keycode: %d  Character: %c", c, c );

    mvprintw(0, 0, "Press a key (ESC to exit)");
    refresh();
  }


It worked, but c (int) was sometimes equal more then one character. It does not depend on the inputing speed just on the character combination (for ex. pressing a letter and then number give me a double output). Other problem is that I want program to execute only when `ESC` is pressed, but it does also when I use arrow keys.

So as a designer instead of fixing the problem (since I'm even not sure what cause it), I start playing around with graphical presentation of the program. Finally my loop looks like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
  attron(A_ALTCHARSET);
  mvprintw(0, 0, "a");
  attroff(A_ALTCHARSET);
  mvprintw(0, 2, "Press a key (ESC to exit)");
  
  while((c = getch()) != 27) {
    attron(A_REVERSE);
    attron(A_BOLD);
    mvprintw((height / 2), (width / 2) - 12, "Keycode: %d  Character: %c", c, c );
    attroff(A_REVERSE);
    attroff(A_BOLD);
    attron(A_ALTCHARSET);
    printw("a");
    attroff(A_ALTCHARSET);
    mvprintw(0, 2, "Press a key (ESC to exit)");
    refresh();
}


Before "Press a key" message I add a special character, just to make it look better. Surprisingly, it fix my problem with the `mysterious double characters` witch been now masked with the `mysterious special character`.

Ok.. it works, but I can not say I fix it. Can someone explain me how the attron(A_ALTCHARSET) modification works now at thous extra characters in mvprintw((height / 2), (width / 2) - 12, "Keycode: %d Character: %c", c, c )
Thank you and sorry if I misspell something. I'm still learning English ^^.
Last edited on
It worked, but c (int) was sometimes equal more then one character. It does not depend on the inputing speed just on the character combination (for ex. pressing a letter and then number give me a double output).

The problem is that the old text is still there so if you press a key that has a keycode with three digits and then press another key that has a keycode that is only two digits the last character from the first message will still be visible.

To get rid of the old message you need to make sure all characters are overwritten. In this case it might be as simple as just adding an extra space to the message that you print. To clear the whole screen I think you can use the clear() function.

Other problem is that I want program to execute only when `ESC` is pressed, but it does also when I use arrow keys.

The loop will exit when you press ESC so why not put the "program" after the loop?

Before "Press a key" message I add a special character, just to make it look better. Surprisingly, it fix my problem with the `mysterious double characters` witch been now masked with the `mysterious special character`.

You have "fixed" the problem by printing an extra character that will overwrite any other character that was there before. The ascii code for 'a' is 97 but with the alternative character set (A_ALTCHARSET) 97 instead maps to the '▒' character. If you don't want any "mysterious special character" to appear you can output spaces instead.
Last edited on
The think I don't understand is where this `mysterious` characters are outputted? I mean, I see that when I `move` render point after mvprintw((height / 2), (width / 2) - 12, "Keycode: %d Character: %c", c, c );, then thous characters moves with it, and later mvprintw() in the loop are rendered where they are moved. But when I put this move statement like:
1
2
3
   attron(A_INVIS);
    move(10, 0);
    attroff(A_INVIS)
;
it still appears. So when it really appears?
Which mysterious character are you talking about now?

The one that you outputted? ▒

The one that was left over from your previous output? %c

If you are talking about the cursor that is showing where the next input/output will be outputted (if you don't move it) you can hide it with curs_set(0).

If you are talking about the character that is outputted when you hit a key you can hide it with noecho().
Last edited on
I see I was because of this code:

1
2
3
attron(A_ALTCHARSET);
 printw("a");
attroff(A_ALTCHARSET);


I didn't specify its position, so it was just rendered the last place I leave the render-point/cursor.

Thank you!
Last edited on
Topic archived. No new replies allowed.