Why does XTerm support 63 function keys

keyboardterminalxterm

According to this table of terminal emulators and their keycodes, new versions of XTerm recognize function keys from F1, F2, F3, … all the way up to F63. Most keyboards only have physical function keys in the range F1..F12. What is the purpose of the extra keys? Is there some (semi-)standard mapping from physical key or mouse presses to those function key numbers, and if not, how are they triggered?

I'm under the impression that in traditional terminal parlance, all special keys including the arrow keys, home/end, etc. were called function keys (whereas those of us who grew up on PCs would consider only F1..F12 to be function keys). But then, XTerm also talks separately about these named keys (e.g. kUP for the up arrow key in that table).

Best Answer

DEC VTs represent function keys and various cursor and editing keys with a control sequence named DECFNK, which is CSI followed by one or two parameters followed by ~. The first parameter is the function key number. The second parameter indicates active modifier key states.

As far as DECFNK is concerned, there can be up to 20 function keys. The DECFNK codes for function keys 1 to 20 are well-defined and documented. There were, after all, physically 20 function keys on the LK keyboards. (Even if several function keys usually did not, they could potentially all generate DECFNK sequences, and the sequences are in the DEC doco.) Here's an LK401 keyboard for a DEC VT420:

An LK401 keyboard

As you can see in your table, the emulators that produce the original DECFNK sequences emit DECFNK 11 to DECFNK 34 for these 20 keys. (Some of the DECFNK numbers are skipped. Interestingly, the skipped numbers correspond to the physical spaces between the key groups of the function key row on the keyboard.)

Where things get complex is where the PC and XENIX were introduced. In the world of the IBM PC, and in particular the PC 3270, there were 24 function keys on the Model F keyboards. The PC/AT's Enhanced keyboard had 12 function keys, half that. Here is a 3270 Model F keyboard:

A 122-key IBM Model F keyboard

Terminal emulation conventions arose that the function keys from F13 to F24 could be accessed from a PC/AT keyboard by using combinations of modifiers. With the four combinations of ⎈ Control and ⇧ Level 2 Shift, the mere 12 function keys on an Enhanced keyboard could be made to simulate 48 function keys. This was the case for the XENIX console, which generated 48 distinct control sequences.

You will observe in the other columns of that table that that is what is happening. The control sequences for (say) F6, F18, F30, and F42 are all DECFNK 17 with different values for the modifier bitflags in the second parameter.

Also observe that F1 to F5 are not DECFNK control sequences in those same columns. This is again down to emulator conventions on PC keyboards. On the DEC LK keyboards, F1 to F5 did have assigned DECFNK sequences and could generate them, but usually operated in a "local" mode where they did not generate input to the host. Emulators did not need to generate these DECFNK sequences and emulate F1 to F5, because few to no applications relied upon these keys as a consequence; but emulators conversely did need to emulate a set of PF1 to PF4 keys that were on the calculator keypad in an actual DEC keyboard, but that PC keyboards did not have. These keys did not generate DECFNK sequences, and weren't true function keys. They generated SS3 control sequences, like the other calculator keys.

(Some of the SS3 sequences are malformed. "SS" stands for "Single Shift" and such shifts apply to a single following character. The XTerm control sequences that append modifier parameters to SS3 are not in fact correctly formed SS3 sequences. Real DEC terminals did not work this way. Modifiers were not reported for calculator key control sequences.)

This explains how the function key control sequence set for emulated terminals can go as high as 48 function keys. To explain 64 control sequences, one simply needs to notice the modifier parameter in the DECFNK sequences. For the additional 12 control sequences it is 3, which denotes the ⎇ Alt modifier.

In fact, there are 8 possible combinations of ⎈ Control, ⇧ Level 2 Shift, and ⎇ Alt; the three modifiers recognized by the DEC VT control sequence protocol. There can thus be 96 different DECFNK control sequences per these terminal emulator conventions from a PC/AT Enhanced keyboard, and 160 from a DEC LK keyboard.

Further reading

Related Question