User Tools

Site Tools


ubuntu:mouse:map_mouse_buttons_to_arbitrary_functions

Ubuntu - Mouse - Map Mouse Buttons to Arbitrary Functions

There are various options that can be used to allow a mouse button click to action an arbitrary function.

These include:

  • xbindkeys allows you to map any mouse (or keyboard) event to some program.
  • xautomation provides the xte program; which can generate fake input, including keystrokes and mouse-clicks.
  • xvkbd can send a keyboard key signal or mouse click to other programs.
  • xdotool can simulate keyboard input and mouse activity, move and resize windows, etc.

Install xbindkeys

sudo apt install xbindkeys

NOTE: Xbindkeys is a program that allows to bind commands to certain keys or key combinations on the keyboard.

  • Xbindkeys works with multimedia keys and is window manager independent.

Install xautomation

sudo apt install xautomation

NOTE: This provides the xte program; which can generate fake input, including keystrokes and mouse-clicks.


Create the xbindkeys Configuration File

xbindkeys --defaults > $HOME/.xbindkeysrc

NOTE: The $HOME/.xbindkeysrc file could also be created manually.

NOTE: The format of this file is a series of paired lines.

  1. The first line is a command to be executed for an event;
    • The quotation marks (“”) around the command is required.
  2. The second line is the associated mouse button.
    • The first mouse button (left) is called b:1, the second b:2 and so forth.

Assign a function to a Mouse Button

Edit the xbindkeys Configuration file, ~/.xbindkeysrc:

Add commands similar to this:

~/.xbindkeysrc
"xte 'keydown Home' 'keyup Home'"  
b:6

NOTE: This binds button 6 to the home key

  • Raw keycodes can be used too, for example:
    "xte 'keydown Home' 'keyup Home'"
    m:0x0 + b:6 

See: Identify the key codes to use


Another examples

~/.xbindkeysrc
# Refresh
"xte 'key F5'"
  b:4 + control

NOTE: This maps:

  • This will cause the forward mouse button and the CTRL key to emulate pressing the F5 key.
  • This is useful for refreshing a web page without moving your hand off the mouse.

Other examples

~/.xbindkeysrc
# Back
"xte 'keydown Alt_L' 'key Left' 'keyup Alt_L'"
 b:8
 
# Forward
"xte 'keydown Alt_L' 'key Right' 'keyup Alt_L'"
  b:9

NOTE: This maps:

  • Button 8 is mapped to ALT+LEFT.
  • Button 9 is mapped to ALT+RIGHT.

More complex examples

~/.xbindkeysrc
"/usr/bin/xte 'mouseclick 1' 'mouseclick 1' &"
b:8 + Release

NOTE: This maps a double-left-click to button 8.

  • The Release option catches release events instead of press events.

Restart xbindkeys

If any changes are made to the config file then xbindkeys needs to be restarted:

killall xbindkeys
xbindkeys

References

ubuntu/mouse/map_mouse_buttons_to_arbitrary_functions.txt · Last modified: 2021/10/26 08:57 by peter

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki