0

Please help me solve the following problem: I have a USB pedal that I want to function as Control key for my work. So far system identifies that this pedal is working as F2 key (I am using Ubuntu 17) and I would like to remap the system to understand F2 key as Control key. I have tried several manuals, some of which refer to the Xmodmap solution which seem to be deprecated, another to XKB which I due to my stupidity have failed to understand. I am looking at some suggestions from you as to how can I make this happen. Thanks in advance.

1 Answers1

0

XKB method

XKB may not be the best way to change this device's behavior, but it should be possible in traditional X environments. Newer XKB contexts like Wayland or kmscon may not provide the granularity needed to configure separate XKB layouts on arbitrary devices. It also won't function under the traditional console.

Applying a udev hwdb override to make this change should work under all of these.


Under traditional X environments, setxkbmap takes a -device flag and xkbcomp uses -i:

-device device

Specifies the numeric device id of the input device to be updated with the new keyboard layout.

Use xinput -list to find the device id numbers.


To prove this functional, use the setxkbmap ... -print to generate a basic layout, save it to a file, then load that with xkbcomp filename.xkb $DISPLAY. You may need to disable GNOME/KDE's keyboard settings daemons while testing.

$ setxkbmap -layout us -option '' -print > f2toctrl.xkb

Edit the file f2toctrl.xkb -- we're only changing the xkb_symbols line:

// generated with `setxkbmap -print` and modified
// load: xkbcomp -i DeviceID# filename $DISPLAY
xkb_keymap {
    xkb_keycodes  { include "evdev+aliases(qwerty)" };
    xkb_types     { include "complete" };
    xkb_compat    { include "complete" };
    xkb_symbols   {
        include "pc+us+inet(evdev)"

        // existing key definition (see `xkbcomp $DISPLAY - | grep -A 3 FK02`)
        // key <FK02> {
        //    type= "CTRL+ALT",
        //    symbols[Group1]= [ F2, F2, F2, F2, XF86Switch_VT_2 ]
        // };

        // patterned after ctrl:nocaps option
        // see /usr/share/X11/xkb/symbols/ctrl
        replace key <FK02> { [ Control_L, Control_L ] };
        modifier_map  Control { <FK02>, <LCTL> };

    };
    xkb_geometry  { include "pc(pc105)" };
};

Now load it (replace N with the device id you found in xinput -list):

$ xkbcomp -i N f2toctrl.xkb $DISPLAY

You can verify your changes with xkbcomp -i N $DISPLAY - | less, and test the button physically in various applications to make sure it's being recognized.


For normal (non-DE) X environments, the loading command can be added to ~/.xinitrc or other windowmanager startup scripts. You'd need to add that override to the system's XKB database for use with most Wayland compositors and DEs like GNOME/KDE, but in those contexts I'm not certain how you'd apply the override to only the pedal device. If that were feasible, I'd create /usr/share/X11/xkb/symbols/pedal to hold the Ctrl override demonstrated above (and potentially other useful overrides for the device).

A very simplistic layout file might look like this:

// /usr/share/X11/xkb/symbols/pedal

// convert F2 key operation to Ctrl
default partial modifier_keys
xkb_symbols "basic" {
    name[Group1]= "USB Pedal as Ctrl";
    replace key <FK02> { [ Control_L, Control_L ] };
    modifier_map Control { <FK02>, <LCTL> };
};
quixotic
  • 1,292
  • 10
  • 12