3

I routinely disable Caps Lock and the respective modifier with a script, using xmodmap. That all works fine.

Sometimes, however, for some reason unknown, Caps Lock is active. Having no key bound to Caps_Lock and no key bound to toggle the Lock modifier, I cannot switch Caps Lock off unless I reset the keymap, press the key, then re-map it to my desired configuration.

So: How can I disable Caps Lock (currently active) without re-mapping keys and with no keys bound to do the job?

Perhaps a command line tool can set the state?

For anyone interested, here is how my script disables the accidental activation of Caps Lock by a key press (I never enable it intentionally):

#!/bin/sh
# I never want to use Caps_Lock.  Make Caps_Lock another Control_L...
xmodmap -e "remove Lock = Caps_Lock" 2> /dev/null
xmodmap -e "keysym Caps_Lock = Control_L" 2> /dev/null
xmodmap -e "add Control = Control_L" 2> /dev/null
Ned64
  • 8,486
  • 9
  • 48
  • 86

2 Answers2

5

I don't know of any utility which does that (except maybe xdotool key Caps_Lock?), but in the meanwhile you can compile this little program with cc xkb_unlock.c -s -lX11 -o ./xkb_unlock (provided that you have installed a compiler and the libc & xorg development packages) and use it as simply ./xkb_unlock.

xkb_unlock.c

#include <X11/Xlib.h>
#include <X11/XKBlib.h>
#include <err.h>
#include <stdlib.h>

int main(void){
        Display *dpy;
        if(!(dpy = XOpenDisplay(0)))
                errx(1, "cannot open display '%s'", XDisplayName(0));
        XkbLockModifiers(dpy, XkbUseCoreKbd, 0xff, 0);
        XSync(dpy, False);
}
  • Excellent, works like a charm, thanks! BTW what is the `-s` option? Didn't find documentation with my GNU cc, worked without it. – Ned64 Feb 28 '20 at 19:06
  • `-s` strips the debug symbols, makes the (on-disk) binary smaller –  Feb 28 '20 at 19:10
  • Thanks. I normally use the `strip` command for that. Good to know, though. At less than 21k size it's not a big concern in this case, though. BTW feel free to upvote my question if it was clear :-) – Ned64 Feb 28 '20 at 19:15
  • 1
    I cannot believe how much manpower has been wasted over the years because of this historic glitch of having this key in the firstplace (typewriters). We should vote it out of our computers. The few people still wanting to use that are for sure a tiny group against the people annoyed or unbothered. – U.V. Jan 16 '21 at 01:57
  • Sadly it doesn't work for me - simply doesn't do anything. Also "xmodmap -e 'clear Lock'" which worked for years stopped working. So probably something changed in X11 recently, but I still have no idea what :-( – Nadav Har'El Jul 09 '21 at 20:18
  • 1
    @Nadav That program doesn't disable the CapsLock *key*, but turns the CapsLock *state* off when there's no CapsLock key you can use for that. As requested by the question. (In fact, it will unlock all modifiers, not just `LockMask`, but I don't know of any AltsLock key ;-)). Neither `setxkbmap` nor `xmodmap` can do that; `xdotool key Caps_Lock` can do that, but it will toggle the state, not turn it off. –  Jul 11 '21 at 05:37
  • Nice, sorry, I guess I misunderstood the question. – Nadav Har'El Jul 11 '21 at 06:20
-1

For many years, xmodmap -e 'clear Lock' worked well for disabling the effect of caps lock. The xkb_unlock.c code posted in another answer does a similar things. But sadly, on my modern Linux distribution this no longer works. You need to use setxkbmap, instead of xmodmap:

setxkbmap -option caps:none

works like a charm.

Nadav Har'El
  • 148
  • 5
  • 2
    Your suggestion removes the possibility to toggle `Caps Lock`. The question was about switching off `Caps Lock` while it is active and there is no key mapped to toggle/switch it. So basically, switch `Caps Lock` on, run your command, now how do you get rid of it (switch it off)? – Ned64 Jul 15 '21 at 14:51