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 ^^.