He, Peng

A Roboticist.

Robotic is the integration of all human knowledge, so far.

Bind Mouse Keys & Swap Ctr and Alt in Ubuntu and Windows

Abstract: As an Ubuntu user, sometimes you want to use the function keys from a mouse, for example, the Logitech ones. However, there is no such a software offered by their website to do the same thing as in Windows. This blog will give a quick instruction on how to set it up.

Bind Mouse Key to a function key combo in Ubuntu

To use the feature on Ubuntu, you need install XEV, it may come with the system, if not, install it.

sudo apt-get install xbindkeys xautomation
xbindkeys --defaults > $HOME/.xbindkeysrc
gedit ~/.xbindkeysrc 

This will create your keybinding config file, after open it type xev in a terminal, it will pop up a small window and you should move your mouse into the window then click the key you want and finally confirm the key number on your terminal outputs.

My script for a Logitech M570 trackball mouse. Copy this to the ~/.xbindkeysrc if you like.

## Mouse lower button trace back to last page
"xte 'keydown Control_L' 'keydown Shift_L' 'key Tab' 'keyup Shift_L' 'keyup Control_L'"
## Mouse upper button close current tab
"xte 'keydown Control_R' 'key w' 'keyup Control_R'"


Swap Control key with Alt key on Ubuntu and Windows

The reason for this is to make my brain not being tire apart by frequently swapping between a Unix key system(MAC OS) and a Windows key system (Ubuntu & Windows).

Major problem being that in MacOS, your command key (⌘) is dominant to most of the things, like ⌘+W is "close a tab", and ⌘+R is "refresh the page". Which for the same effect in Window, is CTR+W or CTR+R.

However, the CMD Key and CTR key are in a different position on a regular keyboard. I don't know why but if we can swap one to the another, then we will make our life way easier. Because swapping the key position will make the key value unchanged to the applications, this is very important because it keeps the rest of your lift remain organized.

For example, usually the shortcuts for "closing a tab" with a same program in Window is CTR+W and in Mac is ⌘+W. So if we can make whatever the key in the position of ⌘ on a Mackeyboard sends out a value that is "CTR" when it is in Windows, this will make you use a same "chord" to do the same function and differernt systems.

In our case, I swap the Windows CTR to Mac ⌘ location because I feel most comfortable with this "chord" composition.

And now we have two solutions:

  • make a physical keyboard and has different CTR position.
  • auto-start a software that swap the keyinput values.

OBS the second one wins, it makes you setting free from any keyboard and you don't need to physically swap the key when attach to a MAC.


It is very easy in window, just use Sharpkey to create two mappings, even they say you are under a risk to use the software on Win10, but I find it works perfectly indeed.

Screenshot of the mapping


While in Ubuntu, it is a little bit hard.
I found this website being very useful http://xahlee.info/linux/linux_xmodmap_tutorial.html.

Ubuntu usually come with xmodmap, for some reason if you don't have it, you need to figure out how to install it.

Then do the following.

~$ gedit ~/.Xmodmap

That will create the file and open it in gedit. Add the following lines to the file:

! -*- coding: utf-8 -*-
! 2013-02-04

! swap Ctrl and Alt keys on both left and right

! here's the default setting on special keys
!  xmodmap -pke | grep -P '(Control|Super|Alt|Menu)'

! keycode  37 = Control_L NoSymbol Control_L
! keycode  64 = Alt_L Meta_L Alt_L Meta_L
! keycode 105 = Control_R NoSymbol Control_R
! keycode 108 = Alt_R Meta_R Alt_R Meta_R
! keycode 133 = Super_L NoSymbol Super_L
! keycode 134 = Super_R NoSymbol Super_R
! keycode 135 = Menu NoSymbol Menu
! keycode 147 = XF86MenuKB NoSymbol XF86MenuKB
! keycode 204 = NoSymbol Alt_L NoSymbol Alt_L
! keycode 206 = NoSymbol Super_L NoSymbol Super_L

clear control
clear mod1
keycode 37 = Alt_L Meta_L
keycode 105 = Alt_R Meta_R
keycode 64 = Control_L
keycode 108 = Control_R
add control = Control_L Control_R
add mod1 = Alt_L Meta_L

Save the file and quit gedit. Next time you login the new keymappings will be active. To have the settings take immediate effect run the following command:

~$ xmodmap ~/.Xmodmap

I will also put this line in the .bashrc to quickly export it when the auto-launch not working.

alias sskey='xmodmap ~/.Xmodmap'

Auto launch the script and start-up

I tried all the methods I can find on line, NONE of them work in this case, so far. From my experience, this is because the Xmod needs to run under your current user action. I tried to use all abs paths and put config files in the root space, but still, not a sigal way working.

One thing I found working is you set a delay timer in the desktop application launch around 2s. however, I have spent too much time on this and just end up using type a terminal alias.

Method 1:

To auto start the config file:
launch the Startup Applications App (search from the launch) to add an new entry. and put this in the command param: xmodmap ~/.Xmodmap

Method 2:

Manually create the auto launch shortcut.

Create a file like this:

touch ~/.local/share/applications/xmodmap.desktop 
gedit ~/.local/share/applications/xmodmap.desktop 

Then put down the configs.

[Desktop Entry]
Exec=xmodmap ~/.Xmodmap
Name[en_US]=Swap CTL with ALT
Name=Swap CTL with ALT
Comment[en_US]=call to start the xmod, needs the terminal bit to be true
Comment=call to start the xmod

Method 3:

Create a start up service using systemd.

Something in mind:
The key point is this needs to be run from a terminal, so set the terminal as true. (I spent two hours on figuring out this)

Recent Posts

Robotics 101: Sharpening The Tools -- Shorthands For Environment Setups

Abstract: After month and month working in robotics, I realize that a good knowledge of tooling coul…

In ConfigsRead More
Earlier Posts

Dinosaur Classifier Using Interesting Key Points, OpenCV

Abstract: This post will introduce two ways of classify an object using the OpenCV library. It is ve…

In Study & Research, Projects, Python, OpenCVRead More
comments powered by Disqus