「Linux コンソール/キーボード設定」の版間の差分

提供: ArchWiki
ナビゲーションに移動 検索に移動
(同期)
25行目: 25行目:
 
</nowiki>}}
 
</nowiki>}}
   
== キーボードレイアウトの設定 ==
+
== キーマップ ==
  +
  +
=== キーマップの一覧表示 ===
   
 
複数のコンポーネントによって構成されている [[XKB]] キーボードレイアウトとは異なり、仮想コンソールのキーボードレイアウトはコンポーネントがひとつしかありません。基本的にはひとつの keymap ファイルがひとつのキーボードレイアウトに対応しています (''include'' ステートメントを使って共有の部分を共有することで keymap ファイルには複数のレイアウトを含めることができます)。keymap ファイルは {{ic|/usr/share/kbd/keymaps/}} ディレクトリツリーに保存されています。次のコマンドを使うことで利用可能な全てのキーマップを表示できます:
 
複数のコンポーネントによって構成されている [[XKB]] キーボードレイアウトとは異なり、仮想コンソールのキーボードレイアウトはコンポーネントがひとつしかありません。基本的にはひとつの keymap ファイルがひとつのキーボードレイアウトに対応しています (''include'' ステートメントを使って共有の部分を共有することで keymap ファイルには複数のレイアウトを含めることができます)。keymap ファイルは {{ic|/usr/share/kbd/keymaps/}} ディレクトリツリーに保存されています。次のコマンドを使うことで利用可能な全てのキーマップを表示できます:
36行目: 38行目:
   
 
$ find /usr/share/kbd/keymaps/ -type f
 
$ find /usr/share/kbd/keymaps/ -type f
  +
  +
=== Loadkeys ===
  +
  +
現在のセッションでのみ有効なキーマップを設定することもできます。異なるキーマップを試したり、問題を解決したりするときに有用です。
  +
  +
一時的な設定を行うには ''loadkeys'' ツールを使います。このツールは [[systemd|systemd]] によって {{ic|/etc/vconsole.conf}} に設定されたキーマップをロードするために内部的に使用されています。とても簡単に使えます:
  +
  +
# loadkeys ''keymap''
  +
  +
詳しくは {{ic|man 1 loadkeys}} を参照してください。
   
 
=== 永続的な設定 ===
 
=== 永続的な設定 ===
52行目: 64行目:
 
詳しくは {{ic|man 1 localectl}} を参照してください。
 
詳しくは {{ic|man 1 localectl}} を参照してください。
   
=== 一時的な設定 ===
+
=== 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.
一時的な設定を行うには ''loadkeys'' ツールを使います。このツールは [[systemd|systemd]] によって {{ic|/etc/vconsole.conf}} に設定されたキーマップをロードするために内部的に使用されています。とても簡単に使えます:
 
   
  +
First, create a keymap file. This keymap file can be anywhere, but one method is to mimic the directory hierarchy in {{ic|/usr/local}}:
# loadkeys ''keymap''
 
   
  +
# mkdir -p /usr/local/share/kbd/keymaps
詳しくは {{ic|man 1 loadkeys}} を参照してください。
 
  +
# 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 [[Power_management#Suspend_and_hibernate|enter a system into hibernation]], the following keymap is added:
  +
  +
string F70 = "sudo systemctl 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"
  +
  +
* 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 {{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}}.
   
 
== 他の設定 ==
 
== 他の設定 ==

2021年8月6日 (金) 21:12時点における版

関連記事

ノート: この記事ではレイアウトの修正や追加キーのマッピングなどを除く基本的な設定についてのみ説明しています。省かれている高度な話題については特別なキーボードキーを参照してください。

仮想コンソールのキーボードマッピング (keymap) やコンソールフォント、コンソールマップは kbd パッケージ (既にインストールされているはずです) によって提供されています。このパッケージには仮想コンソールを管理するための低水準なツールも多く含まれています。

キーボード設定の表示

次のコマンドを使うことでキーボード設定を表示することができます:

$ localectl status
   System Locale: LANG=ja_JP.UTF-8
       VC Keymap: jp106
      X11 Layout: n/a

キーマップ

キーマップの一覧表示

複数のコンポーネントによって構成されている XKB キーボードレイアウトとは異なり、仮想コンソールのキーボードレイアウトはコンポーネントがひとつしかありません。基本的にはひとつの keymap ファイルがひとつのキーボードレイアウトに対応しています (include ステートメントを使って共有の部分を共有することで keymap ファイルには複数のレイアウトを含めることができます)。keymap ファイルは /usr/share/kbd/keymaps/ ディレクトリツリーに保存されています。次のコマンドを使うことで利用可能な全てのキーマップを表示できます:

$ localectl list-keymaps

コンソールキーマップの命名規則は厳格には定まっていませんが、一般的には 2-letter country code と variant をスラッシュ (-) かアンダースコア (_) で区切った名前が付けられています。

find を使ってキーマップを調べることもできます:

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

Loadkeys

現在のセッションでのみ有効なキーマップを設定することもできます。異なるキーマップを試したり、問題を解決したりするときに有用です。

一時的な設定を行うには loadkeys ツールを使います。このツールは systemd によって /etc/vconsole.conf に設定されたキーマップをロードするために内部的に使用されています。とても簡単に使えます:

# loadkeys keymap

詳しくは man 1 loadkeys を参照してください。

永続的な設定

高度な設定を /etc/vconsole.conf ですることができ、起動時に systemd によって読み込まれます。KEYMAP 変数がキーマップの指定に使用されます。変数が空だったり設定されていない場合、デフォルトの値として us キーマップが使われます。全てのオプションは man 5 vconsole.conf を見て下さい。例:

/etc/vconsole.conf
KEYMAP=jp106
...

localectl を使ってコンソールのキーマップを設定することも可能です。コマンドを実行することで /etc/vconsole.conf 内の KEYMAP 変数が変更され現在のセッションのキーマップが設定されます。例:

$ localectl set-keymap --no-convert jp106

詳しくは man 1 localectl を参照してください。

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.

ヒント: 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 systemctl 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.

他の設定

コンソールフォントの変更

kbd パッケージにはコンソールフォントやフォントマッピングを変更するためのツールが含まれています。フォントは /usr/share/kbd/consolefonts/ ディレクトリに保存されています。

vconsole.conf 内で FONTFONT_MAP 変数を使うことで設定できます:

/etc/vconsole.conf
...
FONT=Lat2-Terminus16
FONT_MAP=8859-2

FONT 変数が空だったり設定されていない場合、カーネルに組み込まれているフォントがデフォルトとして使われます。詳細は man 5 vconsole.conf を参照してください。

typematic delay と rate の調整

typematic delay はキーリピートを始めるのにキーを押し続ける必要がある時間 (ミリ秒) を表します。キーリピートが開始されると、typematic rate によって指定された頻度 (Hz) で文字が繰り返されます。これらの値は kbdrate コマンドを使うことで変更できます:

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

例えば typematic delay を 200ms に typematic rate を 30Hz に設定するには、次のコマンドを実行します:

# kbdrate -d 200 -r 30

delay や rate の値を指定しないでコマンドを実行することで typematic の値をデフォルト (delay は 250ms で rate は 11Hz) に戻すことができます:

# kbdrate

Xorg で設定したい場合は Xorg でのキーボード設定#typematic delay と rate の調整を見てください。

Systemd サービス

systemd サービスを使ってキーボードレートを設定することもできます。例:

/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

作成したら kbdrate.service起動有効化してください。