You can do the first (mapping Caps Lock to Esc) but you cannot do the second.
Simple Explanation: Caps Lock is just not designed to do something when pressed.
Elaborate Explanation:
There are many types of keys. Two of them are Modifier Keys and Lock Keys.
Caps Lock is a Lock Key while keys such as Ctrl, Alt, and Shift are Modifier Keys.
It's apparent from their names that Modifier Keys modify the function of the next key(s) pressed, and Lock Keys act as a toggle switch to turn on or off some particular function.
Now, since Caps Lock is a Lock Key, you cannot have it behave like a Modifier Key without mapping it to one. Caps Lock by itself cannot function as a Modifier Key.
install the pre-requisites:
sudo apt-get install xbindkeys xautomation
Then create the xbindkeys config file:
touch .xbindkeysrc
bind the key:
Now we need to figure out what the keysym of your caps lock button is. Run
xev | grep key
Put your mouse into the box that appears and press the caps lock button. This is what I get.
The important part here is
(keysym 0xffe5, Caps_Lock),
Write the keysym (0xffe5
) down, you'll need it later.
Now edit the xbindkeys config file.
gedit .xbindkeysrc
And add the following to the file:
" xte 'key 0xffe5' "
b:2
(note, replace 0xffe5
with whatever number you got earlier. I bet it will be the same, but I'm not sure).
Save and close the file. Now run:
xbindkeys
You're done!
Caveat:
On my keyboard, this method didn't toggle the light on the caps lock key, (which is understandable), so you will probably lose any indicators you have that caps is enabled. Any software indicators should still work though.
Best Answer
From howtogeek.com which has a good answer for any OS.
For Linux (including Ubuntu):
Where
<OPTION>
can be for example of:caps:none
– Disables Caps Lock.caps:super
– Caps Lock becomes an additional Super (aka Win key).caps:ctrl_modifier
– Caps Lock becomes an additional Ctrl.caps:numlock
– Caps Lock becomes an additional Num Lock.caps:escape
– Caps Lock becomes an additional Escape.caps:backspace
– Caps Lock becomes an additional Backspace.caps:swapescape
– Caps Lock becomes Escape, and Escape becomes Caps LockNote: Xmodmap is deprecated in favor of setxkbmap.