techWebsite/content/posts/capsToCtrl.adoc

103 lines
4.7 KiB
Plaintext
Raw Normal View History

2023-06-22 09:43:19 +02:00
---
title: "Mapping caps lock to ctrl in the TTY"
date: 2021-05-23T04:59:28-05:00
draft: false
2023-06-22 09:43:19 +02:00
---
:toc:
:toclevels: 4
2023-06-22 09:43:19 +02:00
2021-05-23 19:54:36 +02:00
= Caps to Ctrl in tty
In the past 2 years or so, I have been using my caps lock key as a seperate ctrl key on my desktop keyboard.
This is very easy to do in X11 with a setxkmap command.
However, with my laptop, I try to run without X as much as possible. (Ive found it make a nice, distraction free environment, and it seems to be pretty good for battery life)
Obviously, without X, we cannot use setxkmap.
In order to do this without the tools in setxkbmap, we will have to edit the keymap used by the vitual console and set it as the keymap using localectl.
Now, according to the archwiki, we should be able to create a file contianing
2023-06-22 09:43:19 +02:00
{{<highlight console "linenos=false">}}
2021-05-23 19:54:36 +02:00
keycode 58 = Control
2023-06-22 09:43:19 +02:00
{{</highlight>}}
2021-05-23 19:54:36 +02:00
and be done with it.
However, if we do this, we will notice a somewhat odd bug.
When we hold down caps lock and press another key, the kernel starts sending control- keycodes.
However, when we release caps lock, the kernel continues to send control- keycodes.
From what I can tell, the only way to \'release\' control is to reboot.
In order to figure out why this is happening, we read the man page 'man keymaps'.
2023-06-22 09:43:19 +02:00
[NOTE]
====
You should be very careful when binding the modifier keys, otherwise you can end up with an unusable keyboard mapping.
2021-05-23 19:54:36 +02:00
If you for example define a key to have Control in its first column and leave the rest of the columns to be VoidSymbols, you're in trouble.
This is because pressing the key puts Control modifier in effect and the following actions are looked up from the fifth column (see the table above).
So, when you release the key, the action from the fifth column is taken.
It has VoidSymbol in it, so nothing happens.
This means that the Control modifier is still in effect, although you have released the key.
Re-pressing and releasing the key has no effect.
2023-06-22 09:43:19 +02:00
====
2021-05-23 19:54:36 +02:00
So what is happening seems to be that when we press caps lock, it looks for what keycode to send when no modifier keys are pressed.
Finding Control in the first column (the only column we specified), it activates the control modifier.
When we release caps lock, it looks for the key to [un]press when C-caps lock is released, and finds nothing.
This means that control is now stuck on.
But wait, if we read a bit further in the man page, we find that this shouldnt be happening!
2023-06-22 09:43:19 +02:00
{{<highlight console "linenos=false">}}
2021-05-23 19:54:36 +02:00
For added convenience, you can usually get off with still more terse definitions.
If you enter a key definition line with only and exactly one action code after the equals sign, it has a special meaning.
If the code (numeric or symbolic) is not an ASCII letter, it means the code is implicitly replicated through all columns being defined.
2023-06-22 09:43:19 +02:00
{{</highlight>}}
2021-05-23 19:54:36 +02:00
Shouldnt this mean that our 'keycode 58 = Control' should be interpreted as 'keycode 58 = Control Control Control (and so on)'?
Well, it should!
However, there seems to be a bug in 'loadkeys', as the above only works when defining a complete keymap, not when overriding parts of default.map.
This means, that in order to correctly modify the keymap, we either have to define all columns manually, or we have to copy the default keymap, edit it, and load it as a complete keymap.
== Keymap patch
To continue overriding the default keymap, you can simply manually repeat the control command.
Now, technically, there are 256 columns in the keymap file, but, at least for latin keyboards, only the first 16 are used.
As sutch, our keymap patch looks like:
2023-06-22 09:43:19 +02:00
{{<highlight console "linenos=false">}}
2021-05-23 19:54:36 +02:00
keycode 58 = Control Control Control Control Control Control Control Control Control Control Control Control Control Control Control Control
2023-06-22 09:43:19 +02:00
{{</highlight>}}
2021-05-23 19:54:36 +02:00
Now just put it in in '/usr/share/kbd/keymaps/', and set it as your keymap with 'sudo localectl set-keymap [filename without .map extention]'.
== Full keymap
In order to create a new full keymap, copy the keymap you want to edit from '/usr/share/kbd/keymaps/i386/[couple more folders here]' to somewhere in 'usr/share/kbd/keymaps/' and unzip it with 'sudo gzip -d [filename]'.
Edit it with sudoedit and replace
2023-06-22 09:43:19 +02:00
{{<highlight console "linenos=false">}}
2021-05-23 19:54:36 +02:00
keycode 58 = Caps_Lock
2023-06-22 09:43:19 +02:00
{{</highlight>}}
2021-05-23 19:54:36 +02:00
with
2023-06-22 09:43:19 +02:00
{{<highlight console "linenos=false">}}
2021-05-23 19:54:36 +02:00
keycode 58 = Control
2023-06-22 09:43:19 +02:00
{{</highlight>}}
2021-05-23 19:54:36 +02:00
Then you can (optionally) re-zip it with 'sudo gzip [filename]' and set it as your keymap with 'sudo localectl set-keymap [filename without .map extention]'.
There we go!
Our caps lock key is now a second control key!
Note that localectl does not seem to propogate our change to X11, unfortuanately.
To do it in X as well, simply run
2023-06-22 09:43:19 +02:00
{{<highlight console "linenos=false">}}
setxkbmap -option ctrl:nocaps
2023-06-22 09:43:19 +02:00
{{</highlight>}}
in your .xinitrc.