<h2>Remapping Mouse Buttons with XBindKeys on Linux</h2>

<p>After a really long time of not bothering to figure out how to do this, I finally did some research and found out how to remap the extra buttons on my Kensington Expert mouse and my Logitech marble trackball mouse in a Linux environment. The tools it needed were <code>xvkbd</code>, <code>xdotool</code>, and <code>xbindkeys</code>. I already had the first two installed, but had never used <code>xbindkeys</code> before. I also used <code>xev</code> to identify the button numbers and key numbers.</p>

<p>The Kensington Expert Mouse is one that I've had for about 15 years, and it was fairly expensive when I bought it, something like $75 or $80. It has four large buttons with a large trackball in the middle and a scroll wheel going around the track ball. I bought it at a time when I was doing a lot of graphic work that required clicking and dragging and double-clicking and stuff like that. If you're using it in a Mac or Windows environment, there is a special configuration tool that you can use to set it up just how you want. I had always configured it so that the upper left and upper right buttons were used for double-clicking and click dragging. This helped reduce a lot of strain on my hands. I have never gotten this to work on Linux, though, until today.</p>

<p>If you want to do this yourself, the first thing to do is make sure you have these packages installed: <code>xvkbd</code>, <code>xdotool</code>, and <code>xbindkeys</code>.</p>

Then create a configuration file in your home directory:

<pre>
~/.xbindkeysrc
</pre>

In order to map the upper left button to "double click," and the upper right button to "click and drag," I added these lines to the configuration file:

<pre>
# Double-click assigned to button 2 (upper left)

"xdotool sleep 0.2 click 1 ; xdotool click 1"
      b:2

# Click and Drag assigned to button 8 (upper right)

"xdotool sleep 0.2 mousedown 1"
      b:8
</pre>

To test the settings, simply kill the <code>xbindkeys</code> process and restart it by typing <code>xbindkeys</code>:

<pre>
user@hostname:~$ pkill -f xbindkeys
user@hostname:~$ xbindkeys
</pre>


<p>On my desktop computer I have a Logitech marble trackball mouse, and it has two small keys that are assigned to back and forward by default. This can be handy for navigating file managers and web pages, but I wanted them to be assigned to "page up" and "page down" (to make up for the lack of a scroll wheel on the mouse). Here is the configuration file for that machine:</p>

<pre>
"xvkbd  -text "\[Page_Down]""
       b:8

"xvkbd  -text "\[Page_Up]""
       b:9
</pre>

<p>I suppose I could have used <code>xdotool</code> for this configuration file as well, but for reasons I can't remember now, I tried <code>xvkbd</code> first and it worked, so I did not experiment further. I  used <code>xdotool</code> for the Kensington because <code>xvkbd</code> did not have a way to perform virtual mouse clicks.</p>


<h2>Links</h2>

<ul>
	<li><a href="https://manpages.ubuntu.com/manpages/jammy/en/man1/xev.1.html">xev</a></li>
	<li><a href="https://www.nongnu.org/xbindkeys/">XBindKeys</a></li>
	<li><a href="https://manpages.ubuntu.com/manpages/jammy/en/man1/xvkbd.1.html">xvkbd</a></li>
	<li><a href="https://manpages.ubuntu.com/manpages/trusty/man1/xdotool.1.html">xdotool</a></li>
</ul>
