Skip to content

Permanent caps lock/control swap in XFCE (and maybe console)?

I have setxkbmap -option ctrl:swapcaps in my ~/.xinitrc, and it works, but when I plug in a USB mouse, the setting gets reset. In addition, in VirtualBox, the Ctrl key seems to trigger a Ctrl in the VM while pressing caps lock!

Surely there's a better way to do this. What would you recommend? Bonus points if my console's control and caps lock are swapped, too.

5 points8 comments
Open on Reddit
About this post
Posted
Archived
Score
5 · 100% up
Kind
Text
ID
69caa8

Author

25
posts
254
comments
14
media
View profile

8 comments

I use xmodmap. From man xmodmap:

One  of  the more irritating differences between keyboards
is the location of the Control and CapsLock keys. A common
use of xmodmap is to swap these two keys as follows:
!
! Swap Caps_Lock and Control_L
!
remove Lock = Caps_Lock
remove Control = Control_L
keysym Control_L = Caps_Lock
keysym Caps_Lock = Control_L
add Lock = Caps_Lock
add Control = Control_L
3 points

I don't know how to set this for the console.

About the desktop and that USB device getting plugged in, move your settings to an X config file. X will then apply it to all devices as they get connected and you'll have no problems with things like suspend/resume resetting settings because all USB devices disappear and reappear. I think you will still have the VM's window reacting to the original keys.

The "localectl" tool can write an X config for you. It saves the stuff in a file /etc/X11/xorg.conf.d/00-keyboard.conf. The command to set stuff looks like this:

localectl set-x11-keymap LAYOUT MODEL VARIANT OPTIONS

Here's two examples on how you would use it in practice:

localectl set-x11-keymap us "" "" caps:swapcaps
localectl set-x11-keymap us pc104 altgr-intl caps:swapcaps

The first one sets "us" layout for the keyboard and adds that "caps:swapcaps" keyboard option you asked for. The second one is with all slots for that "set-x11-keymap" parameter filled in, setting a 104-keys PC keyboard, US layout, "altgr-intl" variant of that US layout, and then your "caps:swapcaps" option.

You can check what you are currently using by just typing localectl. It will print layout, model, variant, options, but only if they are set right now.

I think you need to log-out, log-in for the settings to get applied.

1 points

Ooooh, interesting! Could I apply the swapcaps stuff to my laptop's internal keyboard only? I have a HHKB that got me into this habit, and the Ctrl key is already in the "caps lock spot."

1 points

I think this might be possible, but you will have to take that 00-keyboard.conf as an example and make a copy and then do something to it yourself. There's rules like this that you can use in X config files that sound like it should be possible to apply something for just one keyboard and not the other:

MatchProduct "matchproduct"

This entry can be used to check if the substring "matchproduct" occurs in the device's product name.

MatchVendor "matchvendor"

This entry can be used to check if the substring "matchvendor" occurs in the device's vendor name.

Those options are described somewhere in man xorg.conf.

The product names should be the same as what you see with xinput, and you can search for stuff in /var/log/Xorg.0.log.

1 points

I tried that idea you had, about applying different X configs to different keyboards, and it works. I could use completely different language layouts on two keyboards, and I could also change just those caps-lock key options.

I created a new file for my "special" keyboard in /etc/X11/xorg.conf.d/ and it was applied on top of what I had in 00-keyboard.conf:

Section "InputClass"
        Identifier "second keyboard"
        MatchIsKeyboard "on"
        MatchProduct "NOVATEK USB Keyboard"
        Option "XkbOptions" "caps:ctrl_modifier"
EndSection

That name for the MatchProduct line is the same as what's seen in the output of the command xinput. My other keyboard is connected to PS/2 instead of USB and shows up as "AT Translated Set 2 keyboard".

You can check on what the X server is doing by searching around in the file /var/log/Xorg.0.log. The server prints the config lines it applies to each device.

1 points

I don't know how to set this in xorg, but in tty is super-easy.

I'm assuming that you aren't inside xorg:

  1. check the keycode of the key you want to change. showkey will echo the keycode of the pressed key. Remember: to stop it just don't type anything for 10 seconds and it will exit.

  2. copy your currently map file somewhere. For example mine is /usr/share/kbd/keymaps/i386/dvorak/dvorak.map.gz

  3. un(g)zip that

  4. open that file with your editor of choice

  5. for me the caps lock is the 58 while left-ctrl 29. Just swap the number at the start of the line

  6. recompress the map file gzip -c < dvorak.map > dvorak.map.gz

  7. copy that to the original folder

  8. Automatize that with a PKGBUILD and you're done!

I've alredy done something similar with esc and caps lock with a customized variant of the dvorak, my work is here: https://github.com/omar-polo/Latin-Dvorak

edit: formatting

1 points

Keyboard shortcuts

Move and act

jork
Next / previous post or comment
Enteroro
Open the post
f
Open the media viewer
s
Save
h
Hide (the toast has Undo)
Tab
Right after an action: the toast's Undo
x
Collapse the comment
v
Next layout: cards, compact, grid

Go to

/
Search
gthenh
Home
gthena
All
gthens
Subreddits
gthenl
Library
?
This list
Esc
Close a menu or this list

In the viewer

←or→
Previous / next file
↑or↓
Previous / next post
korj
Previous / next post
Space
Play / pause
m
Mute
z
Zoom
s
Save
o
Open the post (on its own page: back to it)
Esc
Close

Keys do nothing while you type in a box.