My app reading escape sequences from terminal in raw mode. And when it's running on xterm I got F2 like "\eOQ". But when it's running in linux tty terminal (Switching by Ctrl-Alt-F1) I got "\e[[[B".
What is the correct way to determine that I got F2 independent from terminal type application running on?

- 249
- 2
- 10
2 Answers
If you're wanting to read terminal keypresses, you likely want to look at something like libtermkey , which abstracts the general problem away for you. Internally it uses a combination of terminfo
lookups, or hardcoded knowledge of the extended xterm
-like model for modified keypresses, so it can understand things like Ctrl-Up
, which a regular curses/etc... cannot.
while((ret = termkey_waitkey(tk, &key)) != TERMKEY_RES_EOF) {
termkey_strfkey(tk, buffer, sizeof buffer, &key, TERMKEY_FORMAT_VIM);
printf("You pressed key %s\n", buffer);
if(key.type == TERMKEY_TYPE_FUNCTION &&
!key.modifiers &&
key.code.number = 2)
printf("Got F2\n");
}

- 8,344
- 1
- 29
- 36
-
Is it possible to use it in the next way: If one of the F1-F9 keys pressed then I got a some constant or something like in your example. But if any other not alphanumeric key pressed e.g. F10-F12 I got it's escape sequence? – van Aug 22 '12 at 11:44
-
No; the entire point of `libtermkey` is to abstract away those raw escape sequences, and give you instead data structures (i.e. the `TermKeyKey` structure) to represent the keypress. – LeoNerd Aug 23 '12 at 09:11
-
And this new structures doesn't have sequences by which it were produced in there? Maybe it's not hard to patch it to include this data? – van Aug 23 '12 at 19:46
-
Well you could easily create an escape sequence back again, but the original input data has now been lost. – LeoNerd Aug 23 '12 at 20:01
-
Could you please explain how could I create escape sequence back again? – van Aug 23 '12 at 20:47
-
Cause I need to do it depend on terminal type my app working on... Has libtermkey special functions for that? – van Aug 24 '12 at 09:22
-
No. You could regenerate it from terminfo or a static CSI database or something, but the original sequence has been lost. Why do you need to generate a sequence, anyway? That suggests something very odd in the application. – LeoNerd Aug 26 '12 at 11:19
-
I need to handle certain escape sequences local in my app, but if it is not one of the sequences I need to handle, then I send it to remote side in raw form to be handled by remote terminal. Is it difficult to make libtermkey save original escape sequence in a structure TermKeyKey for example? – van Aug 26 '12 at 15:48
-
I think by this point it's turning more into a chat quite away from the original question. I'll reply by email.... – LeoNerd Aug 28 '12 at 15:37
Ok, as I got the best way to use [n]curses library. It is read terminfo (termcap) database and determine what mean escape sequence you got depend on terminal type.
It is not necessary using it's terminal graphics functions. To get correct escape sequences using curses you may do the following:
newterm(NULL, stdout, stdin);
Also, it is probably possibly do it manually by reading terminfo database in you app.
raw();
noecho();
keypad();
ch = getch();
if (ch == KEY_F(2)) printf("Got F2");
endwin();

- 249
- 2
- 10