At the moment I have a keyboard which lacks function keys (F1 through F12), and I was wondering – is there a way I could switch to textual TTYs (and back, which might prove to be a problem if I were to use xmodmap
)?
Linux X11 Keyboard Console – How to Switch to TTYs Without Function Keys
consolekeyboardlinuxx11
Related Solutions
First find the scancode of the key that needs to be remapped, e.g. with the evtest
utility. A line like the following one (with MSC_SCAN
in it) should be output:
Event: time 1417131619.686259, type 4 (EV_MSC), code 4 (MSC_SCAN), value 70068
followed by a second one giving the current key code. If no MSC_SCAN
line is output, this is due to a kernel driver bug, but the scancode can still be found with the input-kbd
utility; evtest
should have given the key code, so that it should be easy to find the corresponding line in the input-kbd
output (e.g. by using grep
).
Once the scancodes of the keys to be remapped have been determined, create a file such as /etc/udev/hwdb.d/98-custom-keyboard.hwdb
containing the remappings. The beginning of the file /lib/udev/hwdb.d/60-keyboard.hwdb
gives some information. In my case (which works), I have:
evdev:input:b0003v05ACp0221*
KEYBOARD_KEY_70035=102nd # Left to z: backslash bar
KEYBOARD_KEY_70064=grave # Left to 1: grave notsign
KEYBOARD_KEY_70068=insert # F13: Insert
(Before udev 220, I had to use keyboard:usb:v05ACp0221*
for the first line.)
The evdev:
string must be at the beginning of the line.
Note that the letters in the vendor and product id should be capital letters.
Each KEYBOARD_KEY_
settings should have exactly one space before (note: a line with no spaces will give an error message, and a line with two spaces were silently ignored with old udev versions). KEYBOARD_KEY_
is followed by the scancode in hexadecimal (like what both evtest
and input-kbd
give). Valid values could be obtained from either the evtest
output or the input-kbd
output, or even from the /usr/include/linux/input.h
file: for instance, KEY_102ND
would give 102nd
(by removing KEY_
and converting to lower case), which I used above.
After the file is saved, type:
udevadm hwdb --update
to (re)build the database /etc/udev/hwdb.bin
(you can check its timestamp). Then,
udevadm trigger --sysname-match="event*"
will take the new settings into account. You can check with evtest
.
In 2014, the released udev had incomplete/buggy information in /lib/udev/hwdb.d/60-keyboard.hwdb
, but you can look at the latest development version of the file and/or my bug report and discussion concerning the documentation and spacing issues.
If this doesn't work, the problem might be found after temporarily increasing the log level of udevd
with udevadm control
(see the udevadm(8) man page for details).
For old udev
versions such as 204, this method should still work.
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:
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:
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.
Best Answer
Assuming Linux, you can use chvt (as in
sudo chvt N
) to switch virtual terminal, whereN
is the number corresponding to the VT to activate. For example,sudo chvt 1
for the first VT, andsudo chvt 7
for the VT where you will most often find X running.Note that chvt requires root privileges to do its thing, hence the sudo is necessary unless you are already logged in as root.
On Debian, chvt is provided by the packages
kbd
andconsole-tools
; mine is the one from console-tools. Other distributions are likely similar.By the way, this took me about 15 seconds to find by typing
linux remap virtual terminal switch
into Google. :-) I was expecting to find something to actually remap the switch to another key combination, but chvt answers your question just as well.