6

I would like to remap CapsLock to Esc when pressed by itself and Ctrl when pressed with another key without using X.

This is similar to a previous question ( Remap CapsLock to Escape and Control System Wide ) but I am specifically looking for a solution without X.

  • What operating system would you like to do this in? –  Jul 20 '13 at 20:52
  • @evan Linux. Specifically Arch. The motivation is to transport my setup from my Macbook to my new machine. – Casey Robinson Jul 20 '13 at 21:15
  • I'm pretty sure the only way to do this is by writing some kernel code. Why aren't you using X? – Gilles 'SO- stop being evil' Jul 20 '13 at 22:47
  • I am using a netbook to program and would rather not waste RAM on X to run a vim. – Casey Robinson Jul 21 '13 at 12:16
  • 1
    Wow, I almost asked this exact question. I'm using a Chromebook with a Debian chroot (CLI only) and hence no X11. – Alex Shroyer Jan 27 '14 at 15:39
  • Perhaps @LeoNerd knows the answer to your question (http://stackoverflow.com/users/1069726/leonerd). If X can do it, then there must be a way to do it without modifying kernel code. You might also like to take a look at kmscon, a new terminal to replace the old default linux terminal. It is much nicer and supports nice things like 24-bit colors. Who knows, maybe it has a way to map keys? https://wiki.archlinux.org/index.php/KMSCON – trusktr Jan 29 '14 at 05:13
  • @hoosierEE Would you need the exact same solution as the origal question? In Linux it's fairly easy to capture events using the /dev/input/eventX devices, and you can inject fake events into the kernel using /dev/uinput. I have some old code that could be used to put something together. You would need to run it using sudo, is that a problem? – brm Jan 29 '14 at 07:23
  • @brm I don't have a problem with sudo requirement, please do share. :) – Alex Shroyer Jan 29 '14 at 14:11
  • @hoosierEE feel free to beta-test https://bitbucket.org/brmtron/keymod – brm Jan 29 '14 at 18:55
  • Hey, look at this. Not the solution, but would help in understanding how it might be done: http://www.danielmiessler.com/blog/enhancements-to-shell-and-vim-productivity#capslock – trusktr Jan 30 '14 at 01:49
  • Actually, I'm using Chrome OS to get into the CLI-only Debian environment on my Chromebook. From Chrome OS I already swapped Ctrl and Search (which is the key to the left of a on my machine). So close! – Alex Shroyer Jan 31 '14 at 16:07

2 Answers2

5

Linux Console Keymaps

To change the keymaps for the virtual terminals ( alt+f1 - alt+f6 ) you use loadkeys. This only effects the virtual terminal logins and will not change keymaping in X or X terminal apps like Xterm or urxvt.

The loadkeys command needs to be run as root or you will get the following error:
Couldn't get a file descriptor referring to the console

To swap escape and caps_lock you would do the following:

# echo keycode 58 = Escape | loadkeys -
# echo keycode 1 = Caps_Lock | loadkeys -

Another option is to remap the Caps_Lock to shift + Scroll Lock

# echo keycode 69 = Num_Lock  Caps_Lock | loadkeys -

The format of the keymaps can be a little confusing. It using a weighting system depending on which modifiers that are pressed. ( Ctrl, Alt, Shift, etc ). As an example from the man page for keymaps.

keycode 30 = a    A    VoidSymbol    VoidSymbol   VoidSymbol  .....

When the shift and a is pressed we do the action in the second column because shift has a weighting of 1. control on the other hand has a weighting of four.

This can cause problems when mapping modifiers. Take for instance if a keycodes first and only column is control as its action. Now the weighting since you pressed it is now 4 and when you release the key it the control is not show released because the 5th column action is now to be performed but it has no action defined. So the control key is never released.

This is why most modifiers have no other action assigned. If only one action is defined in the keyboard mapping then it is applied to all actions.

I have played around trying to work around this but have only ever got inconsistent results. Sometimes the modifier releases, sometimes it does not.

Clarification

Changing the Caps Lock to perform as either escape or as control works great with this method. Mapping to both, not so much.

IMPORTANT

If you screw up your keymap do the following to reset it.

# loadkeys -d

References

rking
  • 571
3

This keymod program reads keyboard events from a /dev/input/eventX device and injects most directly back into the kernel using the /dev/uinput device. The caps lock behavior is special: if the key is pressed and released without touching another key, an Esc key is sent into the kernel. If another key is pressed while holding the caps lock, holding the (left) control key is emulated instead.

Since the program takes control over the specified event device, being able to access the computer using e.g. SSH can be very handy when testing this. For example, pausing this program (using ctrl-z for example) will make sure you can't use your keyboard anymore (it has taken control over it exclusively and is now no longer active).

brm
  • 1,021