Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I am trying to make an ahk for putty to send the keycodes that I want for some key combinations in order for my program to work over terminal too.

For this I have an ahk already with some key combinations working properly by experimenting awfully much time from here and there, from key tables, etc but I still don't get, did not came up with a clear, logical method to calculate the escape key that I want.

An example:

^F1::SendInput ^[O5P

It gives 28 in my test prog.

I see that for ^[1 I get 377 and for ^[2 376, and I see that letters out of hexadecimal numbers (A-F) can be used, as well as ; and ~ or double [[.

Do you understand how this works? Any good descriptive material for this? Thanks a lot!

share|improve this question

1 Answer 1

up vote 2 down vote accepted

If you want to "send" a function key use the appropriate syntax

^F1::SendInput {F5}

That would cause Putty to act as if you had pressed F5 and it will then send an appropriate sequence of characters (an Escape Sequence) to the server.

               PC                                         Server
   +---------------------+   Network     +-------------------------+
   | [AHK] ----> [Putty] | ============> | [TTY] ------> [Program] |
   |        F5           |  Esc [ 15 ~   |        Esc…    |   ^    |
   +---------------------+               |                |   | F5 |
                                         |                v   |    |
                                         |    $TERM------[Curses]  |
                                         |                 |       |
                                         |    /usr/share/terminfo  |
                                         +-------------------------+

At the server, the Escape sequence is first processed by the TTY handler (looking for Interrupt signals etc) and then given to your server program which typuically hands it to the curses library which translates Escape sequences back into names of Functions. The program then decides what to do as a result of receiving that.

The actual Escape Sequence depends on the "terminal emulation". By default, Putty emulates an xterm. The Curses library uses the value of environment variable TERM to look up in terminfo what function is represented by an escape sequence.


Escape sequences for function keys can be found in /etc/termcap, here's a few

                                    SCO     PuTTY    PuTTY
  Key    Termcap  VT100    VT220    ANSI    XTERM    VT400
  ---    -------  -------  -------  ------  ------   -------
  F1     k1       Esc[OP   Esc[OP   Esc[M   Esc[OP   Esc[11~
  F2     k2       Esc[OQ   Esc[OQ   Esc[N   Esc[OQ   Esc[12~
  F3     k3       Esc[OR   Esc[OR   Esc[O   Esc[OR   Esc[13~
  F4     k4       Esc[OS   Esc[OS   Esc[P   Esc[OS   Esc[14~
  F5     k5       -        -        Esc[Q   Esc[15~  Esc[15~
  F6     k6       -        Esc[17~  Esc[R   Esc[17~  Esc[17~
  F7     k7       -        Esc[18~  Esc[S   Esc[18~  Esc[18~
  ...
  F10    k0       -        Esc[21~  Esc[V   Esc[21~  Esc[21~
  F11    k1       -        Esc[23~  Esc[X   Esc[23~  Esc[23~
  ...
  F14    kD       -        Esc[26~  Esc[Z   Esc[26~  Esc[26~
  F15    kE       -        Esc[27~  Esc[a   Esc[27~  Esc[27~
  ...
  F36    FP       -
  ...
  F63    Fr       -

Notes

  • The VT100 series had only four function keys labelled PF1 to PF4
  • The VT220 had 20 function keys labelled up to F20
  • The VT220 used a function key at position 5 for a "break" function. (hence gap above)
  • Not all emulators or extant termcap/terminfo files agree about the above.
  • There are strange gaps in the progression.
  • xterm is not a definition, it's an unruly family of misfits, no two alike.
  • Ditto "ANSI"
  • Many emulators use something like Shift+F1 for F13, Alt+F1 for F25 etc.
  • In PuTTY, shift+F1 is the same as F11 (not F13)
  • Yes it's a mess. That's why we let PuTTY and Terminfo hide it from us.
share|improve this answer
    
I knew the pure {specialKey} syntax only that I need key combinations. For ex.: +F1::SendInput,^6s{F1} and ^F1::SendInput,^6c{F1} –  obeliksz Oct 24 '12 at 13:04
    
What I dont get is how this escape code is composed/works: it decreases as I increase a number in code (see example in the question). Is it possible to do an addition/substraction for a {key}? For example: {Enter-3} I get the code 13 from my test program, I want to send a signal that returnes 10. –  obeliksz Oct 24 '12 at 13:21
    
According to your figure I would think that changing the xterm terminfo should change the terminal's reactions to the changed escape code? I tried this (modified terminfo and recomplied with tic) but it didn't applied my modification at least it was of no effect. Was I just mistaking the terminfo file? I think I changed more xterm terminfo files if not all as TERM returned xterm. –  obeliksz Oct 24 '12 at 13:26
    
@obelisk: A) terminals don't react to codes sent to the server (only those sent from the server). B) Why are you trying to use AHK to send Escape sequences to PuTTY? Send Function Key presses to Putty, let Putty decide what Escape sequence to send. C) There is only one "xterm" file in the Terminfo library (exact spelling). –  RedGrittyBrick Oct 24 '12 at 14:56
    
I'm using AHK because otherwise I cant make putty to react to my key combinations. I accepted your answer because you explained the how it works well but it doesn't work for me... I modified the xterm from usr/share/teminfo/x/xterm... modified kf1 from /EOP to /EOX and tic recomplied it but I don't see the effect... I tested with xterm, TERM returned xterm. For cat F1 I got before EOP and still I get just EOP. This is one part, the other would be to get the escape code I want, how these letters/numbering adds up –  obeliksz Oct 25 '12 at 5:26

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.