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

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.

  • Can I ask, after following these steps, should be binding be permanent or should I put it somewhere so that it's loaded every time I reboot?
    – 719016
    Oct 28 '11 at 15:51
  • certainly not permanent - I would add the statements to a script and run as part of startup applications so it runs on login. Does the answer work for you - it did in my testing...
    – fossfreedom
    Oct 28 '11 at 15:59
  • it works but as you say not permanently
    – 719016
    Oct 28 '11 at 17:32
  • ... just in-case I haven't made it clear, I've added how to start this from login to the answer.
    – fossfreedom
    Oct 28 '11 at 20:13
  • Doe is allow drag and drop? Feb 8 '19 at 12:49

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


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.

  • Great! I didn't need to do the gconf-editor bit, so far it's working, I'll double-check after I reboot. Thx!
    – 719016
    May 26 '11 at 11:52
  • This is not working anymore in Ubuntu 11.10 with a classic gnome-failsafe desktop. I am sure there must be a way of doing the same though.
    – 719016
    Oct 26 '11 at 8:56

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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