3

How would I set custom mouse button actions on my Raspberry Pi?

Here's the idea:

I have a Pi with speakers with a mouse dongle connected. I want to dismantle a wireless mouse and use the already existing buttons (including right, left, DPI, etc)... and assign them to different actions such as "play sound 1", "play sound 2", "stop sound", etc...

If that doesn't make sense let me rephrase: I take all parts of the mouse off other than physical mechanical clickers, circuit board, and part for communication.

If I then align clickers in a row and put on top custom designed button covers (play, stop, volume, etc)... When I press a button I want the Pi to think it's not a left click or whatever but a "play sound 1" for example.

Will this have to be done through a program? Or is there a way for the Pi?

The Pi will not have a screen either. (I'm using a Raspberry Pi 2 by the way).

2
  • Are you using Raspbian PIXEL
    – user67191
    Commented Jul 1, 2017 at 22:19
  • No idea what that is. I have a raspberry pi 2 with raspbian on it. It is a version around 7 months old or so. Commented Jul 2, 2017 at 3:54

1 Answer 1

1

All these buttons are detected by the latest kernel (which as of writing is)

$ uname -a
Linux taichi 3.7.4-204.fc18.x86_64

This is great news because all we need to do now is map each button against the desired application/keystroke.

My system of choice is Ubuntu 10.10 as of writing, this has since changed to Fedora so all the command line and install command listed are correct for this distributions as of writing. If you find other commands work on other distributions leave a not in the command and I will be sure to update the main article.

The Install Instructions for a fresh install

We should install xbindkeys. This will re-map mouse and keyboard inputs so the install is…

$ sudo apt-get install xbindkeys

The goal is to configure the mouse buttons to send key combinations to activate o

ther desktop or application functionalities. Technically all xbindkeys is doing is executing an application in response to a keystroke or mouse button.

So in-order to map a mouse button to a keystroke we have to install an application called xte, which in Ubuntu comes as part of the xautomation package which can be installed like this

$ sudo apt-get install xautomation

Now we need to create a configuration file for xbindkey, which can be done like this…

$ xbindkeys --defaults > /.xbindkeysrc

We need to edit this file in a text editor. I use nano, but vim, kate or gedit are just as good but you can of course use what ever editor you would prefer to…

$ nano -w ~/.xbindkeysrc

The xbindkeys configuration file has a very simple format…

"command_to_run"
keyboard_or_mouse buttons

So we need to add our button-to-key configurations, but first we need to know what ‘events’ the mouse buttons are triggering before we can remap them. For this we can use xev, this program is a key and mouse event sniffer. It runs by opening a small window. You can now start pressing keyboard or mouse buttons over the window and see if they are detected. For example the forward button on my Logitech Revolution MX looks something like this:

ButtonRelease event, serial 31, synthetic NO, window 0x3200001,
  root 0x263, subw 0x3200002, time 6377530, (36,35), root:(38,122),
  state 0x10, button 13, same_screen YES

Bellow I’ve included a table for the Logitech MX mouse, these will probably be if you have a newer, or older mouse

This is the table!
Now that we have a list of all our button codes we can move on to actually writing the configuration file. For example I have this to map the Thumb Button Up and Thumb Button Down buttons to switching workspace in my Gnome shell, these keycombinations are specific to my setup so you will need to chnage them. What I’m doing here is pressing the ‘Windows’ key ‘Left Control’ then ether ‘Down’ or ‘Up’ then releasing the other two keys…

"'xte' 'keydown Super_L' 'keydown Control_L' 'key Down' 'keyup  Super_L' 'keyup Control_L'"
  b:8
"'xte' 'keydown Super_L' 'keydown Control_L' 'key Up' 'keyup Super_L'     'keyup Control_L'"
  b:9

The Rapup Now we make it all work

Thats all there is. Once you have setup your /.xbindkeysrc confirguration file, you just need to configure xbindkeys to run automatically on system startup. This is going to be different depending on your windows manager, but here are the steps for KDE and Gnome

Gnome3

There’s a tool in GNOME 3 which allows you to add, modify and remove autostart entries and you can run it by executing from a terminal or from the ALT+F2 dialog. Just run gnome-session-properties Click on “Add” Write ‘Xbindkeys’ in the Name and ‘/usr/bin/xbindkeys’ as the Command and press OK. And done

Open System Settings. Go to Advanced tab -> Autostart. Click on “Add Program…”. Write ‘/usr/bin/xbindkeys’ and press OK. A new dialog pops up. Press OK again. Done!

Source

I hope this helped you!

1
  • 1
    Thanks, this is perfect! Answered my question and described everything very well! Commented Oct 15, 2017 at 22:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.