Last active
December 18, 2020 01:08
-
-
Save BrandonIrizarry/a477cdae56212d33c9e4ec1f8176d909 to your computer and use it in GitHub Desktop.
Small program to investigate handling multibyte keys in ncurses; based on another Github Gist.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Adapted, with light modifications, from | |
https://gist.github.com/lesovsky/f0f5d31ac48f2fc53e5f | |
The original name of the program was, "ncurses-get-key-code.c". | |
The problem was the following: | |
BACKGROUND: | |
Normally, ncurses will read an arrow key, function key, or some other | |
"unusual" key as three bytes (the first byte of which seems to usually | |
be 27, or Escape.) The following program can be adapted to | |
demonstrate this, by removing the invocation to 'keypad': it can | |
be shown, for example, that pressing the 'home' key emits the sequence | |
27, 79, 72. | |
The aforementioned ncurses function 'keypad' will cause such | |
unusual keys to be read as a _single_ integer code, facilitating that | |
that the value can be captured and handled by a program (as is done | |
below.) | |
PROBLEM: | |
I enabled 'keypad', but only arrow keys were being synthesized into | |
single-integer codes; function keys, along with the HOME key, were still | |
being read by the program as their original three-byte sequence. | |
SOLUTION: | |
The following Stack Overflow post helped: | |
https://stackoverflow.com/questions/55357479/how-to-process-key-home-and-key-end-reliably-under-ncurses | |
It turns out that the value of my TERM variable in my AntiX installation | |
was 'xterm-color', which was confusing ncurses working under this same | |
xterm. Simply setting TERM=xterm (and making that change persistent by | |
including it in my .bashrc) solves the problem! | |
Incidentally, the person who provided the Stack Overflow answer, | |
Thomas Dickey, was involved in the development of both ncurses and xterm! | |
See 'https://invisible-island.net/'. | |
*/ | |
#include <ncurses.h> | |
int main() | |
{ | |
initscr(); | |
keypad(stdscr, true); // Turn off the function-keys' reading mode | |
noecho(); // Turn off the display of entered characters - you need it for getch() | |
scrollok(stdscr, true); // Added to provide the user more lines to work with | |
printw("Press F2 to exit.\n"); | |
bool exit = false; | |
while ( !exit ) | |
{ | |
int ch = getch(); | |
switch ( ch ) | |
{ | |
case ERR: | |
printw("Please, press any key...\n"); // If there was no pressing, we remind the user to press the key | |
break; | |
case KEY_F(2): // Exit the program if F2 was pressed | |
exit = true; | |
break; | |
default: // If everything is fine, we display the code of the pressed key | |
printw("vvvvvvv\n"); | |
printw("Code of pressed key is %d\n", ch); | |
printw("^^^^^^^\n"); | |
break; | |
} | |
refresh(); // Displaying on the real screen... | |
} | |
printw("Thank you. Good bye!"); | |
getch(); | |
endwin(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment