Latest LQ Deal: Latest LQ Deals
Go Back > Forums > Linux Forums > Linux - Software
User Name
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.


  Search this Thread
Old 08-04-2015, 06:56 PM   #1
LQ Newbie
Registered: Aug 2015
Posts: 1

Rep: Reputation: Disabled
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.

Last edited by wba2; 08-05-2015 at 11:27 AM. Reason: Make very clear what inputs I'm trying to distinguish.
Old 08-05-2015, 07:04 AM   #2
LQ Addict
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 20,772

Rep: Reputation: 7058Reputation: 7058Reputation: 7058Reputation: 7058Reputation: 7058Reputation: 7058Reputation: 7058Reputation: 7058Reputation: 7058Reputation: 7058Reputation: 7058
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.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Map serial input to key presses prushik Linux - Software 0 03-21-2011 04:55 PM
C program to perform an operation continuously until user presses a key ananth86coolguy Linux - Newbie 4 03-13-2009 09:59 AM
Simulating Key Presses Hal Programming 2 09-02-2005 05:05 PM
Detecting Arrow Key Presses in C / C++ BooKA. Programming 8 04-07-2004 06:10 AM
How to listen to all key presses in whole Linux? pvv Programming 1 03-28-2004 01:06 PM > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 12:45 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration