How can I assign a middle button press to a specific key in my laptop?

How can I attach a middle button press to a specific key in my laptop? I can do middle-button if I tap on the top right corner of the touchpad, but I would like to associate it to a key, like the "windows" key or the "menu" key.

Is there a way to do that?

The current answer doesn't work for Ubuntu 11.10 with a classic gnome-failsafe desktop. I would need someone to figure out how can this be done for the updated Ubuntu version.


To remap the menu key you will need to-do the following.

install prerequisites

Install package xkbset:

sudo apt-get install xkbset

define keyboard layout

Next you need to define what keyboard layout you are using. In my case gb for a british/united kingdom keyboard layout or us for United States

setxkbmap -layout gb

Note: if you run

setxkbmap -query

it will give you the output as follows - if the model and layout is your keyboard then you are using the correct layout

rules:      evdev
model:      pc105
layout:     gb

Next define that we will be next mapping mouse keys:

xkbset m
xkbset exp =m

find Menu key-code

Map the Menu key. To do this we need the Menu key-code. In a terminal run xev, move your mouse inside the square and press the Menu key.

enter image description here

End Result

In my case my Menu key-code is the value 135 and this will be mapped to Mouse Button 2

xmodmap -e "keycode 135 = Pointer_Button2"

Tested as follows:

  1. Copy some text in a terminal - Paste action initiated
  2. Hover mouse over title bar - Window move to background

Putting this all together:

Ok, once we've worked out the correct keycodes, we can put this together to start from login.

First create a file in your home folder called remapkeys

gedit ~/remapkeys

now copy and paste the following

#!/bin/sh
setxkbmap -layout gb
xkbset m
xkbset exp =m
xmodmap -e "keycode 135 = Pointer_Button2"

Save. Give the file execute rights

chmod +x ~/remapkeys

Then from Dash search for Startup Applications or if using Gnome Classic - Applications - Other - Startup Applications

Add a new Startup Applications entry

enter image description here

The command should be:

bash -c "sleep 5 && ~/remapkeys"

The command sleeps for 5 seconds to allow your desktop to appear before running your new script - if your desktop doesnt appear within 5 seconds (maybe you are using a slow netbook?) then increase this timeout to 10 or 15 seconds. Logout and login to test.


First, install xautomation:

sudo apt-get install xautomation

You can send a middle-click event using xte:

xte 'mouseclick 2'

You can create a custom shortcut for that under Preferences -> Keyboard Shortcuts (or 'Keyboard Shortcuts' from the Unity search). Click 'Add', enter a name and the above command and click 'Apply'. Scroll to the bottom and set a shortcut as you would for any other action.

When I tried this, I wasn't able to bind the action to just the Windows key. You can try it by doing the following, but there seems to be a bug that prevents this from working:

Open a terminal and run

gconf-editor

Hit Ctrl+f, select the 'Search also in key values' box and enter the following to the search box:

xte 'mouseclick 2'

There should be only one result in the bottom pane. If there's more, select the one that contains '/desktop/gnome/keybindings/custom*/action' in its name. In the top-right pane, enter '' (without quotes) next to the 'binding' field. If you get the same result that I did, all your window appearances will change, and the new keybinding won't do anything. Change that back to whatever it was (or make it blank) and open Appearance (Preferences -> Appearance or 'Appearance' under unity search). That should fix the issue.


I'd note, for others (since I found this several times while looking for my own mouse) that some Logitech mice have a few buttons (on my MX1100, they were by default "DPI +/-" buttons) that do not send a signal to the USB receiver.

It is possible to override this and have the mouse report the button presses to your mouse driver, but the process is not even close to user-friendly (or even easily replicable, since it appears to be different for each mouse). The "lomoco" package will apparently work for some users, but has not been updated for newer mice in a while.