Difference between revisions of "Extra keyboard keys"

From ArchWiki
Jump to: navigation, search
(fix heading levels)
(38 intermediate revisions by 13 users not shown)
Line 1: Line 1:
{{i18n|Extra Keyboard Keys}}
+
[[Category:Keyboards]]
[[Category:Input devices (English)]]
+
[[zh-CN:Extra Keyboard Keys]]
 +
{{Related articles start}}
 +
{{Related|Extra Keyboard Keys in Xorg}}
 +
{{Related|Extra Keyboard Keys in Console}}
 +
{{Related|Keyboard Configuration in Xorg}}
 +
{{Related|Keyboard Configuration in Console}}
 +
{{Related|Map scancodes to keycodes}}
 +
{{Related|Xmodmap}}
 +
{{Related articles end}}
 +
Many keyboards include some ''special keys'' (also called ''hotkeys'' or ''multimedia keys''), which are supposed to execute an application or print special characters (not included in the standard national keymaps). [[udev]] contains a large database of mappings specific to individual keyboards, so common keyboards usually work out of the box. If you have very recent or uncommon piece of hardware, you may need to adjust the mapping manually.
  
Many keyboards include some "special keys" (also called hotkeys, such as HP Quickplay), which are supposed to execute an application or print special characters (not included in the standard national keymaps). The lack of specification for these extra keys makes it impossible for the kernel to know what to do with them and that is why we need to map the keys to actions. There are two ways of doing that:
+
Prerequisite for modifying the key mapping is knowing how the keys are identified on the system. There are multiple levels:  
*The universal way using [[Xorg]] utilities (and eventually your desktop environment tools)
+
*The quicker way using a third-party program to do everything in GUI, such as the Gnome Control Center or [[Keytouch]]
+
  
Before starting you need to learn some vocabulary:
+
* A [[Wikipedia:Scancode|scancode]] is the lowest identification number for a key, it is the value that a keyboard sends to a computer.
 +
* A '''keycode''' is the second level of identification for a key, a ''keycode'' corresponds to a function.
 +
* A '''keysym''' is the third level of identification for a key, it corresponds to a ''symbol''. It may depend on whether the Shift key or another [[Wikipedia:Modifier key|modifier key]] was also pressed.
  
A '''scancode''' is the lowest identification number for a key. If a key does not have a scancode then we cannot do anything because it means that the kernel does not see it.
+
''Scancodes'' are mapped to ''keycodes'', which are then mapped to ''keysyms'' depending on used keyboard layout. Most of your keys should already have a ''keycode'', or at least a ''scancode''. Keys without a ''scancode'' are not recognized by the kernel.
  
A '''keycode''' is the second level of identification for a key, a keycode corresponds to a function.
+
In Xorg, some ''keysyms'' (e.g. {{ic|XF86AudioPlay}}, {{ic|XF86AudioRaiseVolume}} etc.) can be mapped to actions (i.e. launching an external application). See [[Extra Keyboard Keys in Xorg#Map keysyms to actions]] for details.
  
A '''symbol''' is the third level of identification for a key, it is the way Xorg refers to keys.
+
In Linux console, some ''keysyms'' (e.g. {{ic|F1}} to {{ic|F246}}) can be mapped to certain actions (e.g. switch to other console or print some sequence of characters). See [[Extra Keyboard Keys in Console]] for details.
  
==Step 1: Check for keycodes==
+
== Identifying key codes ==
Most of your keys should already have a keycode, or at least a scancode. Keys without a scancode are not recognized by the kernel.
+
  
===The diagnosis===
+
=== Scancodes ===
====Using xev====
+
Use the graphical X program "xev" (without having to switch to a console environment).
+
Install the xev program:
+
  
{{Cli|# pacman -S xorg-xev}}
+
==== Using showkey ====
  
With the following line you can start xev and directly grep the important parts:
+
The universal way to get a ''scancode'' is to use the ''showkey'' utility. ''showkey'' waits for a key to be pressed and if none is during 10 seconds it quits, which is the only way to exit the program. To execute ''showkey'' you need to be in a [[Wikipedia:Virtual console|virtual console]], not in a graphical environment. Run the following command
  
{{Cli|<nowiki>$ xev | grep -A2 --line-buffered '^KeyRelease' | sed -n '/keycode /s/^.*keycode \([0-9]*\).* (.*, \(.*\)).*$/\1 \2/p'</nowiki>}}
+
# showkey --scancodes
 +
 
 +
and try to push keyboard keys, you should see ''scancodes'' being printed to the output.
 +
 
 +
==== Using dmesg ====
 +
 
 +
{{Note|This method does not provide ''scancodes'' for all keys, it only identifies the unknown keys.}}
 +
 
 +
You can get the ''scancode'' of a key by pressing the desired key and looking the output of {{ic|dmesg}} command. For example, if you get:
 +
 
 +
Unknown key pressed (translated set 2, code 0xa0 on isa0060/serio0
 +
 
 +
then the ''scancode'' you need is {{ic|0xa0}}.
 +
 
 +
=== Keycodes ===
 +
 
 +
{{Warning|Note that the ''keycodes'' are different for Linux console and Xorg. Use the appropriate tool to determine the desired value.}}
 +
 
 +
==== In console ====
 +
 
 +
The ''keycodes'' for [[Wikipedia:Virtual console|virtual console]] are reported by the ''showkey'' utility. ''showkey'' waits for a key to be pressed and if none is during 10 seconds it quits, which is the only way to exit the program. To execute ''showkey'' you need to be in a virtual console, not in a graphical environment. Run the following command
 +
 
 +
# showkey --keycodes
 +
 
 +
and try to push keyboard keys, you should see ''keycodes'' being printed to the output.
 +
 
 +
==== In Xorg ====
 +
 
 +
The ''keycodes'' used by [[Xorg]] are reported by a utility called ''xev'', which is provided by the {{Pkg|xorg-xev}} package. Of course to execute ''xev'', you need to be in a graphical environment, not in the console.
 +
 
 +
With the following command you can start ''xev'' and show only the relevant parts:
 +
 
 +
$ xev | grep -A2 --line-buffered '^KeyRelease' | sed -n '/keycode /s/^.*keycode \([0-9]*\).* (.*, \(.*\)).*$/\1 \2/p'
 +
 
 +
Here is an example output:
  
In the example below I pressed the "a", "r", "c" and "h" keys and two of the media keys on my Dell keyboard. This gives me the following output:
 
 
  38 a
 
  38 a
 
  27 r
 
  27 r
Line 35: Line 73:
 
  153 NoSymbol
 
  153 NoSymbol
 
  144 NoSymbol
 
  144 NoSymbol
This means that the "a", "r", "c" and "h" keys have the keycodes 38, 27, 54 and 43 and are properly bound while the media keys with the keycodes 153 and 144 have no function yet, which is indicated by "NoSymbol". If you press a key and nothing appears in the terminal, this means that the kernel does not see that key or that it is not mapped.
 
  
====Using showkey====
+
In the example the keys {{ic|a}}, {{ic|r}}, {{ic|c}}, {{ic|h}} and two other multimedia keys were pressed. The former four keys with ''keycodes'' {{ic|38}}, {{ic|27}}, {{ic|54}} and {{ic|43}} are properly mapped, while the multimedia keys with ''keycodes'' {{ic|153}} and {{ic|144}} are not. The {{ic|NoSymbol}} indicates that no ''keysyms'' are assigned to those keys.
The universal way to know if a key has a keycode is to use the kernel <code>showkey</code> program. showkey waits for a key to be pressed and if none is during 10 seconds it quits, note that this is the only way to exit the program. To execute showkey you need to be in a real console, it means not in a graphical environment so '''switch using Ctrl+Alt+F1'''.
+
 
{{Cli|$ showkey}}
+
If you press a key and nothing appears in the terminal, it means that either the key does not have a ''scancode'', or the ''scancode'' is not mapped to a ''keycode''.
and try to push your hotkeys. If a keycode appears the key is mapped, if not it can mean either that the kernel does not see the key or that the key is not mapped.
+
 
 +
==== 2.6 kernels ====
 +
 
 +
{{Out of date|Now we have 3.x kernel, is it still relevant?}}
  
====2.6 kernels====
 
 
According to the keymap man page:
 
According to the keymap man page:
  
{{Note|In  2.6  kernels  raw  mode, or scancode mode, is not very raw at all.  Scan codes are first translated to key codes, and when scancodes are desired the key codes are translated  back...there is no guarantee at all that the final result corresponds to what the keyboard hardware did send.}}
+
{{Note|In  2.6  kernels  raw  mode, or scancode mode, is not very raw at all.  Scan codes are first translated to key codes, and when scancodes are desired the key codes are translated  back...there is no guarantee at all that the final result corresponds to what the keyboard hardware did send. To change behavior back to the old raw mode, add the parameter {{ic|1=atkbd.softraw=0}} to your kernel while booting. This can be removed for later boots when the old raw functionality is not required.}}
  
 
This is relevant if the keymaps obtained from showkey and the ones set by [[setkeycodes]] differ from the ones obtained by xev in X. Keep this in mind when translating the keymaps into keysyms using xmodmap (See [[Extra Keyboard Keys in Xorg]]).
 
This is relevant if the keymaps obtained from showkey and the ones set by [[setkeycodes]] differ from the ones obtained by xev in X. Keep this in mind when translating the keymaps into keysyms using xmodmap (See [[Extra Keyboard Keys in Xorg]]).
  
====Conclusion====
+
== Mapping scancodes to keycodes ==
If all your keys have a keycode you can go directly to the second step.
+
  
If not keep reading below:
+
See the main article: [[Map scancodes to keycodes]].
  
===Check for scancodes===
+
== Mapping keycodes to keysyms ==
If a key does not have a keycode you can know if it has a scancode by looking at the kernel log using the dmesg command:
+
$ dmesg|tail -5
+
If when you press the key something like that appears:
+
atkbd.c: Unknown key pressed (translated set 2, code 0xf1 on isa0060/serio0).
+
atkbd.c: Use 'setkeycodes e071 <keycode>' to make it known.
+
then your key has a scancode which can be mapped to a keycode. See [[Map scancodes to keycodes]].
+
  
If nothing new appears in dmesg then your key does not have a scancode, which means that it is not recognized by the kernel and cannot be used.
+
=== In console ===
  
==Step 2: Map keycodes==
+
See the main article: [[Extra Keyboard Keys in Console]].
===In Console===
+
When we are in console, we can use our hotkeys to print a certain 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!
+
  
''See the detailed article: [[Extra Keyboard Keys in Console]].''
+
=== In Xorg ===
  
===In Xorg===
+
See the main article: [[xmodmap]].
When we are in a graphical environment we may want a key to print a special character or execute a command. There are many ways of doing that and they are covered in a dedicated article: [[Extra Keyboard Keys in Xorg]].
+
 
 +
== Laptops ==
 +
 
 +
=== Asus M series ===
  
==Laptops==
 
===Asus M series===
 
 
In order to have control over the light sensor and the multimedia keys on your Asus machine, you should use the following command:
 
In order to have control over the light sensor and the multimedia keys on your Asus machine, you should use the following command:
  
{{Cli|# echo 0 > /sys/devices/platform/asus-laptop}}
+
# echo 0 > /sys/devices/platform/asus-laptop
 +
 
 +
To have it run on boot create a [[Systemd#Temporary_files|Systemd tmpfile]]:
 +
{{hc|/etc/tmpfiles.d/local.conf|
 +
w /sys/devices/platform/asus-laptop/ls_switch - - - - 0
 +
}}
 +
 
 +
{{Note|This may work also for other Asus notebook models.}}
 +
 
 +
=== Asus N56VJ (or possibly others) ===
 +
 
 +
if most of your special keys don't work, try loading the asus-nb-wmi kernel module with
 +
# modprobe asus-nb-wmi
 +
 
 +
then check xev again. if you combine this with the acpi_osi="!Windows 2012" boot option, you may get weird results in xev, so try not using it. If this did fix things, make sure to make the module load at boot with methods described [[Kernel Modules|here]]
  
To have it run on boot:
+
== See also ==
{{File|/etc/rc.local|content=echo 0 > /sys/devices/platform/asus-laptop/ls_switch}}
+
  
Note that this may work for other Asus notebook models.
+
* [http://keytouch.sourceforge.net/howto_keyboard/node4.html How to retrieve scancodes] by Marvin Raaijmakers
''See the detailed article: [[lineak]].''
+
* [http://wiki.linuxquestions.org/wiki/Configuring_keyboards#Enabling_Keyboard_Multimedia_Keys Enabling Keyboard Multimedia Keys] - guide on LinuxQuestions wiki

Revision as of 03:11, 1 December 2013

Many keyboards include some special keys (also called hotkeys or multimedia keys), which are supposed to execute an application or print special characters (not included in the standard national keymaps). udev contains a large database of mappings specific to individual keyboards, so common keyboards usually work out of the box. If you have very recent or uncommon piece of hardware, you may need to adjust the mapping manually.

Prerequisite for modifying the key mapping is knowing how the keys are identified on the system. There are multiple levels:

  • A scancode is the lowest identification number for a key, it is the value that a keyboard sends to a computer.
  • A keycode is the second level of identification for a key, a keycode corresponds to a function.
  • A keysym is the third level of identification for a key, it corresponds to a symbol. It may depend on whether the Shift key or another modifier key was also pressed.

Scancodes are mapped to keycodes, which are then mapped to keysyms depending on used keyboard layout. Most of your keys should already have a keycode, or at least a scancode. Keys without a scancode are not recognized by the kernel.

In Xorg, some keysyms (e.g. XF86AudioPlay, XF86AudioRaiseVolume etc.) can be mapped to actions (i.e. launching an external application). See Extra Keyboard Keys in Xorg#Map keysyms to actions for details.

In Linux console, some keysyms (e.g. F1 to F246) can be mapped to certain actions (e.g. switch to other console or print some sequence of characters). See Extra Keyboard Keys in Console for details.

Identifying key codes

Scancodes

Using showkey

The universal way to get a scancode is to use the showkey utility. showkey waits for a key to be pressed and if none is during 10 seconds it quits, which is the only way to exit the program. To execute showkey you need to be in a virtual console, not in a graphical environment. Run the following command

# showkey --scancodes

and try to push keyboard keys, you should see scancodes being printed to the output.

Using dmesg

Note: This method does not provide scancodes for all keys, it only identifies the unknown keys.

You can get the scancode of a key by pressing the desired key and looking the output of dmesg command. For example, if you get:

Unknown key pressed (translated set 2, code 0xa0 on isa0060/serio0

then the scancode you need is 0xa0.

Keycodes

Warning: Note that the keycodes are different for Linux console and Xorg. Use the appropriate tool to determine the desired value.

In console

The keycodes for virtual console are reported by the showkey utility. showkey waits for a key to be pressed and if none is during 10 seconds it quits, which is the only way to exit the program. To execute showkey you need to be in a virtual console, not in a graphical environment. Run the following command

# showkey --keycodes

and try to push keyboard keys, you should see keycodes being printed to the output.

In Xorg

The keycodes used by Xorg are reported by a utility called xev, which is provided by the xorg-xev package. Of course to execute xev, you need to be in a graphical environment, not in the console.

With the following command you can start xev and show only the relevant parts:

$ xev | grep -A2 --line-buffered '^KeyRelease' | sed -n '/keycode /s/^.*keycode \([0-9]*\).* (.*, \(.*\)).*$/\1 \2/p'

Here is an example output:

38 a
27 r
54 c
43 h
153 NoSymbol
144 NoSymbol

In the example the keys a, r, c, h and two other multimedia keys were pressed. The former four keys with keycodes 38, 27, 54 and 43 are properly mapped, while the multimedia keys with keycodes 153 and 144 are not. The NoSymbol indicates that no keysyms are assigned to those keys.

If you press a key and nothing appears in the terminal, it means that either the key does not have a scancode, or the scancode is not mapped to a keycode.

2.6 kernels

Tango-view-refresh-red.pngThis article or section is out of date.Tango-view-refresh-red.png

Reason: Now we have 3.x kernel, is it still relevant? (Discuss in Talk:Extra keyboard keys#)

According to the keymap man page:

Note: In 2.6 kernels raw mode, or scancode mode, is not very raw at all. Scan codes are first translated to key codes, and when scancodes are desired the key codes are translated back...there is no guarantee at all that the final result corresponds to what the keyboard hardware did send. To change behavior back to the old raw mode, add the parameter atkbd.softraw=0 to your kernel while booting. This can be removed for later boots when the old raw functionality is not required.

This is relevant if the keymaps obtained from showkey and the ones set by setkeycodes differ from the ones obtained by xev in X. Keep this in mind when translating the keymaps into keysyms using xmodmap (See Extra Keyboard Keys in Xorg).

Mapping scancodes to keycodes

See the main article: Map scancodes to keycodes.

Mapping keycodes to keysyms

In console

See the main article: Extra Keyboard Keys in Console.

In Xorg

See the main article: xmodmap.

Laptops

Asus M series

In order to have control over the light sensor and the multimedia keys on your Asus machine, you should use the following command:

# echo 0 > /sys/devices/platform/asus-laptop

To have it run on boot create a Systemd tmpfile:

/etc/tmpfiles.d/local.conf
w /sys/devices/platform/asus-laptop/ls_switch - - - - 0
Note: This may work also for other Asus notebook models.

Asus N56VJ (or possibly others)

if most of your special keys don't work, try loading the asus-nb-wmi kernel module with

# modprobe asus-nb-wmi

then check xev again. if you combine this with the acpi_osi="!Windows 2012" boot option, you may get weird results in xev, so try not using it. If this did fix things, make sure to make the module load at boot with methods described here

See also