LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   How can I make a program receive function-key presses? (https://www.linuxquestions.org/questions/linux-software-2/how-can-i-make-a-program-receive-function-key-presses-4175549849/)

wba2 08-04-2015 06:56 PM

How can I make a program receive function-key presses?
 
I'm trying to make my program receive function key (and arrow, home, end, page up, etc.) key presses. I'm reading data in character-at-a-time mode (so I can do immediate completions) with
term.c_cc[VMIN] = 1; /* 1 char at a time */
term.c_cc[VTIME] = 0; /* no time limit on input */
term.c_lflag &= ~(ICANON|ECHO);
and using getchar() or getc(stdin).

Special keys come across as sequences with ESC, as in
f5 = esc [ 1 5 ~ or something like that.
I think these are old VT100 sequences. But the keyboard has an actual escape button, and I want that to be a command also. (This was very bad design of the VT-100, but it's too late now. It should have transmitted two escapes when the escape button was pressed, in the same way that \\ in a C character string means "backslash, and I really mean it".)

So, when I get the escape code (0x1B) I have to check whether there is more data immediately. I've tried read() with various values of VMIN and VTIME, or getc() for the first character and read() to see if there are any more in the buffer, or poll(), to look for more characters. None of these can see the extra characters in the sequence. Subsequent calls to getc() get them, of course, but without any timing information.

Very specifically, my application program needs to accept a press of the ESC key as a command to auto-complete the input line buffer (I'm doing my own line editing and completion). Function keys like F5 are user-programmable "hot keys". I need to be able to tell the difference between a user pressing F5 and a user pressing ESC, left-bracket, 1, 5, and tilde. Or the difference between pressing alt-C and ESC, C. As things stand now, they differ only in timing. I could distinguish them if, after receiving ESC, I could get an answer to the question "Is there more data in the keyboard input buffer right now at this instant?" Presumably, if the user pressed ESC and then C, the system keyboard handler would know that the C came in perceptibly later than the ESC.

There must be some accepted standard way of doing this. Perhaps a lower-level access to the system keyboard handler?

I'm running Ubuntu 14.04 LTS on an HP-envy 15, though this problem has shown up, over the years, on many Linux systems.

Please don't tell me to use curses, though I'm willing to do whatever curses does.

pan64 08-05-2015 07:04 AM

if I remember well I had to remember ESC was sent and continue. If [ arrived I could go forward (that means getc again for the next byte), otherwise start over.


All times are GMT -5. The time now is 09:53 PM.