Capturing key input from events device and mapping it (toggle TouchPad key is unmapped)

10,097

Solution 1

As it turns out the kernel did pick it up, but kept complaining that it's not recognised.

For anyone else having this issue, or wants to map a key that's not read by the OS, read on.

Open a terminal and run dmesg | grep -A 1 -i setkeycodes. This will give you multiple entries like this:

[    9.307463] atkbd serio0: Unknown key pressed (translated set 2, code 0xbe on isa0060/serio0).
[    9.307476] atkbd serio0: Use 'setkeycodes e03e <keycode>' to make it known.

What we are interested is the hexadecimal value after "setkeycodes", in this case this is e03e. If you have multiple of these, you can run tail -f /var/log/kern.log. Once you've done so, you can tap the button you're looking for, and this will give you a the same line as above, and again, we only need the hexadecimal value. Make a note of this.

Now run xmodmap -pke | less and find the appropriate mapping. In my case, I needed to map this to toggle my touch pad, which means I was interested in the following line:

keycode 199 = XF86TouchpadToggle NoSymbol XF86TouchpadToggle

If you can't find whatever you're interested in, read @Gilles answer too, as you can define custom mappings too, then read on (if the kernel reads it, you won't need to add it to xorg.conf.d)

Now I ran the following command: sudo setkeycodes [hexadecimal] [keycode], so in my case that became: setkeycodes e03e 199.

Now you can use the following line to test if it worked and/or you have the correct mapping:

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

When you run this command, you need to focus on the newly opened window (xev) and check the console output. In my case it read as following:

207 NoSymbol

This was obviously wrong, as I requested keycode 199, so it's mapped to XF86TouchpadToggle. I checked xmodmap -pke again, and noticed that keycode 207 is actually mapped to NoSymbol, and I noticed that there was an offset difference of 8, so I tried the setkeycodes command again, but the key is mapped to keycode 191.

sudo setkeycodes e03e 191

This worked perfectly.

EDIT -- the solution I provided to have to working on start up does not. I will figure this out tomorrow and update this answer. For now I suppose you can run this on start up manually.

Solution 2

From the looks of it, the device is a keyboard (recognized in software as separate from the “core” keyboard) but the X server isn't listening to it.

Try adding an Input section under /etc/X11/xorg.conf.d. That is, create a directory /etc/X11/xorg.conf.d if it doesn't already exist, and create a file extra-keyboard-buttons in that directory containing

Section "InputClass"
    Identifier "extra-keyboard-buttons"
    Driver "kbd"
    MatchProduct "AT Translated Set 2 keyboard"
EndSection

I'm not sure about that MatchProduct line; if it doesn't work, try fiddling with the MatchXXX directives.

Once this file is set up correctly, when you start the X server, it should process events from this keyboard, and you should see events in xev and elsewhere. Note the keycode (I'm not sure if the X11 keycode will be the same as the hardware keycode in this case) and associate a keysym to it. You can do it via a file called .Xmodmap in your home directory containing

keycode 190 = XF86_Launch5

You can now define keyboard shortcuts using these keys.

To toggle the touchpad, use the command xinput. Run xinput list to see what input devices are available and note the name of your touchpad (I'll call it Touchpad below). Run xinput list-props Touchpad to list its properties; one of them should be called “Device Enabled” with a number next to it (e.g. 135). The following script will toggle the touchpad:

#!/bin/sh
device_name='Touchpad'
property=135
flipped_state=$(xinput list-props "$device_name" |
                awk "/\\($property\\)/ {print 1 - \$NF}")
xinput set-prop "$device_name" "$property" "$flipped_state"

Save this in a file ~/bin/toggle-touchpad and make it executable (chmod +x ~/bin/toggle-touchpad). To bind it to a key, install xbindkeys and configure it to run your script by putting the following lines in ~/.xbindkeysrc:

"toggle-touchpad"
  XF86_Launch5
Share:
10,097

Related videos on Youtube

Hosh Sadiq
Author by

Hosh Sadiq

Updated on September 18, 2022

Comments

  • Hosh Sadiq
    Hosh Sadiq almost 2 years

    I have a Lenovo IdeaPad Yoga 13 with Ubuntu 13.10 Installed. The device has a "Toggle TouchPad" button on the keyboard (F5). The keyboard's F* buttons are reversed (so to get F5, I need to press Fn + F5, and F5 is actually the toggle key).

    I've found out that the button is actually read by the keyboard (rather than the TouchPad like certain devices), which is at /dev/input/event3. So using sudo input-events 3 I was able to figure out that the button sends the scan code 190:

    Output of sudo lsinput:

    /dev/input/event3
       bustype : BUS_I8042
       vendor  : 0x1
       product : 0x1
       version : 43907
       name    : "AT Translated Set 2 keyboard"
       phys    : "isa0060/serio0/input0"
       bits ev : EV_SYN EV_KEY EV_MSC EV_LED EV_REP
    

    Output of sudo input-events 3:

    23:13:03.849392: EV_MSC MSC_SCAN 190
    23:13:03.849392: EV_SYN code=0 value=0
    23:13:03.855413: EV_MSC MSC_SCAN 190
    23:13:03.855413: EV_SYN code=0 value=0
    

    No other programs (such as xev) seem to be able to read it except for input-events. Is there any way to map this button to make it toggle the TouchPad on my laptop? If so, how can I do so?

  • Hosh Sadiq
    Hosh Sadiq over 10 years
    Thanks for the answer. This unfortunately didn't work. I got it working though, so feel free to have a look at my own answer! :)
  • vak
    vak over 7 years
    how to make it permanent in the nice way? (i.e. after reset/boot-up)