Linux / Gnome: Turning two extra mouse buttons into Copy/Paste commands
The goal
Use my Microsoft mouse’s two extra buttons to do Copy and Paste on a Fedora 12 machine (Gnome 2.28.2). The problem is that different applications have different keystrokes. But the nice thing is that almost all applications will respond properly to Alt-e c for copy (that is, open the “Edit” menu, and choose C”). CTRL-C is problematic, because pressing it accidentally over a shell will halt the execution of whatever is running.
I should note that this doesn’t work with Xemacs. I’ll come to that some day, I suppose.
Doing it
Assuming the necessary packages are installed (see below), run xev and press the relevant mouse buttons over the window to detect which button generates what event.
Then setup xbindkeys’ initial configuration file (and wipe any existing settings, if present) with
xbindkeys --defaults > /home/eli/.xbindkeysrc
and edit the file, commenting out all examples, just to be safe.
Then add the following snippet to the same file:
# Copy with mouse "xte 'keydown Alt_L' 'key e' 'keyup Alt_L' 'usleep 10000' 'key c'" release + b:8 # Paste with mouse "xte 'keydown Alt_L' 'key e' 'keyup Alt_L' 'usleep 10000' 'key p'" release + b:9
Note that the relevant mouse buttons were found to be 8 and 9, using xev. The “release” keyword makes sure that the script is run upon release events, and not when the button is still pressed, because the target applications won’t execute the command while the button is pressed. The 10ms sleep between opening the menu and the command was necessary for Firefox to catch the command. Tweaking is the name of the game.
And finally, if xbindkeys isn’t run, just go
$ killall xbindkeys ; xbindkeys
Note that there’s no need to rerun xbindkeys after modifying its configuration file, as it’s reloaded automatically.
To have xbindkeys executed on each login (on a Gnome system) add the two following lines to /etc/gdm/PreSession/Default:
su -l $USER -c killall xbindkeys su -l $USER -c xbindkeys
Just a list of relevant utilities
(Not all were used for the original purpose)
- xev — Creates a small window, and prints out events related to it. Useful for mapping the codes of mouse events, for example
- xbindkeys — A daemon which catches key or mouse events, and executes shell commands accordingly (never tried it, though. yum install xbindkeys)
- xmodmap — Utility for modifying the mapping of keys, e.g. reverse the mouse buttons or certain keys on the keyboard (but not their function)
- xte and xautomation — Create fake key presses for running X applications automatically (yum install xautomation)
Reader Comments
very nice put up, i definitely love this web site, keep on it
Great and useful post, thank you! I’m using fedora 18.
I’m having problems setting a mouse button to do any xte or xdotool command. When I use xev to see what the mouse button triggers I get this:
FocusOut event, serial 31, synthetic NO, window 0x2200001,
mode NotifyGrab, detail NotifyAncestor
FocusOut event, serial 31, synthetic NO, window 0x2200001,
mode NotifyUngrab, detail NotifyPointer
FocusIn event, serial 31, synthetic NO, window 0x2200001,
mode NotifyUngrab, detail NotifyAncestor
KeymapNotify event, serial 31, synthetic NO, window 0x0,
keys: 4294967199 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0
32 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
KeyPress event, serial 31, synthetic NO, window 0x2200001,
root 0x9f, subw 0x0, time 573916, (45,4), root:(46,95),
state 0x50, keycode 40 (keysym 0x64, d), same_screen YES,
XLookupString gives 1 bytes: (64) “d”
XmbLookupString gives 1 bytes: (64) “d”
XFilterEvent returns: False
KeyRelease event, serial 31, synthetic NO, window 0x2200001,
root 0x9f, subw 0x0, time 573950, (45,4), root:(46,95),
state 0x50, keycode 40 (keysym 0x64, d), same_screen YES,
XLookupString gives 1 bytes: (64) “d”
XFilterEvent returns: False
KeyRelease event, serial 31, synthetic NO, window 0x2200001,
root 0x9f, subw 0x0, time 573982, (45,4), root:(46,95),
state 0x50, keycode 133 (keysym 0xffeb, Super_L), same_screen YES,
XLookupString gives 0 bytes:
XFilterEvent returns: False
As for this, I get a combination like Mod2+Mod4+ d for the mouse button press. But when I put this in to the .xbindkeysrc like for example:
#Mozilla
“firefox”
m:0x50 + c:40
Mod2+Mod4+d
It doesn’t work with the mouse button and I just keep getting a “d” everytime I press the button. Nevertheless if I press the keyboard key Super_R + d it does open firefox. But if I press the key Super_L + d it doesn’t. I managed to notice that pressing Super_L + d + d (pressing d repeteadly twice) opens firefox.
Somehow this mouse button is linked to my Super_L physical key. when I press Super_L by itself, it show’s the activities overview (the same as alt+F1 in the keyboard shortcuts, or going with the mouse pointer to the upper left corner activities menu). I did try the keyboard layout distribution map, to see which keys highlight when I press one on my physical keyboard. I noticed that when I press the right Super key, control, alt, etc, the appropriate key highlights on the map. Nonetheless when I press the left Super key it doesn’t highlight any keys on the layout map.
I do have xmodmap but haven’t done anything strange with it. This is the output I get when running xmodmap and seems to be fine.
xmodmap: up to 4 keys per modifier, (keycodes in parentheses):
shift Shift_L (0×32), Shift_R (0x3e)
lock Caps_Lock (0×42)
control Control_L (0×25), Control_R (0×69)
mod1 Alt_L (0×40), Alt_R (0x6c), Meta_L (0xcd)
mod2 Num_Lock (0x4d)
mod3
mod4 Super_L (0×85), Super_R (0×86), Super_L (0xce), Hyper_L (0xcf)
mod5 ISO_Level3_Shift (0x5c), Mode_switch (0xcb)
If you could help me with this, it would be great.
Thanks in advance for your help.
Hi guys if anyone still is visitng this site, I am trying to bind copy n paste ctrl c and v to the file for a while now have read many guides but can’t get the mapping to work in live environment ? any help is greatly appreciated