Sunday, September 24, 2023
 Popular · Latest · Hot · Upcoming
rated 0 times [  11] [ 0]  / answers: 1 / hits: 5643  / 2 Years ago, fri, december 24, 2021, 8:45:37

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.

More From » keyboard


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.

[#44977] Saturday, December 25, 2021, 2 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.

Total Points: 71
Total Questions: 124
Total Answers: 106

Location: Honduras
Member since Sat, Jul 24, 2021
2 Years ago
losbu questions
Mon, Jan 31, 22, 21:41, 2 Years ago
Sun, Apr 23, 23, 06:18, 6 Months ago
Tue, Nov 22, 22, 01:27, 11 Months ago
Wed, Jan 25, 23, 00:28, 9 Months ago
Wed, May 11, 22, 08:41, 1 Year ago