[svlug] Extended Keyboard Input Utility For Bash?

Mark S Bilk mark at cosmicpenguin.com
Sat Mar 24 17:21:04 PDT 2007


On Fri, Mar 23, 2007 at 08:13:19PM -0700, Tim Utschig wrote:
>On Fri, Mar 23, 2007 at 07:01:59PM -0700, Mark S Bilk wrote:
>> And I've written a C program to return the extended
>> codes like UP and F1, but it has a fixed translation
>> for the escape sequences (for KDE konsole) implemented
>> as a decision tree.
>
>Maybe use [n]curses' keyname() function?
>
><quote src="man 3 keyname">

Thanks, Tim.  I don't want to use ncurses because I'm just 
trying to get the extended keys, from a program that would
be called once per keystroke in a bash script.  So I don't
want to initialize the terminal (window) for ncurses and 
then deinitialize it for each key (or even figure out how to 
do that).

I found a solution in the program infocmp, which when 
called like so gives a list of the extended-key escape
codes derived from the terminfo database, using the TERM
variable to select the right table (ltrim is a sed call
that removes spaces and tabs from the beginning of a line):

infocmp -L1 | ltrim | grep '^key_' \
   | grep -v f[2-6][0-9] | sed 's/,$//' 

Trouble is, the escape code definitions for the keypad are
all wrong except for PgUp and PgDn.  For example, the 
Up-Arrow key emits Esc[A, but the entry I get from infocmp
is key_up=\EOA .  The other keypad keys have the same problem,
with their actual sequences containing [ where the terminfo 
entries have O.  My TERM variable is set to "xterm" in KDE 
konsoles and xterms, both of which give the same result.  
I don't think it's a problem with my C program or the bash 
script that matches the sequences with the infocmp output, 
since the function keys are detected OK, and some of them 
have O instead of [, e.g., key_f1=\EOP .  And all the keys 
are detected properly by the same software in the (e.g., 
Ctrl-Alt-F3) text consoles, in which TERM is set to "linux".  

Ncurses programs like mc and mutt run fine on my system; 
maybe they ignore those terminfo keypad entries and translate 
the escape sequences themselves.  I downloaded the latest 
Open-SuSE edition of the terminfo database, but its xterm* 
files have the same Esc O keypad sequences.

I guess I'll just convert and hand-edit a file from the infocmp 
xterm output and compile it into the C program, likewise for 
the console version, and the program can look at the TERM 
variable to decide which one to use.  Much neater than a 
decision tree.  I don't want to alter the system terminfo 
database, since it's working now for mutt, etc.  Still think 
I must be reinventing the wheel for a bash helper utility, 
but it's almost done now.

  Mark





More information about the Svlug mailing list