LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 09-18-2010, 05:34 AM   #1
tel7
LQ Newbie
 
Registered: Sep 2010
Posts: 3

Rep: Reputation: 0
Putting function key chars in a file


Hi guys,

Years ago on AIX I used to create a file of key strokes, including function keys (mainly F3 and F12) into a file, and used that file as input to an INFORMIX program, to automated tasks, something like this:
fglgo myprogram.fgo <keystrokefile.txt

Now, I'm using Aubit language on GNU/Linux, and I'd like to do the same kind of thing, but I can't recall how I worked out the chars for the function keys, I'm using a different emulation (xterm), and I can't work out what characters to put in the key stroke file.
My $TERM variable contains "xterm".
If I type "infocmp", I get this:
Code:
#       Reconstructed via infocmp from file: /usr/share/terminfo/x/xterm
xterm|xterm terminal emulator (X Window System),
        am, bce, km, mc5i, mir, msgr, npc, xenl,
        colors#8, cols#80, it#8, lines#24, pairs#64,
        acsc=``aaffggiijjkkllmmnnooppqqrrssttuuvvwwxxyyzz{{||}}~~,
        bel=^G, blink=\E[5m, bold=\E[1m, cbt=\E[Z, civis=\E[?25l,
        clear=\E[H\E[2J, cnorm=\E[?12l\E[?25h, cr=^M,
        csr=\E[%i%p1%d;%p2%dr, cub=\E[%p1%dD, cub1=^H,
        cud=\E[%p1%dB, cud1=^J, cuf=\E[%p1%dC, cuf1=\E[C,
        cup=\E[%i%p1%d;%p2%dH, cuu=\E[%p1%dA, cuu1=\E[A,
        cvvis=\E[?12;25h, dch=\E[%p1%dP, dch1=\E[P, dl=\E[%p1%dM,
        dl1=\E[M, ech=\E[%p1%dX, ed=\E[J, el=\E[K, el1=\E[1K,
        flash=\E[?5h$<100/>\E[?5l, home=\E[H, hpa=\E[%i%p1%dG,
        ht=^I, hts=\EH, ich=\E[%p1%d@, il=\E[%p1%dL, il1=\E[L,
        ind=^J, indn=\E[%p1%dS, invis=\E[8m,
        is2=\E[!p\E[?3;4l\E[4l\E>, kDC=\E[3;2~, kEND=\E[1;2F,
        kHOM=\E[1;2H, kIC=\E[2;2~, kLFT=\E[1;2D, kNXT=\E[6;2~,
        kPRV=\E[5;2~, kRIT=\E[1;2C, kb2=\EOE, kbs=\177, kcbt=\E[Z,
        kcub1=\EOD, kcud1=\EOB, kcuf1=\EOC, kcuu1=\EOA,
        kdch1=\E[3~, kend=\EOF, kent=\EOM, kf1=\EOP, kf10=\E[21~,
        kf11=\E[23~, kf12=\E[24~, kf13=\E[2P, kf14=\E[2Q,
        kf15=\E[2R, kf16=\E[2S, kf17=\E[15;2~, kf18=\E[17;2~,
        kf19=\E[18;2~, kf2=\EOQ, kf20=\E[19;2~, kf21=\E[20;2~,
        kf22=\E[21;2~, kf23=\E[23;2~, kf24=\E[24;2~, kf25=\E[5P,
        kf26=\E[5Q, kf27=\E[5R, kf28=\E[5S, kf29=\E[15;5~,
        kf3=\EOR, kf30=\E[17;5~, kf31=\E[18;5~, kf32=\E[19;5~,
        kf33=\E[20;5~, kf34=\E[21;5~, kf35=\E[23;5~,
        kf36=\E[24;5~, kf37=\E[6P, kf38=\E[6Q, kf39=\E[6R,
        kf4=\EOS, kf40=\E[6S, kf41=\E[15;6~, kf42=\E[17;6~,
        kf43=\E[18;6~, kf44=\E[19;6~, kf45=\E[20;6~,
        kf46=\E[21;6~, kf47=\E[23;6~, kf48=\E[24;6~, kf49=\E[3P,
        kf5=\E[15~, kf50=\E[3Q, kf51=\E[3R, kf52=\E[3S,
        kf53=\E[15;3~, kf54=\E[17;3~, kf55=\E[18;3~,
        kf56=\E[19;3~, kf57=\E[20;3~, kf58=\E[21;3~,
        kf59=\E[23;3~, kf6=\E[17~, kf60=\E[24;3~, kf61=\E[4P,
        kf62=\E[4Q, kf63=\E[4R, kf7=\E[18~, kf8=\E[19~, kf9=\E[20~,
        kfnd=\E[1~, khome=\EOH, kich1=\E[2~, kind=\E[1;2B,
        kmous=\E[M, knp=\E[6~, kpp=\E[5~, kri=\E[1;2A, kslt=\E[4~,
        mc0=\E[i, mc4=\E[4i, mc5=\E[5i, meml=\El, memu=\Em,
        op=\E[39;49m, rc=\E8, rev=\E[7m, ri=\EM, rin=\E[%p1%dT,
        rmacs=\E(B, rmam=\E[?7l, rmcup=\E[?1049l, rmir=\E[4l,
        rmkx=\E[?1l\E>, rmm=\E[?1034l, rmso=\E[27m, rmul=\E[24m,
        rs1=\Ec, rs2=\E[!p\E[?3;4l\E[4l\E>, sc=\E7,
        setab=\E[4%p1%dm, setaf=\E[3%p1%dm,
        setb=\E[4%?%p1%{1}%=%t4%e%p1%{3}%=%t6%e%p1%{4}%=%t1%e%p1%{6}%=%t3%e%p1%d%;m,
        setf=\E[3%?%p1%{1}%=%t4%e%p1%{3}%=%t6%e%p1%{4}%=%t1%e%p1%{6}%=%t3%e%p1%d%;m,
        sgr=%?%p9%t\E(0%e\E(B%;\E[0%?%p6%t;1%;%?%p2%t;4%;%?%p1%p3%|%t;7%;%?%p4%t;5%;%?%p7%t;8%;m,
        sgr0=\E(B\E[m, smacs=\E(0, smam=\E[?7h, smcup=\E[?1049h,
        smir=\E[4h, smkx=\E[?1h\E=, smm=\E[?1034h, smso=\E[7m,
        smul=\E[4m, tbc=\E[3g, u6=\E[%i%d;%dR, u7=\E[6n,
        u8=\E[?1;2c, u9=\E[c, vpa=\E[%i%p1%dd,
I can see the "\E" chars above, and I understand that I can do Ctrl-V followed by the next char to get them into a file. Having tried several things, I can't get it to work, and I'm not even sure which are the F3 and F12 chars. (I recall there's some offset by 1?).

Questions:
1. Is the above infocmp output the place I should get the information I need?
2. What chars do I need to put into my file to simulate me pressing F3 and F12?
3. Is there a way for me to put such chars in a file by just pressing those function keys (I tried a here document, but that doesn't work (it's as if the function keys are not even pressed)).

Thanks heaps!
Tel7
 
Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 09-18-2010, 07:36 AM   #2
tronayne
Senior Member
 
Registered: Oct 2003
Location: Northeastern Michigan, where Carhartt is a Designer Label
Distribution: Slackware 32- & 64-bit Stable
Posts: 3,541

Rep: Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065
The "\E" is escape (the Esc key) that you can get (in vi, say) by typing Ctrl-M then Ctrl-[.

For example, the F1 key (kf1=\EOP) produces escape-upper case letter O-upper case letter P.

Now, to get those into a file you can start with the output of infocmp (which is showing you the definitions of all the functions that xterm "knows" about); if you use
Code:
infocmp -1 > filename
you'll get each capability on an individual line (which you can then edit, replacing the "\E" with the value of the escape key:
Code:
Dec     Hex     Octal   Binary          ASCII
027     01b     0033    00011011        ESC     (Ctrl-[)
As above, you produce "escape" with Ctrl-M followed by Ctrl-[ along with the other characters given by infocmp.

Hope this helps some.
 
2 members found this post helpful.
Old 09-18-2010, 09:48 AM   #3
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
How about doing it automatically by using the script command to capture actual session keystrokes?
 
Old 09-19-2010, 11:53 PM   #4
tel7
LQ Newbie
 
Registered: Sep 2010
Posts: 3

Original Poster
Rep: Reputation: 0
Thanks tronayne,

That "-1" switch of infocmp makes it more readable, thanks.

Ctrl-M doesn't work for me - it just creates a newline (as expected). As mentioned in my last post, I've been using Ctrl-V (it works).
I was doing:
Code:
Ctrl-V <Esc>
for the \E, but I think I was just looking at the wrong code from infocmp. I now see that I can also do:
Code:
Ctrl-V <F3>
which means I don't even need infocmp anymore. And I now notice that Ctrl-V also works outside of vi, so I can use the above when creating a here document, too!


Hi catkin,

Good suggestion about the "script" command. I forgot about it - haven't used it in years. I'm not sure if it's working from the application, because there's so much application stuff that I can't see the wood for the trees. It seems to work from the command line, when I preceed the function keys with Ctrl-V, but it's still messy. The here document seems to give me what I need, except for one thing... It would be easier if I can create the keystroke file while running the application. How can I tell Linux to take my keystrokes, and feed them to the application and to a file (e.g. presumably somehow using the "tee" command)?


So, thanks, both of you, for your help so far.

Despite the good progress, my automated sessions are not working as cleanly as I had hoped. I'm getting errors output from my sessions including things like this:
Code:
  tcgetattr():: Inappropriate ioctl for device
  tcsetattr():: Inappropriate ioctl for device
Any ideas why?

Also, I'd like to slow down the sessions, so I can see what's happening if things go wrong. I tried writing this Perl script (called "slow.pl") which takes the keystroke file as input, and outputs it, one byte per second:
Code:
#!/usr/bin/perl
$|=1;
while ($line = <>)
{
        for ($i=0; $i<length($line); $i++)
        {
                print substr($line,$i,1);
                sleep 1;
        }
}
and running it like this:
slow.pl <keys.in | mypgm.4ae
and it works until it hits a function key, and then it just displays the chars that make up that function key, one at a time, in the field I'm in a the time, instead of just actioning that function key. Is this some multi-byte char issue, or what? Any ideas how I can resolve it, or do I need to go to a Perl forum?

Thanks again.
Terry
 
Old 09-20-2010, 07:34 AM   #5
tronayne
Senior Member
 
Registered: Oct 2003
Location: Northeastern Michigan, where Carhartt is a Designer Label
Distribution: Slackware 32- & 64-bit Stable
Posts: 3,541

Rep: Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065
Yer welcome.

Bear in mind that, if you've got codes in a file, the line terminator is a line feed (ASCII NL, Ctrl-J), not a carriage return (ASCII CR, Ctrl-M); see the chart below for top of a cross-reference (for whatever it may be worth).

Also, unless you display the codes as in the chart; i.e., as decimal, hex or octal, you won't be able to see them -- they'll just do what the function key (or control key) does. And, yeah, the only way you can isolate the shell or enter codes into a file with an editor is to use Ctrl-V followed by the non-printing code you want (pain in the hiney, eh?). I'm not a Perl type, so I dunno how you'd do that with it but it's dirt simple with C -- just do a raw write to send the function key string (or use the printf formatting codes to be able to see the values).

What the function keys do is "print" a series of values, starting with escape, character by character, stopping at a terminator (the terminator is not "printed").
Code:
        Dec     Hex     Octal   Binary          ASCII
        000     000     0000    00000000        NUL     (Ctrl-@)
        001     001     0001    00000001        SOH     (Ctrl-A)
        002     002     0002    00000010        STX     (Ctrl-B)
        003     003     0003    00000011        ETX     (Ctrl-C)
        004     004     0004    00000100        EOT     (Ctrl-D)
        005     005     0005    00000101        ENQ     (Ctrl-E)
        006     006     0006    00000110        ACK     (Ctrl-F)
        007     007     0007    00000111        BEL     (Ctrl-G)
        008     008     0010    00001000        BS      (Ctrl-H)
        009     009     0011    00001001        HT      (Ctrl-I)
        010     00a     0012    00001010        NL      (Ctrl-J)
        011     00b     0013    00001011        VT      (Ctrl-K)
        012     00c     0014    00001100        NP      (Ctrl-L)
        013     00d     0015    00001101        CR      (Ctrl-M)
        014     00e     0016    00001110        SO      (Ctrl-N)
        015     00f     0017    00001111        SI      (Ctrl-O)
        016     010     0020    00010000        DLE     (Ctrl-P)
        017     011     0021    00010001        DC1     (Ctrl-Q)
        018     012     0022    00010010        DC2     (Ctrl-R)
        019     013     0023    00010011        DC3     (Ctrl-S)
        020     014     0024    00010100        DC4     (Ctrl-T)
        021     015     0025    00010101        NAK     (Ctrl-U)
        022     016     0026    00010110        SYN     (Ctrl-V)
        023     017     0027    00010111        ETB     (Ctrl-W)
        024     018     0030    00011000        CAN     (Ctrl-X)
        025     019     0031    00011001        EM      (Ctrl-Y)
        026     01a     0032    00011010        SUB     (Ctrl-Z)
        027     01b     0033    00011011        ESC     (Ctrl-[)
        028     01c     0034    00011100        FS      (Ctrl-\)
        029     01d     0035    00011101        GS      (Ctrl-])
        030     01e     0036    00011110        RS      (Ctrl-^)
        031     01f     0037    00011111        US      (Ctrl-_)                                    
        032     020     0040    00100000        SP      (Ctrl-`)                                    
        033     021     0041    00100001        !                                                   
        034     022     0042    00100010        "                                                   
        035     023     0043    00100011        #                                                   
        036     024     0044    00100100        $
Hope this helps some.
 
Old 09-23-2010, 05:00 PM   #6
tel7
LQ Newbie
 
Registered: Sep 2010
Posts: 3

Original Poster
Rep: Reputation: 0
Thanks for that, tronayne.

BTW, in regard to my slow.pl script, I found that printing all the bytes for a given function key in one hit, solved the problem.

Thanks again for your help.
TRS
 
  


Reply



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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Japanese chars in File Names Dankles Slackware 5 03-24-2009 06:53 AM
bash: how to replace strings of a file with some " chars ? frenchn00b Linux - General 1 03-01-2008 09:10 AM
Function Key eggoz Linux - Software 0 06-28-2006 08:34 AM
RHEL 3 - Keyboard send several chars when key pressed once lmx Red Hat 1 01-29-2005 11:46 PM
German umlaute (special chars) in file system steltner Linux - General 5 10-27-2003 02:07 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 06:11 AM.

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