User Tools

Site Tools


doc:appunti:linux:sa:remap_keyboard_keys

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
doc:appunti:linux:sa:remap_keyboard_keys [2021/12/07 10:24] – [Customize events using udev] niccolodoc:appunti:linux:sa:remap_keyboard_keys [2021/12/07 11:50] (current) – [xmodmap] niccolo
Line 17: Line 17:
 </code> </code>
  
-It is possibile to customize the ACPI events to reassign the brightness keys to plain function keys, but it is not the preferred way (see [[#acpi_and_evemu|ACPI and evemu]]), because it requires to run an additional sofware layer (the acpid daemon) and the execution of slow action scripts.+It is possibile to customize the ACPI events to reassign the brightness keys to plain function keys, but it is not the preferred way (see [[#acpi_and_evemu|ACPI and evemu]]), because it requires to run an additional sofware layer (the acpid daemon) and the execution of slow action scripts. The simplest method is to **[[#customize_events_using_udev_and_hwdb|customize input events using udev and hwdb]]**.
  
 It seems that the LCD (DISPLAYTOGGLE) multimedia function is intercepted by the hardware and it is not handled as an input event nor as an ACPI event by the operating system. It seems that the LCD (DISPLAYTOGGLE) multimedia function is intercepted by the hardware and it is not handled as an input event nor as an ACPI event by the operating system.
Line 111: Line 111:
 </code> </code>
  
-===== Customize events using udev =====+===== Customize events using udev and hwdb =====
  
 We will use **udev** and **hwdb** to customize the actions associated to the input events, basically to swap the effects of keys from **F2** to **F12** keys with the effects of the same keys combined with the **Fn** key. We will use **udev** and **hwdb** to customize the actions associated to the input events, basically to swap the effects of keys from **F2** to **F12** keys with the effects of the same keys combined with the **Fn** key.
Line 144: Line 144:
 </file> </file>
  
-We have two **device selector** linesthe ones starting with **evdev**. They must match the **AT keyboard** and the **Intel HID events** input device using the **name** and the **DMI** descriptor (for brevity we used the asterisk wildcard). To view both the name and the DMI descriptor, run the **evemu-describe** tool from the **evemu-tools** Debian package.+We have two **device selector** lines (the ones starting with ''evdev''). They must match the **AT keyboard** and the **Intel HID events** input devices using the **name** and the **DMI** descriptor (for brevity we used asterisk wildcards). To view both the name and the DMI descriptor, run the **evemu-describe** tool provided by the **evemu-tools** Debian package.
  
-After each selector there is a list of **key=value** entries one per linethey must be indented by one space and cannot contain comments. The //key// is the **MSC_SCAN** code generated when a key is pressed on the input device. The //value// is an **EV_KEY** event code that will be acted upon by the input subsystem.+After each selector there is a list of **key=value** entriesone per linethey must be indented by one space and cannot contain comments. The //key// is the **MSC_SCAN** code generated when a key is pressed on the input device. The //value// is an **EV_KEY** event code that will be acted upon by the input subsystem.
  
-You can get the label of **event codes** (to be placed at the right of the equal sign) by reading the **EV_KEY** part printed by ''evtest'' or by inspecting the file **/usr/include/linux/input-event-codes.h**. You must remove the **%%KEY_%%** prefix and convert the label to lowercase.+To know the **MSC_SCAN** codes (to be placed to the left of the equal sign) you can look at what is reported by the **evtest** command line tool. The numeric code must be prefixed with **KEYBOARD_KEY_**. 
 + 
 +Also the **EV_KEY** codes (to be placed to the right of the equal sign) can be obtained from the **evtest** command line tool, they are something line **KEY_F4** or **KEY_BRIGHTNESSUP**. You can know other codes by browsing the file **/usr/include/linux/input-event-codes.h**. You must remove the **%%KEY_%%** prefix and convert the label to lowercase.
  
 To update the **hardware database** and to trigger a kernel device **coldplug event**: To update the **hardware database** and to trigger a kernel device **coldplug event**:
Line 155: Line 157:
 systemd-hwdb update systemd-hwdb update
 udevadm trigger --verbose /dev/input/event0 udevadm trigger --verbose /dev/input/event0
 +udevadm trigger --verbose /dev/input/event7
 </code> </code>
 +
  
 You can also check that your changes were effective using **udevadm**: You can also check that your changes were effective using **udevadm**:
Line 164: Line 168:
 P: /devices/platform/i8042/serio0/input/input0/event0 P: /devices/platform/i8042/serio0/input/input0/event0
 ... ...
 +E: KEYBOARD_KEY_3d=volumedown
 E: KEYBOARD_KEY_3e=volumeup E: KEYBOARD_KEY_3e=volumeup
 +E: KEYBOARD_KEY_40=brightnessdown
 +E: KEYBOARD_KEY_41=brightnessup
 +...
 E: KEYBOARD_KEY_b0=f4 E: KEYBOARD_KEY_b0=f4
 ... ...
Line 173: Line 181:
 ===== Configuration example for the Teclast F6 notebook ===== ===== Configuration example for the Teclast F6 notebook =====
  
-The following is the file **/etc/udev/hwdb.d/90-custom-keyboard.hwdb** which I use on my Teclast F6 notebook:+The following is the file **/etc/udev/hwdb.d/90-teclast-f6-keyboard.hwdb** which I use on my Teclast F6 notebook:
  
 <file> <file>
-# /etc/udev/hwdb.d/90-custom-keyboard.hwdb +# /etc/udev/hwdb.d/90-teclast-f6-keyboard.hwdb 
-#+
 # Keyboard remapping for the Teclast F6 notebook. # Keyboard remapping for the Teclast F6 notebook.
-# 2021-05-13 Niccolo Rigacci <niccolo@rigacci.org>+# 2021-12-07 Niccolo Rigacci <niccolo@rigacci.org>
 # #
 # The following udev hwdb configuration swaps the Fn behaviour # The following udev hwdb configuration swaps the Fn behaviour
-# on keys F2, F3, F4, F8, F9, F10, F11 and F12.+# on keys F2, F3, F4, F6, F7, F8, F9, F10, F11 and F12.
 # It also binds the Fn+ESC to Ctrl+LeftWinLogo+Esc, which can # It also binds the Fn+ESC to Ctrl+LeftWinLogo+Esc, which can
 # be used as keyboard shortcut into XFCE or other desktop # be used as keyboard shortcut into XFCE or other desktop
 # environments to execute a script and toggle the touchpad. # environments to execute a script and toggle the touchpad.
 # #
-# Function keys F1, F6 and F7 cannot be remapped using +# Function key F1 cannot be remapped using udev Hardware 
-udev Hardware Database (as far as I know).+Database (as far as I know).
 # #
-# To make this file effective execute:+# To make this file effective execute (please check the 
 +# input number using lsinput):
 #   systemd-hwdb update #   systemd-hwdb update
 #   udevadm trigger --verbose /dev/input/event0 #   udevadm trigger --verbose /dev/input/event0
 +#   udevadm trigger --verbose /dev/input/event7
 # #
-# To view current binding:+# To view current bindings:
 #   udevadm info /dev/input/event0 #   udevadm info /dev/input/event0
 # #
Line 209: Line 219:
 #   KEYBOARD_KEY_76 => code 85  KEY_ZENKAKUHANKAKU #   KEYBOARD_KEY_76 => code 85  KEY_ZENKAKUHANKAKU
 # #
-Function keys F1, F6 and F7 do not generate events +Fn+F6 and Fn+F7 produce keys 64 and 65 (0x40 and 0x41).
-(verified with evtest and showkey), so they cannot +
-# be remapped. +
-+
-# The following EV_KEY do not work: +
-#   KEY_DISPLAYTOGGLE +
-#   KEY_BRIGHTNESSDOWN +
-#   KEY_BRIGHTNESSUP+
 # #
 +# Function keys F1 does not generate events (verified with
 +# evtest and showkey), so it cannot be remapped.
 +
 +# Teclast AT Keyboard input device.
 evdev:atkbd:dmi:bvnTECLAST:bvr*:bd*:svnTECLAST:pnF6:pvr* evdev:atkbd:dmi:bvnTECLAST:bvr*:bd*:svnTECLAST:pnF6:pvr*
  KEYBOARD_KEY_a0=f2  KEYBOARD_KEY_a0=f2
Line 230: Line 237:
  KEYBOARD_KEY_3d=volumedown  KEYBOARD_KEY_3d=volumedown
  KEYBOARD_KEY_3e=volumeup  KEYBOARD_KEY_3e=volumeup
 + KEYBOARD_KEY_40=brightnessdown
 + KEYBOARD_KEY_41=brightnessup
  KEYBOARD_KEY_42=previoussong  KEYBOARD_KEY_42=previoussong
  KEYBOARD_KEY_43=nextsong  KEYBOARD_KEY_43=nextsong
Line 236: Line 245:
  KEYBOARD_KEY_58=sysrq  KEYBOARD_KEY_58=sysrq
  KEYBOARD_KEY_76=esc  KEYBOARD_KEY_76=esc
 +
 +# F6 and F7 are connected to input "Intel HID events", not keyboard.
 +evdev:name:Intel HID events:dmi:bvnTECLAST:bvr*:bd*:svnTECLAST:pnF6:pvr*
 + KEYBOARD_KEY_14=f6
 + KEYBOARD_KEY_13=f7
 </file> </file>
  
Line 279: Line 293:
 </code> </code>
  
-These keysyms do not produce the expected action; i.e. they don't do anything, despite I associate them to a key and depite that ''evtest'' and ''xev''do report the expected action:+These keysyms do not produce the expected action; i.e. they don't do anything, despite I associate them to a key and despite that ''evtest'' and ''xev''do report the expected action:
  
   * XF86TouchpadToggle, XF86TouchpadOn, XF86TouchpadOff   * XF86TouchpadToggle, XF86TouchpadOn, XF86TouchpadOff
Line 285: Line 299:
  
 ===== ACPI and evemu ===== ===== ACPI and evemu =====
 +
 +**WARNING**: This method is deprecated: it cannot actually swap F6/F7 with Fn+F6/Fn+F7. Used alone it can rempap BrightnessDown and BrightnessUp with F6 and F7, but it cannot remap the vice-versa. It also requires the //acpid// service and it requires the execution of slow scripts on each keypress.
  
 With this recipe we simulate a **keyboard event** (e.g. a function key press/release) when an **ACPI event** occurs (eg. when the **BrightnessDown** or **BrightnessUp** buttons are pressed). With this recipe we simulate a **keyboard event** (e.g. a function key press/release) when an **ACPI event** occurs (eg. when the **BrightnessDown** or **BrightnessUp** buttons are pressed).
Line 322: Line 338:
 WARNING: The **sleep** command was determined empirically, may be it is required to let the default ACPI routine to complete. WARNING: The **sleep** command was determined empirically, may be it is required to let the default ACPI routine to complete.
  
-Once the acpid service is restarted, every time you press the **BrightnessDown** and **BrightnessUp** keys, you get the brightness adjusted and also the key **F6** or **F7** simulated events.+Once the acpid service is restarted, every time you press the **BrightnessDown** and **BrightnessUp** keys, you get the key **F6** or **F7** simulated events **along with the default brightness adjust**.
  
-Now you have to disable the default brightness control behaviour.+To disable (or remap) the default brightness control behaviour we need to customize the input subsystem, disabilng or remapping the **Intel HID events** device. In this case customizing ACPI becomes pointless: you can obtain all the required mapping using only udev and hwdb (see above), leaving ACPI alone.
  
-If the brightness keys are sent as **keyboard keycodes**, simply remap the relevant keycodes to the empty string. The keycodes can be discovered with: +On different hardware, if the multimedia key generates a plain **keyboard keycode**, you can use ''xmodmap'' to remap it, but the mapping only works under the Xserver environment.
- +
-<code> +
-xmodmap -display :0.0 -pke | grep -i monbright +
-keycode 232 = XF86MonBrightnessDown NoSymbol XF86MonBrightnessDown NoSymbol XF86MonBrightnessDown +
-keycode 233 = XF86MonBrightnessUp NoSymbol XF86MonBrightnessUp NoSymbol XF86MonBrightnessUp +
-</code> +
- +
-So we can disable keycodes 232 and 233: +
- +
-<code> +
-xmodmap -e 'keycode 232 =' +
-xmodmap -e 'keycode 233 =' +
-</code> +
- +
-Unfortunately this is not the case with the Teclast F6; the BrightnessDown and BrightnessUp events are not reported as keyboard keycodes. They are instead input events generated by the **Intel HID events** device. Use **lsinput** and **input-events** +
- +
-<code> +
-lsinput +
-... +
-/dev/input/event7 +
-   bustype : BUS_HOST +
-   vendor  : 0x0 +
-   product : 0x0 +
-   version : 0 +
-   name    : "Intel HID events" +
-   bits ev : (null) (null) (null) +
-</code> +
- +
-<code> +
-input-events -t 10 7 +
-18:45:12.250030: (null) ??? 20 +
-18:45:12.250030: (null) ??? (0xe0) pressed +
-18:45:12.250030: (null) code=0 value=0 +
-18:45:12.250066: (null) ??? (0xe0) released +
-18:45:12.250066: (null) code=0 value=0 +
-18:45:12.833473: (null) ??? 19 +
-18:45:12.833473: (null) ??? (0xe1) pressed +
-18:45:12.833473: (null) code=0 value=0 +
-18:45:12.833506: (null) ??? (0xe1) released +
-18:45:12.833506: (null) code=0 value=0 +
-</code> +
- +
-It is possible to disable the input events (but still receive the ACPI events). Discover the id: +
- +
-<code> +
-xinput +
-... +
-    ↳ Intel HID events                          id=12   [slave  keyboard (3)] +
-</code> +
- +
-and disable it: +
- +
-<code> +
-xinput disable 12 +
-</code>+
  
 ===== Web References ===== ===== Web References =====
doc/appunti/linux/sa/remap_keyboard_keys.1638869090.txt.gz · Last modified: 2021/12/07 10:24 by niccolo