スキャンコードをキーコードにマップ
スキャンコードからキーコードへのマッピングは Linux コンソールもしくは Xorg に限定されない普遍的なことであり、コンソールと Xorg 両方に影響を与えます。
スキャンコードをキーコードにマップする方法は2つあります:
- udev を使う
- setkeycodes を使う
udev を使用する方法が推奨されます。udev は (情報源として信頼できる) ハードウェア情報を使ってデータベースからキーボードモデルを選択しているからです。あなたの使用しているキーボードモデルがデータベースから見つからなかった場合、あなたのキーは out of the box として認識されます。
目次
スキャンコードの確認
リマップするキーのスキャンコードを知っておく必要があります。詳しくは特別なキーボードキー#スキャンコードを見て下さい。
udev を使う
udev には /etc/udev/hwdb.bin のハードウェアデータベースインデックスを管理するための hwdb という組み込み関数が存在します。データベースは /usr/lib/udev/hwdb.d/, /run/udev/hwdb.d/, /etc/udev/hwdb.d/ ディレクトリに存在する拡張子が .hwdb のファイルから作成されます。デフォルトの scancodes-to-keycodes マッピングファイルは /usr/lib/udev/hwdb.d/60-keyboard.hwdb です。詳しくは man udev を見て下さい。
The .hwdb file can contain multiple blocks of mappings for different keyboards, or one block can be applied to multiple keyboards. The keyboard: prefix is used to match a block against a hardware, the following hardware matches are supported:
- USB keyboards identified by the usb kernel modalias:
keyboard:usb:v<vendor_id>p<product_id>*
where<vendor_id>and<product_id>are the 4-digit hex uppercase vendor and product IDs (you can find those by running thelsusbcommand). - AT keyboard DMI data matches:
keyboard:dmi:bvn*:bvr*:bd*:svn<vendor>:pn<product>:pvr*
where<vendor>and<product>are the firmware-provided strings exported by the kernel DMI modalias. - Platform driver device name and DMI data match:
keyboard:name:<input_device_name>:dmi:bvn*:bvr*:bd*:svn<vendor>:pn*
where<input_device_name>is the name device specified by the driver and<vendor>is the firmware-provided string exported by the kernel DMI modalias.
The format of each line in the block body is KEYBOARD_KEY_<scancode>=<keycode>. The value of <scancode> is hexadecimal, but without the leading 0x (i.e. specify a0 instead of 0xa0), whereas the value of <keycode> is the lower-case keycode name string as listed in /usr/include/linux/input-event-codes.h (see the KEY_<KEYCODE> variables), a sorted list is available at [1]. It is not possible to specify decimal value in <keycode>.
hwdb のカスタム例
以下の hwdb ファイルは全ての USB・AT キーボードにマッチします:
/etc/udev/hwdb.d/90-custom-keyboard.hwdb
keyboard:usb:v*p* keyboard:dmi:bvn*:bvr*:bd*:svn*:pn*:pvr* KEYBOARD_KEY_10=suspend KEYBOARD_KEY_a0=search
Hardware Database Index のアップデート
設定ファイルを変更した後は、ハードウェアデータベースインデックス hwdb.bin を再構築する必要があります。
- 次を実行して
hwdb.binを手動でアップデート:
# udevadm hwdb --update
systemd-udev-hwdb-update.serviceのConditionNeedsUpdateをコメントアウトすると再起動するたびに自動的に更新されます:
/usr/lib/systemd/system/systemd-udev-hwdb-update.service
# This file is part of systemd. . . #ConditionNeedsUpdate=/etc . .
systemd-udev-hwdb-update.service のロードが完了すると systemd-udev-trigger.service は
hwdb.bin の変更をリロードします。
- Systemd のアップグレード時には自動的に更新が行われます。
Systemd のアップグレード時、インストールスクリプトが # udevadm hwdb --update を実行して hwdb.bin を再構築するため手動の操作は必要ありません。
Hardware Database Index のリロード
カーネルは起動時に hwdb.bin を読み込むため、システムを再起動することで最新の hwdb.bin をロードできます。
udevadm を以下のようなコマンドで使うことで最新の hwdb.bin から新しいキーマッピングをロードできます:
# udevadm trigger
Be aware that with udevadm only added or changed key mapping are loaded so if we delete a mapping from the config file, rebuild hwdb.bin and run # udevadm trigger then the deleted mapping still kept by the kernel, at least until a reboot.
setkeycodes を使う
setkeycodes は scancodes-to-keycodes マッピングテーブルを Linux カーネルにロードさせるツールです。使用方法は:
# setkeycodes scancode keycode ...
一度に複数のペアを指定することが可能です。スキャンコードは16進数で、キーコードは10進数で記述します。