2021-02-19

Remap Capslock to Control in VirtualBox Linux guest on Windows host

Let's say you have Debian Linux guest OS in VirtualBox, running on a Windows 10 host.

Here's how to map capslock to control

If it was a Mac host, it'd be super easy: install and use Karabiner-Elements to remap capslock to control in the Mac host [3]. Now both host and guest Linux will have the remapping.  Done.

On Windows, it's not so easy.

You basically have to do the remapping in both Windows and Linux.  Follow the ideas here:

  1. Remap one key to another in Windows, like Capslock to Control 
  2. Remap one key to another in Linux, like Capslock to Control

Set shift key to break capslock for greater reliability

For greater reliability, you should also set shift key to break capslock.  So adding that to the Linux command for making capslock a control key, you'd get this command to run instead:

setxkbmap -option -option caps:ctrl_modifier -option shift:breaks_caps

The first blank "-option" clears any previously set.  Then the second option sets the capslock to control.  The third option makes shift key break capslock.

This is needed because sometimes switching between Windows host and Linux guest, the capslock gets locked down.  Now a quick tap on the shift key should break that lock.

What's wrong with remapping in Windows host only?

If you remap capslock to control in Windows host only, it'll actually sort of work in the Linux guest --- except in Linux, the capslock will now function as both capslock and control!

I did that for a while and didn't notice as I tested it inside Linux guest.  Capslock-T in Firefox still created new tabs!  Works!  But if you start typing, you'll notice things are in all caps until you tap capslock again, and vice versa.

What's wrong with remapping in Linux guest only?

If you remap capslock to control in Linux guest only, it'll actually work!  Except the capslock LED light will turn on/off with each click.  And if you switch back to Windows, capslock might have been engaged as you used capslock-as-control in Linux guest.

That's why you must do the remapping in both Windows host and Linux guest.  That way they stay in sync with each other, and the LED light won't turn on.

Capslock LED light in Linux

As I was troubleshooting the Linux guest only remapping, and noticed the capslock LED light would turn on/off, at first I thought the problem was in Linux.

And in case it is for your situation, here's how to fix or check.

xmodmap -e 'clear Lock' is suppose to clear the capslock LED from being turned on [1].  Try that, despite xmodmap being a deprecated system.

You can check if the LED is being turned on by Linux by running this command:

cat '/sys/class/leds/input5::capslock/brightness'

Turns out the LED property is represented as "just a file" on Linux you can open and read [2].

In my case, that command showed the LED was always off from Linux guest's point of view.  Turned out Windows host was controlling it.


Reference

[1] How to prevent the caps-lock toggle effect, without remapping or disabling it?

[2] Turn the Caps Lock LED on, while keep Caps Lock status is off

[3] Actually, I think Mac's System Preferences even have a built in setting for doing this remapping, so Karabiner might not even be needed for this single simple remapping.

No comments: