How do I change the keymap of a single device (Logitech presenter)?

My Logitech R400 presenter sends the following key codes:

keycode 112 (keysym 0xff55,  Prior) [page up]
keycode 117 (keysym 0xff56,   Next) [page down]
keycode  60 (keysym   0x2e, period)
keycode  71 (keysym 0xffc2,     F5)
keycode   9 (keysym 0xff1b, Escape)

Basically, I want to use this presenter to remotely control Banshee, so I'd like to remap Prior to B, Next to N, and so on.

How can I change the keymap for this device only (as I certainly don't want to remap those key codes globally)?


Solution 1:

I never did that but I think you need to create a keymap file in /lib/udev/keymaps/ and add a rule for your device to /lib/udev/rules.d/95-keymap.rules


Details:

  • An udev keymap maps scan codes to key codes, so you need to find both to create the keymap file. To find the scan codes run the following and press the buttons on the device:

    sudo /lib/udev/keymap -i input/event5
    

    You might need to try different numbers for event*. The output should look like this:

    scan code: 0x70037   key code: dot
    scan code: 0x70029   key code: esc
    scan code: 0x7003E   key code: f5
    scan code: 0x7004B   key code: pageup
    scan code: 0x7004E   key code: pagedown
    

    A list of key codes can be found here.

    Now create the keymap file (I'm storing it as /lib/udev/keymaps/logitech-r400):

    0x70037 brightnessdown
    0x70029 brightnessup
    0x7003E brightnessdown
    0x7004B brightnessup
    0x7004E brightnessdown
    

    Doesn't do anything useful - just a test to easily see if it works.

  • To test the new keymap temporarily, run:

    sudo /lib/udev/keymap input/event5 /lib/udev/keymaps/logitech-r400
    

    which should result in output like this:

    Remapped scancode 0x70037 to 0xe0 (prior: 0x34)
    Remapped scancode 0x70029 to 0xe1 (prior: 0x01)
    Remapped scancode 0x7003e to 0xe0 (prior: 0x3f)
    Remapped scancode 0x7004b to 0xe1 (prior: 0x68)
    Remapped scancode 0x7004e to 0xe0 (prior: 0x6d)
    

    The buttons should now change the brightness.

  • If you want to make that change permanent, you need to add an udev rule to /lib/udev/rules.d/95-keymap.rules that applies the keymap file to the device.

    1. Open the file: gksudo gedit /lib/udev/rules.d/95-keymap.rules
    2. At the bottom of the file, but before the LABEL="keyboard_end" line, add:

      ENV{ID_VENDOR}=="Logitech*", ATTRS{idProduct}=="c52d", RUN+="keymap $name logitech-r400"
      
    3. Save, close, and after a reboot the buttons should change the brightness.

Solution 2:

I'm on Ubuntu 16.04. I made a guide here

Detecting with evtest

sudo evtest

Look at the device list :

/dev/input/event0:  Lid Switch
/dev/input/event1:  Power Button
/dev/input/event2:  Sleep Button
/dev/input/event3:  Power Button
/dev/input/event4:  AT Translated Set 2 keyboard
/dev/input/event5:  Video Bus
/dev/input/event6:  USB Optical Mouse
/dev/input/event7:  AlpsPS/2 ALPS DualPoint Stick
/dev/input/event8:  AlpsPS/2 ALPS DualPoint TouchPad
/dev/input/event9:  Logitech USB Receiver
/dev/input/event10: Logitech USB Receiver
/dev/input/event11: HDA Intel PCH Dock Mic
/dev/input/event12: HDA Intel PCH Headset Mic
/dev/input/event13: HDA Intel PCH Dock Line Out
/dev/input/event14: HDA Intel PCH Headphone
/dev/input/event15: HDA Intel PCH HDMI/DP,pcm=3
/dev/input/event16: HDA Intel PCH HDMI/DP,pcm=7
/dev/input/event17: HDA Intel PCH HDMI/DP,pcm=8
/dev/input/event18: Integrated Webcam
/dev/input/event19: Dell WMI hotkeys
Select the device event number [0-19]: 9
Input driver version is 1.0.1
Input device ID: bus 0x3 vendor 0x46d product 0xc52d version 0x111
Input device name: "Logitech USB Receiver"

Took the first "Logitech USB Receiver", here it has event id 9.

Testing ... (interrupt to exit)

OK now evtest wants inputs, just click on the Logitech remote buttons, I got this (simplified):

[>] value 7003e (KEY_PRESENTATION)
[>] value 70029 (KEY_PRESENTATION)
[ ] value 70037 (KEY_DISPLAYTOGGLE)
 <  value 7004b (KEY_PAGEUP)
 >  value 7004e (KEY_PAGEDOWN)

Note: I discovered after that clicking multiple times on [>] was giving 7003e, then 70029, then 7003e, etc. I don't know why.

Modifying udev conf

Now that we have input codes, let's have a look at udev conf:

sudo gedit /lib/udev/hwdb.d/60-keyboard.hwdb

Search for "R400" and replace presentation & displaytoggle by what you want, playpause & stopcd arrow keys:

# Logitech Presenter R400
evdev:input:b0003v046DpC52D*
 KEYBOARD_KEY_070029=playpause    # bottom  left [>] was "presentation"   
 KEYBOARD_KEY_07003e=playpause    # bottom  left [>] was "presentation"  
 KEYBOARD_KEY_070037=stopcd       # bottom right [ ] was "displaytoggle"

As previously noted, because 07003e & 070029 are the same button, I gave them the same output up key. The 7004b & 7004e does not appear here by default, maybe because they are native PAGEUP & PAGEDOWN inputs and not related to "Logitech Presenter R400". Let's add them:

 KEYBOARD_KEY_07004b=previoussong #    top  left  <
 KEYBOARD_KEY_07004e=nextsong     #    top right  >  

Here is my final conf:

 KEYBOARD_KEY_07004b=previoussong #    top  left  <
 KEYBOARD_KEY_07004e=nextsong     #    top right  >
 KEYBOARD_KEY_070029=playpause    # bottom  left [>] was "presentation"
 KEYBOARD_KEY_07003e=playpause    # bottom  left [>] was "presentation"
 KEYBOARD_KEY_070037=stopcd       # bottom right [ ] was "displaytoggle"

Reloading (new) rules

sudo udevadm hwdb --update

Then by using the same event id we chose before, here event id 9:

sudo udevadm trigger /dev/input/event9

You can check this new mapping in a web browser, for example:

$('body').on('keydown', function(e){ console.log(e.key) })

That gave me:

[>] ArrowUp
[ ] ArrowDown
 <  ArrowLeft
 >  ArrowRight