Difference between revisions of "Linux console/Keyboard configuration"

From ArchWiki
Jump to navigation Jump to search
(→‎Other settings: There is only one "other setting" left. So change it to level 2.)
m (update ES interlanguage link)
 
(69 intermediate revisions by 17 users not shown)
Line 1: Line 1:
[[Category:Keyboards]]
+
[[Category:Linux console]]
[[es:Keyboard Configuration in Console]]
+
[[Category:Keyboard configuration]]
[[ja:Keyboard Configuration in Console]]
+
[[es:Linux console (Español)/Keyboard configuration]]
[[zh-CN:Keyboard Configuration in Console]]
+
[[fr:Keyboard]]
 +
[[ja:コンソールでのキーボード設定]]
 +
[[pt:Linux console (Português)/Keyboard configuration]]
 +
[[ru:Linux console (Русский)/Keyboard configuration]]
 +
[[zh-hans:Keyboard configuration in console]]
 
{{Related articles start}}
 
{{Related articles start}}
{{Related|Keyboard configuration in Xorg}}
+
{{Related|Xorg/Keyboard configuration}}
{{Related|Extra keyboard keys}}
+
{{Related|Keyboard input}}
{{Related|Fonts#Console fonts}}
+
{{Related|Linux console#Fonts}}
 
{{Related articles end}}
 
{{Related articles end}}
 
{{Note|This article covers only basic configuration without modifying layouts, mapping extra keys etc. See [[Extra keyboard keys]] for these advanced topics.}}
 
 
 
Keyboard mappings (keymaps) for [[Wikipedia:Virtual console|virtual console]], console fonts and console maps are provided by the {{Pkg|kbd}} package (a dependency of [[systemd]]), which also provides many low-level tools for managing virtual console. In addition, ''systemd'' also provides the ''localectl'' tool, which can control both the system [[locale]] and keyboard layout settings for both the virtual console and Xorg.
 
Keyboard mappings (keymaps) for [[Wikipedia:Virtual console|virtual console]], console fonts and console maps are provided by the {{Pkg|kbd}} package (a dependency of [[systemd]]), which also provides many low-level tools for managing virtual console. In addition, ''systemd'' also provides the ''localectl'' tool, which can control both the system [[locale]] and keyboard layout settings for both the virtual console and Xorg.
  
 
== Viewing keyboard settings ==
 
== Viewing keyboard settings ==
  
You can use the following command to view the current keyboard configurations, amongst other localised settings:
+
Use {{ic|localectl status}} to view the current keyboard configurations.
 
 
{{hc|$ localectl status|<nowiki>
 
  System Locale: LANG=en_GB.utf8
 
                  LC_COLLATE=C
 
      VC Keymap: cz-qwertz
 
      X11 Layout: cz
 
</nowiki>}}
 
  
== Setting keyboard layout ==
+
== Keymaps ==
  
=== Keymap codes ===
+
The keymap files are stored in the {{ic|/usr/share/kbd/keymaps/}} directory tree. Usually one keymap file corresponds to one keyboard layout (the {{ic|include}} statement can be used to share common parts and a keymap file can contain multiple layouts with some key combination used for switching). For more details see {{man|5|keymaps}}.
  
Usually one keymap file corresponds to one keyboard layout (the {{ic|include}} statement can be used to share common parts and a keymap file can contain multiple layouts with some key combination used for switching). The keymap files are stored in the {{ic|/usr/share/kbd/keymaps/}} directory tree.
+
=== Listing keymaps ===
  
 
The naming conventions of console keymaps are somewhat arbitrary, but usually they are based on:
 
The naming conventions of console keymaps are somewhat arbitrary, but usually they are based on:
Line 43: Line 37:
  
 
  $ localectl list-keymaps | grep -i ''search_term''
 
  $ localectl list-keymaps | grep -i ''search_term''
 +
 +
Alternatively, using find:
 +
 +
$ find /usr/share/kbd/keymaps/ -type f
 +
 +
=== Loadkeys ===
 +
 +
It is possible to set a keymap just for current session. This is useful for testing different keymaps, solving problems etc.
 +
 +
The ''loadkeys'' tool is used for this purpose, it is used internally by [[systemd]] when loading the keymap configured in {{ic|/etc/vconsole.conf}}. It can be used very simply for this purpose:
 +
 +
# loadkeys ''keymap''
 +
 +
See {{man|1|loadkeys}} details.
  
 
=== Persistent configuration ===
 
=== Persistent configuration ===
  
A persistent keymap can be set in {{ic|/etc/vconsole.conf}}, which is read by [[systemd]] on start-up. The {{ic|KEYMAP}} variable is used for specifying the keymap. If the variable is empty or not set, the {{ic|us}} keymap is used as default value. See {{ic|man 5 vconsole.conf}} for all options. For example:
+
A persistent keymap can be set in {{ic|/etc/vconsole.conf}}, which is read by [[systemd]] on start-up. The {{ic|KEYMAP}} variable is used for specifying the keymap. If the variable is empty or not set, the {{ic|us}} keymap is used as default value. See {{man|5|vconsole.conf}} for all options. For example:
  
 
{{hc|/etc/vconsole.conf|<nowiki>
 
{{hc|/etc/vconsole.conf|<nowiki>
Line 57: Line 65:
 
  $ localectl set-keymap --no-convert ''keymap''
 
  $ localectl set-keymap --no-convert ''keymap''
  
The {{ic|--no-convert}} option can be used to prevent {{ic|localectl}} to automatically change the [[Keyboard configuration in Xorg|Xorg keymap]] to the nearest match. See {{ic|man localectl}} for more information.
+
The {{ic|--no-convert}} option can be used to prevent {{ic|localectl}} from automatically changing the [[Keyboard configuration in Xorg|Xorg keymap]] to the nearest match. See {{man|1|localectl}} for more information.
 +
 
 +
If required, the keymap from {{ic|/etc/vconsole.conf}} can be loaded during early userspace by the {{ic|keymap}} [[mkinitcpio#Common hooks|mkinitcpio hook]].
 +
 
 +
=== Creating a custom keymap ===
 +
 
 +
When using the console, you can use hotkeys to print a specific character. Moreover we can also print a sequence of characters and some escape sequences. Thus, if we print the sequence of characters constituting a command and afterwards an escape character for a new line, that command will be executed.
 +
 
 +
One method of doing this is editing the keymap file. However, since it will be rewritten anytime the package it belongs to is updated, editing this file is discouraged. It is better to integrate the existing keymap with a personal keymap. The {{ic|loadkeys}} utility can do this.
 +
 
 +
First, create a keymap file. This keymap file can be anywhere, but one method is to mimic the directory hierarchy in {{ic|/usr/local}}:
 +
 
 +
# mkdir -p /usr/local/share/kbd/keymaps
 +
# vim /usr/local/share/kbd/keymaps/personal.map
 +
 
 +
As a side note, it is worth noting that such a personal keymap is useful also to redefine the behaviour of keys already treated by the default keymap: when loaded with {{ic|loadkeys}}, the directives in the default keymap will be replaced when they conflict with the new directives and conserved otherwise. This way, only changes to the keymap must be specified in the personal keymap.
 +
 
 +
{{Tip|You can also edit an existing keymap located in the {{ic|/usr/share/kbd/keymaps/}} directory tree. Keymaps have an ''.map.gz'' extension, for example {{ic|us.map.gz}} is an American keymap. Just copy the keymap to {{ic|/usr/local/share/kbd/keymaps/personal.map.gz}} and ''gunzip'' it.}}
 +
 
 +
==== Adding directives ====
 +
 
 +
Two kinds of directives are required in this personal keymap. First of all, the [[Extra keyboard keys|keycode]] directives, which matches the format seen in the default keymaps. These directives associate a keycode with a keysym. Keysyms represent keyboard actions. The actions available include outputting character codes or character sequences, switching consoles or keymaps, booting the machine, and many other actions. The full currently active keymap can be obtained with
 +
 
 +
# dumpkeys -l
 +
 
 +
Most keysyms are intuitive. For example, to set key 112 to output an 'e', the directive will be:
 +
 
 +
keycode 112  = e
 +
 
 +
To set key 112 to output a euro symbol, the directive will be:
 +
 
 +
keycode 112 = euro
 +
 
 +
Some keysym are not immediately connected to a keyboard actions. In particular, the keysyms prefixed by a capital F and one to three digits (F1-F246) constituting a number greater than 30 are always free. This is useful directing a hotkey to output a sequence of characters and other actions:
 +
 
 +
keycode 112 = F70
 +
 
 +
Then, F70 can be bound to output a specific string:
 +
 
 +
string F70 = "Hello"
 +
 
 +
When key 112 is pressed, it will output the contents of F70. In order to execute a printed command in a terminal, a newline escape character must be appended to the end of the command string. For example, to enter a system into hibernation, the following keymap is added:
 +
 
 +
string F70 = "sudo /usr/sbin/hibernate\n"
 +
 
 +
==== Other examples ====
 +
 
 +
* To make the Right Alt key same as Left Alt key (for Emacs), use the following line in your keymap. It will include the file {{ic|/usr/share/kbd/keymaps/i386/include/linux-with-two-alt-keys.inc}}, check it for details.
 +
 
 +
include "linux-with-two-alt-keys"
  
=== Temporary configuration ===
+
* To swap CapsLock with Escape (for Vim), remap the respective keycodes:
  
Of course it is possible to set a keymap just for current session. This is useful for testing different keymaps, solving problems etc.
+
keycode 1 = Caps_Lock
 +
keycode 58 = Escape
  
The ''loadkeys'' tool is used for this purpose, it is used internally by [[systemd]] when loading the keymap configured in {{ic|/etc/vconsole.conf}}. It can be used very simply for this purpose:
+
* To make CapsLock another Control key, remap the respective keycode:
 +
 
 +
keycode 58 = Control
 +
 
 +
* To swap CapsLock with Left Control key, remap the respective keycodes:
 +
 
 +
keycode 29 = Caps_Lock
 +
keycode 58 = Control
 +
 
 +
==== Saving changes ====
 +
 
 +
In order to make use of the personal keymap, it must be loaded with ''loadkeys'':
  
  # loadkeys ''keymap''
+
  # loadkeys /usr/local/share/kbd/keymaps/personal.map
  
See {{ic|man 1 loadkeys}} details.
+
However this keymap is only active for the current session. In order to load the keymap at boot, specify the full path to the file in the {{ic|KEYMAP}} variable in [[#Persistent configuration|/etc/vconsole.conf]]. The file does not have to be gzipped as the official keymaps provided by {{Pkg|kbd}}.
  
 
== Adjusting typematic delay and rate ==
 
== Adjusting typematic delay and rate ==
  
The ''typematic delay'' indicates the amount of time (typically in miliseconds) a key needs to be pressed in order for the repeating process to begin. After the repeating process has been triggered, the character will be repeated with a certain frequency (usually given in Hz) specified by the ''typematic rate''. These values can be changed using the ''kbdrate'' command. Note that the typematic delay in the virtual console is different from the [[Keyboard_configuration_in_Xorg#Adjusting_typematic_delay_and_rate|typematic delay in Xorg]].
+
The ''typematic delay'' indicates the amount of time (typically in milliseconds) a key needs to be pressed and held in order for the repeating process to begin. After the repeating process has been triggered, the character will be repeated with a certain frequency (usually given in Hz) specified by the ''typematic rate''. These values can be changed using the ''kbdrate'' command. Note that these settings are configured separately for the virtual console and [[Keyboard configuration in Xorg#Adjusting typematic delay and rate|for Xorg]].
  
 
  # kbdrate [-d ''delay''] [-r ''rate'']
 
  # kbdrate [-d ''delay''] [-r ''rate'']
Line 93: Line 162:
  
 
[Service]
 
[Service]
Type=simple
+
Type=oneshot
 
RemainAfterExit=yes
 
RemainAfterExit=yes
 
StandardInput=tty
 
StandardInput=tty
Line 103: Line 172:
 
</nowiki>}}
 
</nowiki>}}
  
$ systemctl enable kbdrate.service
+
Then [[start/enable]] the {{ic|kbdrate.service}} systemd service.
$ systemctl start kbdrate.service
 

Latest revision as of 13:47, 19 September 2019

Keyboard mappings (keymaps) for virtual console, console fonts and console maps are provided by the kbd package (a dependency of systemd), which also provides many low-level tools for managing virtual console. In addition, systemd also provides the localectl tool, which can control both the system locale and keyboard layout settings for both the virtual console and Xorg.

Viewing keyboard settings

Use localectl status to view the current keyboard configurations.

Keymaps

The keymap files are stored in the /usr/share/kbd/keymaps/ directory tree. Usually one keymap file corresponds to one keyboard layout (the include statement can be used to share common parts and a keymap file can contain multiple layouts with some key combination used for switching). For more details see keymaps(5).

Listing keymaps

The naming conventions of console keymaps are somewhat arbitrary, but usually they are based on:

For a list of all the available keymaps, use the command:

$ localectl list-keymaps

To search for a keymap, use the following command, replacing search_term with the code for your language, country, or layout:

$ localectl list-keymaps | grep -i search_term

Alternatively, using find:

$ find /usr/share/kbd/keymaps/ -type f

Loadkeys

It is possible to set a keymap just for current session. This is useful for testing different keymaps, solving problems etc.

The loadkeys tool is used for this purpose, it is used internally by systemd when loading the keymap configured in /etc/vconsole.conf. It can be used very simply for this purpose:

# loadkeys keymap

See loadkeys(1) details.

Persistent configuration

A persistent keymap can be set in /etc/vconsole.conf, which is read by systemd on start-up. The KEYMAP variable is used for specifying the keymap. If the variable is empty or not set, the us keymap is used as default value. See vconsole.conf(5) for all options. For example:

/etc/vconsole.conf
KEYMAP=uk
...

For convenience, localectl may be used to set console keymap. It will change the KEYMAP variable in /etc/vconsole.conf and also set the keymap for current session:

$ localectl set-keymap --no-convert keymap

The --no-convert option can be used to prevent localectl from automatically changing the Xorg keymap to the nearest match. See localectl(1) for more information.

If required, the keymap from /etc/vconsole.conf can be loaded during early userspace by the keymap mkinitcpio hook.

Creating a custom keymap

When using the console, you can use hotkeys to print a specific character. Moreover we can also print a sequence of characters and some escape sequences. Thus, if we print the sequence of characters constituting a command and afterwards an escape character for a new line, that command will be executed.

One method of doing this is editing the keymap file. However, since it will be rewritten anytime the package it belongs to is updated, editing this file is discouraged. It is better to integrate the existing keymap with a personal keymap. The loadkeys utility can do this.

First, create a keymap file. This keymap file can be anywhere, but one method is to mimic the directory hierarchy in /usr/local:

# mkdir -p /usr/local/share/kbd/keymaps
# vim /usr/local/share/kbd/keymaps/personal.map

As a side note, it is worth noting that such a personal keymap is useful also to redefine the behaviour of keys already treated by the default keymap: when loaded with loadkeys, the directives in the default keymap will be replaced when they conflict with the new directives and conserved otherwise. This way, only changes to the keymap must be specified in the personal keymap.

Tip: You can also edit an existing keymap located in the /usr/share/kbd/keymaps/ directory tree. Keymaps have an .map.gz extension, for example us.map.gz is an American keymap. Just copy the keymap to /usr/local/share/kbd/keymaps/personal.map.gz and gunzip it.

Adding directives

Two kinds of directives are required in this personal keymap. First of all, the keycode directives, which matches the format seen in the default keymaps. These directives associate a keycode with a keysym. Keysyms represent keyboard actions. The actions available include outputting character codes or character sequences, switching consoles or keymaps, booting the machine, and many other actions. The full currently active keymap can be obtained with

# dumpkeys -l

Most keysyms are intuitive. For example, to set key 112 to output an 'e', the directive will be:

keycode 112  = e

To set key 112 to output a euro symbol, the directive will be:

keycode 112 = euro

Some keysym are not immediately connected to a keyboard actions. In particular, the keysyms prefixed by a capital F and one to three digits (F1-F246) constituting a number greater than 30 are always free. This is useful directing a hotkey to output a sequence of characters and other actions:

keycode 112 = F70

Then, F70 can be bound to output a specific string:

string F70 = "Hello"

When key 112 is pressed, it will output the contents of F70. In order to execute a printed command in a terminal, a newline escape character must be appended to the end of the command string. For example, to enter a system into hibernation, the following keymap is added:

string F70 = "sudo /usr/sbin/hibernate\n"

Other examples

  • To make the Right Alt key same as Left Alt key (for Emacs), use the following line in your keymap. It will include the file /usr/share/kbd/keymaps/i386/include/linux-with-two-alt-keys.inc, check it for details.
include "linux-with-two-alt-keys"
  • To swap CapsLock with Escape (for Vim), remap the respective keycodes:
keycode 1 = Caps_Lock
keycode 58 = Escape
  • To make CapsLock another Control key, remap the respective keycode:
keycode 58 = Control
  • To swap CapsLock with Left Control key, remap the respective keycodes:
keycode 29 = Caps_Lock
keycode 58 = Control

Saving changes

In order to make use of the personal keymap, it must be loaded with loadkeys:

# loadkeys /usr/local/share/kbd/keymaps/personal.map

However this keymap is only active for the current session. In order to load the keymap at boot, specify the full path to the file in the KEYMAP variable in /etc/vconsole.conf. The file does not have to be gzipped as the official keymaps provided by kbd.

Adjusting typematic delay and rate

The typematic delay indicates the amount of time (typically in milliseconds) a key needs to be pressed and held in order for the repeating process to begin. After the repeating process has been triggered, the character will be repeated with a certain frequency (usually given in Hz) specified by the typematic rate. These values can be changed using the kbdrate command. Note that these settings are configured separately for the virtual console and for Xorg.

# kbdrate [-d delay] [-r rate]

For example to set a typematic delay to 200ms and a typematic rate to 30Hz, use the following command:

# kbdrate -d 200 -r 30

Issuing the command without specifying the delay and rate will reset the typematic values to their respective defaults; a delay of 250ms and a rate of 11Hz:

# kbdrate

Systemd service

A systemd service can be used to set the keyboard rate. For example

/etc/systemd/system/kbdrate.service

[Unit]
Description=Keyboard repeat rate in tty.

[Service]
Type=oneshot
RemainAfterExit=yes
StandardInput=tty
StandardOutput=tty
ExecStart=/usr/bin/kbdrate -s -d 450 -r 60
 
[Install]
WantedBy=multi-user.target

Then start/enable the kbdrate.service systemd service.