M BUZZ CRAZE NEWS
// general

How to get tty to recognise shift-F2 (and other key combinations)?

By Mia Morrison

Firstly, apologies if my question or terminology doesn't make sense - I'm still wrapping my head around terminal, console, shell etc.

I can use the Shift+F2 shortcut in an application in a terminal running on the desktop, but I can't use it in a tty terminal. Some other shortcuts don't work either, such as Ctrl+F6. Using Ctrl+V followed by F1 to print its character value, I can see that they differ - in a desktop terminal with TERM=xterm-256color, Shift+F2 returns ^[[1;2Q whereas in tty1 with TERM=linux it returns ^[[26~. Setting tty to appear as xterm with export TERM=xterm-256color makes the F keys return letters (without even pressing Ctrl+V first), and pressing Shift+F2 simply prints ~.

Is there any way to have the tty virtual terminals recognise all the same shortcuts and key combinations that the X terminal does?

1 Answer

First, you should not set TERM=xterm-256color in the Linux console, most apps will fail to work properly. Stick with TERM=linux.

Second, let's clarify that altering the value of TERM does not change the escape sequences generated by the keypress. The graphical emulator, or the Linux kernel in case you're using the console, cannot see if you alter the environment variable in the process running "inside" the terminal emulator; in fact, there might be multiple processes running in that terminal (their standard input connected there) with different TERM variables. Changing TERM might change though how an application reacts on those keypresses.

Is there any way to have the tty virtual terminals recognise all the same shortcuts and key combinations that the X terminal does?

Terminal emulators don't recognize these sequences you're talking about. They emit them, and the terminal-based applications (text editors, file managers etc.) recognize them.

Run infocmp (or infocmp [terminal-name] such as e.g. infocmp linux) to list the capabilities/features of a terminal, including the sequences generated by function keys. Look for kf2 for F2, and kf14 for Shift+F2. Generally Shift should result in an offset of 12.

As far as I recall from a couple of years ago, some of the keymaps on the Linux console have an offset of 10 instead, and this discrepancy potentially causes all sorts of misbehavior in various apps.

You didn't mention how (e.g. in what app) you're trying to catch these keypresses. If you're configuring an existing piece of software which requires concrete sequences, you'll probably have to teach it (at least) two different values, one for xterm and friends, and one for linux.

If you are developing your own app and don't want to have escape sequences hardcoded or specified in a config file, you should probably rely on libtinfo to read the terminfo capabilities, or use a higher level screen drawing and keyboard handling library such as ncurses or slang.

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy