Difference between revisions of "Extra keyboard keys"

From ArchWiki
Jump to: navigation, search
m
m (Added note on how to return to the graphical environment.)
(47 intermediate revisions by 19 users not shown)
Line 1: Line 1:
[[Category:Input devices (English)]][[Category:HOWTOs (English)]]
+
[[Category:Keyboards]]
{{i18n_links_start}}
+
[[zh-CN:Extra Keyboard Keys]]
{{i18n_entry|English|Extra Keyboard Keys}}
+
{{Article summary start}}
{{i18n_entry|简体中文|热键_(简体中文)}}
+
{{Article summary text|A general overview of how to assign actions to extra keyboard keys.}}
{{i18n_links_end}}
+
{{Article summary heading|Related}}
 +
{{Article summary wiki|Xorg}}
 +
{{Article summary wiki|Xmodmap}}
 +
{{Article summary wiki|Extra Keyboard Keys in Xorg}}
 +
{{Article summary wiki|Extra Keyboard Keys in Console}}
 +
{{Article summary end}}
  
= Introduction =
+
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 3 ways of doing that:
Many keyboards include some "special keys" (also called hotkeys), 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's why we need to map the keys to actions. There are two ways of doing that:
+
*The most portable way using low level tools, such as [[acpid]]. Not all keys are supported, but configuration in uniform way is possible for keyboard keys, power adapter connection and even headphone jack (un)plugging events.
*The universal way using [[HAL]] and [[Xorg]] utilities (and eventually your desktop environment tools)
+
*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
+
*The quicker way using a third-party program to do everything in GUI, such as the Gnome Control Center or [[Keytouch]]
  
Contrary to what you may think the first method is preferred as [[HAL]] and [[Xorg]] are more universal than any third-party program could ever claim to be.
+
Before starting you need to learn some vocabulary:
  
 +
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.
  
 +
A '''keycode''' is the second level of identification for a key, a keycode corresponds to a function.
  
'''If you don't have time''' and just want the keys to work there are 2 cases:
+
A '''symbol''' is the third level of identification for a key, it is the way Xorg refers to keys.
*You use Gnome:
+
Everything can be done from the Control Center.
+
*You use KDE or an other environment that doesn't support kernel-level extra keys detection:
+
Use a third-party program, preferably [[keytouch]].
+
  
=The Arch way=
+
==Step 1: Check for keycodes==
==Preface==
+
Most of your keys should already have a keycode, or at least a scancode. Keys without a scancode are not recognized by the kernel.
Before starting you need to learn some vocabulary ...
+
  
A '''scancode''' is the lowest identification number for a key. If a key doesn't have a scancode then we can't do anything because it means that the kernel doesn't see it.
+
===Using xev===
 +
Use the graphical X program "xev" (without having to switch to a console environment).
 +
Install the xev program:
  
A '''keycode''' is the second level of identification for a key, a keycode corresponds to a function.
+
{{bc|# pacman -S xorg-xev}}
  
A '''symbol''' is the third level of identification for a key, it is the way Xorg refers to keys.
+
With the following line you can start xev and directly grep the important parts:
  
==Step 1: Map scancodes==
+
{{bc|<nowiki>$ xev | grep -A2 --line-buffered '^KeyRelease' | sed -n '/keycode /s/^.*keycode \([0-9]*\).* (.*, \(.*\)).*$/\1 \2/p'</nowiki>}}
If you use [[HAL]] the most part of your keys should already have a keycode. If not, think about helping the community by expanding the database.
+
  
===The diagnosis===
 
====Using showkey====
 
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'''.
 
$ showkey
 
and try to push your hotkeys. If a keycode appears the key is mapped, if not it can mean either that the kernel doesn't see the key or that the key is not mapped.
 
 
====Using xev====
 
Another way to get the keycodes of your keys is to use the graphical X program "xev" (without having to switch to a console environment). With the following line you can start xev and directly grep the important parts:
 
$ xev | grep -A2 --line-buffered '^KeyRelease' | sed -n '/keycode /s/^.*keycode \([0-9]*\).* (.*, \(.*\)).*$/\1 \2/p'
 
 
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:
 
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
Line 49: Line 43:
 
  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 doesn't see that key or that it is not mapped.
+
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.
  
====Conclusion====
+
=== Using showkey ===
If all your keys have a keycode you can go directly to the second step.
+
The universal way to know if a key has a keycode is to use the kernel {{ic|showkey}} 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''' (Ctrl+Alt+F7 returns to the graphical environment).
 +
{{bc|# showkey}}
 +
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.
  
If not keep reading below:
+
=== 2.6 kernels ===
 +
According to the keymap man page:
  
===Know if a key has a scancode===
+
{{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.}}
If a key doesn't have a keycode you can know if it has a scancode by looking at the kernel log using the dmesg command:
+
 
 +
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]]).
 +
 
 +
If all your keys have a keycode you can go directly to the second step. If not keep reading below:
 +
 
 +
=== Check for scancodes ===
 +
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
 
  $ dmesg|tail -5
 
If when you press the key something like that appears:
 
If when you press the key something like that appears:
Line 64: Line 67:
 
then your key has a scancode which can be mapped to a keycode. See [[Map scancodes to keycodes]].
 
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 doesn't have a scancode which means that it is not recognized by the kernel so it cannot be used.
+
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.
 
+
===Map scancodes to keycodes===
+
''See the detailed article: [[Map scancodes to keycodes]].''
+
  
 
==Step 2: Map keycodes==
 
==Step 2: Map keycodes==
 
===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 dedicated article: [[Extra Keyboard Keys in Console]].''
  
''See the detailed article: [[Extra Keyboard Keys in Console]].''
+
When in console, hotkeys can be used to print sequences of characters, including escape sequences. Thus, printing the sequence of characters constituting a command followed by the escape sequence for a new will execute the command.
  
 
===In Xorg===
 
===In Xorg===
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]].
+
''See the dedicated article: [[Extra Keyboard Keys in Xorg]].''
 
+
=The quick way=
+
==In Gnome==
+
If you use Gnome everything can be done from the Control Center. However if you decide to change for another Desktop Environment you'll have to re-configure your keys.
+
 
+
==In other desktops environments==
+
In desktop environments that can't read scancodes and/or keycodes you can use one of the following third-party softwares:
+
  
===keytouch===
+
==Laptops==
KeyTouch is a program which allows you to easily configure the extra function keys of your keyboard. This means that you can define, for every individual function key, what to do if it is pressed.
+
===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:
  
''See the detailed article: [[keytouch]].''
+
{{bc|# echo 0 > /sys/devices/platform/asus-laptop}}
  
===lineak===
+
To have it run on boot:
{{Box Note |Lineak is not really an active project. If you really want to use a third-party program you are encouraged to use [[keytouch]] instead.}}
+
{{hc|/etc/rc.local|echo 0 > /sys/devices/platform/asus-laptop/ls_switch}}
Lineak is a utility designed to enable the use and configuration of internet, easy access and multimedia keys.
+
  
=Laptops=
+
Note that this may work for other Asus notebook models.
==Asus M series==
+
I think it'll be work in another series with Asus product of laptops. How to make work multimedia keys on Arch? And disable light sensor? It's very simple, login as root, edit:
+
$ /etc/rc.local
+
add only one line:
+
$ echo 0 > /sys/devices/platform/asus-laptop/ls_switch
+
It's all, regads.
+
 
''See the detailed article: [[lineak]].''
 
''See the detailed article: [[lineak]].''

Revision as of 14:45, 20 March 2013

Template:Article summary start Template:Article summary text Template:Article summary heading Template:Article summary wiki Template:Article summary wiki Template:Article summary wiki Template:Article summary wiki Template:Article summary end

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 3 ways of doing that:

  • The most portable way using low level tools, such as acpid. Not all keys are supported, but configuration in uniform way is possible for keyboard keys, power adapter connection and even headphone jack (un)plugging events.
  • 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 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.

A keycode is the second level of identification for a key, a keycode corresponds to a function.

A symbol is the third level of identification for a key, it is the way Xorg refers to keys.

Step 1: Check for keycodes

Most of your keys should already have a keycode, or at least a scancode. Keys without a scancode are not recognized by the kernel.

Using xev

Use the graphical X program "xev" (without having to switch to a console environment). Install the xev program:

# pacman -S xorg-xev

With the following line you can start xev and directly grep the important parts:

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

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
27 r
54 c
43 h
153 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

The universal way to know if a key has a keycode is to use the kernel showkey 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 (Ctrl+Alt+F7 returns to the graphical environment).

# showkey

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

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).

If all your keys have a keycode you can go directly to the second step. If not keep reading below:

Check for scancodes

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.

Step 2: Map keycodes

In Console

See the dedicated article: Extra Keyboard Keys in Console.

When in console, hotkeys can be used to print sequences of characters, including escape sequences. Thus, printing the sequence of characters constituting a command followed by the escape sequence for a new will execute the command.

In Xorg

See the dedicated article: Extra Keyboard Keys in Xorg.

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:

/etc/rc.local
echo 0 > /sys/devices/platform/asus-laptop/ls_switch

Note that this may work for other Asus notebook models. See the detailed article: lineak.