Ubuntu – Map Ctrl and Alt to mouse thumb buttons

mousexbindkeys

I'm running Ubuntu 12.04 and have a multi-button Microsoft mouse. I would like to map the Ctrl and Alt modifier keys to the left and right thumb buttons of my mouse, respectively, so I can Ctrl + click and Alt-click without touching the keyboard.
My thumb buttons are buttons 8 and 9.

I tried the solution in How do I configure a mouse thumb button?
which explained how to map a double click to a thumb button – this worked for the double-click but I couldn't figure out how to modify the solution for Ctrl and Alt

I also tried How to map Ctrl/Shift to thumb buttons of Mouse?
which used xdotools and xbindkeys. I modified the script to this:

~/.xbindkeysrc:

"xdotool keydown alt"
        b:9
"xdotool keyup alt"
        release + alt + b:9
"xdotool keydown ctrl"
        b:8
"xdotool keyup ctrl"
        release + control + b:8

Which ALMOST works. It simulates a Ctrl-key press when I click the left thumb button, but I can't actually hold the button and click at the same time – holding the thumb button seems to prevent it from listening to other input until it is released.

Does anyone know how I can make my mouse thumb button actually work as a modifier key, so I can use thumb button + click instead of Ctrl + click?

Best Answer

i found a working solution here. it uses Easystroke (sudo apt install easystroke). kudos to @stuartr from ubuntuforums!

though there was one issue - re-mapped mouse click sporadically fired an original ('back' in my case) event. to avoid this, mouse button can be remapped to some unused number with xinput set-button-map (sudo apt install xinput). to re-map on every login i've created (exacutable) $HOME/.config/autostart/mouse-buttons.sh with this content:

$ cat .config/autostart/mouse-buttons.sh
#!/usr/bin/env bash

mi_mouse_id=$(xinput | grep 'GTech MI wireless mouse.*pointer' | sed 's/.*\tid=\([0-9]*\)\t.*/\1/')
xinput set-button-map $mi_mouse_id 1 2 3 4 5 6 7 20 9 10 11 12 13 14 15 16 17 18 19 20
$

this maps mouse button 8 to button 20. hopefully button 20 has no meaning. at least it has absolutely no effect for me. now it's time to re-map button 20 to Ctrl:

  1. Preferences tab: additional buttons -> add -> radio button 'Instant Gestures' -> click the mouse button of choice in the grey area (for me a 'back, thumb button' became '(Instantly) Button 20')
  2. Preferences tab: Select 'Autostart Easystroke'
  3. Actions tab: Add Action
     Name: anything you like (e.g. 'Mouse 20 -> Ctrl')
     Type: 'Ignore'
     Details: click it once to change 'Ignore' to 'Key combination...'. then press Ctrl + a. 'a' doesn't matter and is ignored. 'Key Combination' will be replaced with 'Ctr'
  4. With the new action selected/highlighted -> click 'Record Stroke' -> press the mouse button you're wanting to use again (this came up with '20 -> 20' in the Stroke column for me)
  5. Now pressing and holding my mouse button brings up a dinky 'Ctr' on the screen and acts like the button is being held for as long as the mouse button is