Linux – Is it possible to remap the Back/Forward keys of a Thinkpad USB keyboard on Linux

keyboardlinuxthinkpadUbuntuusb

On my Thinkpad laptop I can easily change the Back/Forward keys to something else (I usually map them to PageDown/PageUp), as they have their own keycode (166 and 167):

xmodmap -e 'keycode 166=Prior'
xmodmap -e 'keycode 167=Next'

However, on my Thinkpad USB keyboard, the Back/Forward keys seem to be hard-mapped to Alt+Left and Alt+Right, respectively, so I cannot use xmodmap to remap them. Is there any other way I can remap them? I've already tried xautomation, but that didn't work.

This is what 'xev' shows me when I press the Forward key:

KeyPress event, serial 36, synthetic NO, window 0x4e00001,
    root 0xb4, subw 0x0, time 65023191, (16,-20), root:(641,33),
    state 0x0, keycode 64 (keysym 0xffe9, Alt_L), same_screen YES,
    XLookupString gives 0 bytes: 
    XmbLookupString gives 0 bytes: 
    XFilterEvent returns: False

KeyPress event, serial 36, synthetic NO, window 0x4e00001,
    root 0xb4, subw 0x0, time 65023191, (16,-20), root:(641,33),
    state 0x8, keycode 114 (keysym 0xff53, Right), same_screen YES,
    XLookupString gives 0 bytes: 
    XmbLookupString gives 0 bytes: 
    XFilterEvent returns: False

KeyRelease event, serial 36, synthetic NO, window 0x4e00001,
    root 0xb4, subw 0x0, time 65023271, (16,-20), root:(641,33),
    state 0x8, keycode 64 (keysym 0xffe9, Alt_L), same_screen YES,
    XLookupString gives 0 bytes: 
    XFilterEvent returns: False

KeyRelease event, serial 36, synthetic NO, window 0x4e00001,
    root 0xb4, subw 0x0, time 65023271, (16,-20), root:(641,33),
    state 0x0, keycode 114 (keysym 0xff53, Right), same_screen YES,
    XLookupString gives 0 bytes: 
    XFilterEvent returns: False

Best Answer

  1. Install xdotool
  2. Create a script or shell alias for the keypress command you want to run (xdotool key --clearmodifiers Page_Down)
  3. Bind that script/alias to the sent combo (Alt+Left) in your desktop environment's Hotkey/Shortcut config.

Note that in gnome-control-center it doesn't seem to be possible to assign the Back/Forward buttons of the USB keyboard to a shortcut (you can actually assign them, but the command/action you mapped to it will never be called/happen), so you might need to use something else, like compiz' Commands plugin.

Related Question