How to get the F1-F12 keys to switch screens in gnu screen in cygwin when connecting via SSH


I'm connecting to a desktop running cygwin via SSH from the terminal app in Mac OS X. I have already started screen on the cygwin side and can connect to it over the SSH session. Furthermore, I have the following in the .screenrc file:

bindkey -k k1 select 1  #  F1 = screen 1
bindkey -k k2 select 2  #  F2 = screen 2
bindkey -k k3 select 3  #  F3 = screen 3
bindkey -k k4 select 4  #  F4 = screen 4
bindkey -k k5 select 5  #  F5 = screen 5
bindkey -k k6 select 6  #  F6 = screen 6
bindkey -k k7 select 7  #  F7 = screen 7
bindkey -k k8 select 8  #  F8 = screen 8
bindkey -k k9 select 9  #  F9 = screen 9
bindkey -k F1 prev      # F11 = prev
bindkey -k F2 next      # F12 = next

However, when I start multiple windows in screen and attempt to switch between them via the function keys, all I get is a beep.

I have tried various settings for $TERM (e.g. ansi, cygwin, xterm-color, vt100) and they don't really seem to affect anything.

I have verified that the terminal app is in fact sending the escape sequence for the function key that I'm expecting and that my bash shell (running inside screen) is receiving it. For example, for F1, it sends the following (hexdump is a perl script I wrote that takes STDIN in binmode and outputs it as a hexadecimal/ascii dump):

% hexdump
[press F1 and then hit ^D to terminate input]
00000000:  1b4f50                               .OP

If things were working correctly, I don't think bash should receive the escape sequence because screen should have caught it and turned it into a command.

How do I get the function keys to work?

Best Answer

With a great deal of experimentation, I was able to get it to work by adding the following lines to my .screenrc:

terminfo * k1=\EOP
terminfo * k2=\EOQ
terminfo * k3=\EOR
terminfo * k4=\EOS
terminfo * k5=\E[15~
terminfo * k6=\E[17~
terminfo * k7=\E[18~
terminfo * k8=\E[19~
terminfo * k9=\E[20~
terminfo * F1=\E[23~
terminfo * F2=\E[24~