Difference between revisions of "Keyboard input"

From ArchWiki
Jump to: navigation, search
(Identifying keycodes in Xorg: expand expansion template)
 
(21 intermediate revisions by 4 users not shown)
Line 4: Line 4:
 
[[zh-hans:Extra keyboard keys]]
 
[[zh-hans:Extra keyboard keys]]
 
{{Related articles start}}
 
{{Related articles start}}
{{Related|Xorg keyboard configuration}}
+
{{Related|Extra keyboard keys}}
{{Related|Console keyboard configuration}}
 
{{Related|Map scancodes to keycodes}}
 
{{Related|Xmodmap}}
 
 
{{Related articles end}}
 
{{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.
 
  
 
Prerequisite for modifying the key mapping is knowing how a key press results in a symbol:
 
Prerequisite for modifying the key mapping is knowing how a key press results in a symbol:
Line 16: Line 11:
 
# The keyboard sends a [[Wikipedia:Scancode|scancode]] to the computer.
 
# The keyboard sends a [[Wikipedia:Scancode|scancode]] to the computer.
 
# The Linux kernel maps the scancode to a '''keycode''', see [[Map scancodes to keycodes]].
 
# The Linux kernel maps the scancode to a '''keycode''', see [[Map scancodes to keycodes]].
# The keyboard layout maps the keycode to a symbol or '''keysym''', depending on what [[Wikipedia:Modifier key|modifier keys]] are pressed, see [[Console keyboard configuration]] or [[XKB]]/[[xmodmap]].
+
# The [[Wikipedia:Keyboard layout|keyboard layout]] maps the keycode to a symbol or '''keysym''', depending on what [[Wikipedia:Modifier key|modifier keys]] are pressed.
 +
#* For the [[Linux console]], see [[Linux console/Keyboard configuration]].
 +
#* For [[Xorg]] and [[Wayland]], see [[Xorg/Keyboard configuration]].
  
Most of your keys should already have a ''keycode'', or at least a ''scancode''. Keys without a ''scancode'' are not recognized by the kernel; these can include additional keys from 'gaming' keyboards, etc.
+
Most of your keys should already have a ''keycode'', or at least a ''scancode''. Keys without a ''scancode'' are not recognized by the kernel; these can include additional keys from "gaming" keyboards, etc.
  
In Xorg, some ''keysyms'' (e.g. {{ic|XF86AudioPlay}}, {{ic|XF86AudioRaiseVolume}} etc.) can be mapped to actions (i.e. launching an external application). See [[Xorg keyboard configuration#Mapping keysyms to actions]] for details.
+
In Xorg, some ''keysyms'' (e.g. {{ic|XF86AudioPlay}}, {{ic|XF86AudioRaiseVolume}} etc.) can be mapped to actions (i.e. launching an external application). See [[Keyboard shortcuts#Xorg]] for details.
  
 
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 [[Console keyboard configuration#Creating a custom keymap]] for details.
 
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 [[Console keyboard configuration#Creating a custom keymap]] for details.
Line 28: Line 25:
 
=== Using showkey ===
 
=== Using showkey ===
  
The traditional way to get a ''scancode'' is to use the ''showkey'' utility. ''showkey'' waits for a key to be pressed, or exits if no keys are pressed within 10 seconds. For ''showkey'' to work you need to be in a [[Wikipedia:Virtual console|virtual console]], not in a graphical environment or logged in via a network connection. Run the following command:
+
The traditional way to get a ''scancode'' is to use the {{man|1|showkey}} utility. ''showkey'' waits for a key to be pressed, or exits if no keys are pressed within 10 seconds. For ''showkey'' to work you need to be in a [[Wikipedia:Virtual console|virtual console]], not in a graphical environment or logged in via a network connection. Run the following command:
  
 
  # showkey --scancodes
 
  # showkey --scancodes
Line 36: Line 33:
 
=== Using evtest ===
 
=== Using evtest ===
  
For USB keyboards, it is apparently necessary to use ''evtest'' from the {{Pkg|evtest}} package instead of ''showkey'':[https://ask.fedoraproject.org/en/question/46201/how-to-map-scancodes-to-keycodes/]
+
For USB keyboards, it is apparently necessary to use {{man|1|evtest}} from the {{Pkg|evtest}} package instead of ''showkey'' [https://ask.fedoraproject.org/en/question/46201/how-to-map-scancodes-to-keycodes/]:
  
 
  # evtest /dev/input/event12
 
  # evtest /dev/input/event12
Line 58: Line 55:
 
== Identifying keycodes ==
 
== Identifying keycodes ==
  
{{Note|The Xorg ''keycodes'' are larger than the Linux ''keycodes'' by 8. [https://cgit.freedesktop.org/xorg/driver/xf86-input-evdev/tree/src/evdev.c]}}
+
The Linux keycodes are defined in {{ic|/usr/include/linux/input-event-codes.h}} (see the {{ic|KEY_}} variables).
  
=== In console ===
+
=== Identifying keycodes 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. To execute ''showkey'' you need to be in a virtual console, not in a graphical environment. Run the following command:
+
The ''keycodes'' for [[virtual console]] are reported by the {{man|1|showkey}} utility. ''showkey'' waits for a key to be pressed and if none is during 10 seconds it quits. To execute ''showkey'' you need to be in a virtual console, not in a graphical environment. Run the following command:
  
 
  # showkey --keycodes
 
  # showkey --keycodes
Line 68: Line 65:
 
and try to push keyboard keys; you should see ''keycodes'' being printed to the output.
 
and try to push keyboard keys; you should see ''keycodes'' being printed to the output.
  
=== In Xorg ===
+
=== Identifying keycodes in Xorg ===
 +
 
 +
{{Expansion|xev also reports keysyms. Mention that you need to focus the "Event Tester" window.}}
 +
 
 +
{{Note|The Xorg ''keycodes'' are 8 larger than the Linux ''keycodes''.[https://cgit.freedesktop.org/xorg/driver/xf86-input-evdev/tree/src/evdev.c]}}
  
 
The ''keycodes'' used by [[Xorg]] are reported by a utility called {{man|1|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.
 
The ''keycodes'' used by [[Xorg]] are reported by a utility called {{man|1|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.
Line 85: Line 86:
 
  135 Menu
 
  135 Menu
  
[[Xbindkeys#Identifying keycodes]]
+
[[Xbindkeys#Identifying keycodes|Xbindkeys]] is another wrapper to ''xev'' that reports ''keycodes''.
is another wrapper to "xev" that reports keycodes.
 
  
If you press a key and nothing appears in the terminal, it means that either the key does not have a ''scancode'', the ''scancode'' is not mapped to a ''keycode'', or some other process is capturing the keypress. If you suspect that a process listening to X server is capturing the keypress, you can try running xev from a clean X session:  
+
If you press a key and nothing appears in the terminal, it means that either the key does not have a ''scancode'', the ''scancode'' is not mapped to a ''keycode'', or some other process is capturing the keypress. If you suspect that a process listening to X server is capturing the keypress, you can try running ''xev'' from a clean X session:  
  
 
  $ xinit /usr/bin/xterm -- :1
 
  $ xinit /usr/bin/xterm -- :1
 
== Mapping scancodes to keycodes ==
 
 
See the main article: [[Map scancodes to keycodes]].
 
 
== Mapping keycodes to keysyms ==
 
 
=== In Xorg ===
 
 
See the main article: [[xmodmap]].
 
 
== Laptops ==
 
 
=== Apple MacBooks ===
 
 
All the required information is available on the [[Apple Keyboard]] dedicated article.
 
 
=== 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 1 > /sys/devices/platform/asus_laptop/ls_switch
 
 
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 - - - - 1
 
}}
 
 
{{Note|This may work also for other Asus notebook models.}}
 
 
=== Asus N56VJ (or possibly others) ===
 
 
If most of your special keys do not 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 in [[Kernel modules#Automatic module handling]].
 
 
=== Lenovo T460p (or possibly others) ===
 
Out of the box, the backlight keys (on F5, F6) might not be available, even via the {{ic|/dev/input}} interface. To fix this, follow [[Backlight#Kernel command-line options]].
 
 
== Gaming Keyboards ==
 
 
Gaming keyboards have some special features which may cause them to "misbehave" in Linux.
 
 
===Cooler Master CM Storm QuickFire TK===
 
 
This keyboard has two features that could cause confusion in Linux: N-Key Rollover and the Win-Lock Key.
 
 
N-Key Rollover can [https://bbs.archlinux.org/viewtopic.php?id=170877 cause problems with the Function keys]. To disable N-key rollover, hold down the FN lock key (next to right-ctrl) until it lights up, then hold Escape and press 6 to switch to 6-key rollover. Hold down the FN lock key to disable the Fn lock.
 
 
The Win-Lock Key completely disables the Super (Windows) keys. Simply press the FN lock key and F12 together to toggle Win-Lock on and off.
 
 
=== Corsair K series keyboards ===
 
 
There is a winlock button on these keyboards that can disable the use of the Super (Windows) keys. This button is located at the top right of the keyboard next to the num and capslock buttons. CKB can be used to disable this functionality entirely preventing further locking. However, in a default state, simply pressing the button would enable the Super (Windows) keys again.
 
 
=== Logitech G series G710 and 710+ ===
 
 
This keyboard has a row of 6 programmable G keys. In order to use them as intended by Logitech, you need to install {{aur|sidewinderd}} and [[start]] {{ic|sidewinderd.service}}.
 
 
== See also ==
 
 
* [http://wiki.linuxquestions.org/wiki/Configuring_keyboards#Enabling_Keyboard_Multimedia_Keys Enabling Keyboard Multimedia Keys] - guide on LinuxQuestions wiki
 
* [http://www.gentoo-wiki.info/HOWTO_Use_Multimedia_Keys Multimedia Keys] on [http://www.gentoo-wiki.info/ Gentoo Wiki Archives]
 

Latest revision as of 16:50, 23 October 2018

Prerequisite for modifying the key mapping is knowing how a key press results in a symbol:

  1. The keyboard sends a scancode to the computer.
  2. The Linux kernel maps the scancode to a keycode, see Map scancodes to keycodes.
  3. The keyboard layout maps the keycode to a symbol or keysym, depending on what modifier keys are pressed.

Most of your keys should already have a keycode, or at least a scancode. Keys without a scancode are not recognized by the kernel; these can include additional keys from "gaming" keyboards, etc.

In Xorg, some keysyms (e.g. XF86AudioPlay, XF86AudioRaiseVolume etc.) can be mapped to actions (i.e. launching an external application). See Keyboard shortcuts#Xorg 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 Console keyboard configuration#Creating a custom keymap for details.

Identifying scancodes

Using showkey

The traditional way to get a scancode is to use the showkey(1) utility. showkey waits for a key to be pressed, or exits if no keys are pressed within 10 seconds. For showkey to work you need to be in a virtual console, not in a graphical environment or logged in via a network connection. Run the following command:

# showkey --scancodes

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

Using evtest

For USB keyboards, it is apparently necessary to use evtest(1) from the evtest package instead of showkey [1]:

# evtest /dev/input/event12
...
Event: time 1434666536.001123, type 4 (EV_MSC), code 4 (MSC_SCAN), value 70053
Event: time 1434666536.001123, type 1 (EV_KEY), code 69 (KEY_NUMLOCK), value 0
Event: time 1434666536.001123, -------------- EV_SYN ------------

Use the "value" field of MSC_SCAN. This example shows that NumLock has scancode 70053 and keycode 69.

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.

Identifying keycodes

The Linux keycodes are defined in /usr/include/linux/input-event-codes.h (see the KEY_ variables).

Identifying keycodes in console

The keycodes for virtual console are reported by the showkey(1) utility. showkey waits for a key to be pressed and if none is during 10 seconds it quits. 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.

Identifying keycodes in Xorg

Tango-view-fullscreen.pngThis article or section needs expansion.Tango-view-fullscreen.png

Reason: xev also reports keysyms. Mention that you need to focus the "Event Tester" window. (Discuss in Talk:Keyboard input#)
Note: The Xorg keycodes are 8 larger than the Linux keycodes.[2]

The keycodes used by Xorg are reported by a utility called xev(1), 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 | awk -F'[ )]+' '/^KeyPress/ { a[NR+2] } NR in a { printf "%-3s %s\n", $5, $8 }'

Here is an example output:

38  a
55  v
54  c
50  Shift_L
133 Super_L
135 Menu

Xbindkeys is another wrapper to xev that reports keycodes.

If you press a key and nothing appears in the terminal, it means that either the key does not have a scancode, the scancode is not mapped to a keycode, or some other process is capturing the keypress. If you suspect that a process listening to X server is capturing the keypress, you can try running xev from a clean X session:

$ xinit /usr/bin/xterm -- :1