I don't know of any command line tool for that in Ubuntu. (For Num Lock, there is numlockx .) Here's a one-liner that you can copy-paste into a terminal window:
python -c 'from ctypes import *; X11 = cdll.LoadLibrary("libX11.so.6"); display = X11.XOpenDisplay(None); X11.XkbLockModifiers(display, c_uint(0x0100), c_uint(2), c_uint(0)); X11.XCloseDisplay(display)'
Here it is again in a more expanded form. We use the Python ctypes library to call C functions from the X library directly. The function XkbLockModifiers
changes the state of the keyboard locks, on the core keyboard (XkbUseCoreKbd
= 0x0100), affecting Caps Lock (2), setting it to 0 (off).
#!/usr/bin/env python
from ctypes import *
X11 = cdll.LoadLibrary("libX11.so.6")
display = X11.XOpenDisplay(None)
X11.XkbLockModifiers(display, c_uint(0x0100), c_uint(2), c_uint(0))
X11.XCloseDisplay(display)
If you have a stuck modifier, change 2 to the mask of the modifiers you want to turn off. The modifiers are 1=Shift, 2=Lock (Caps Lock), 4=Control, 8=Mod1, 16=Mod2, 32=Mod3, 64=Mod4, 128=Mod5. Run xmodmap -pm
to see what Mod1 through Mod5 correspond to. For example, to turn off all modifiers, call X11.XkbLockModifiers(display, c_uint(0x0100), c_uint(255), c_uint(0))
. To turn on Num Lock which is on Mod2 and at the same time turn off Caps Lock, call X11.XkbLockModifiers(display, c_uint(0x0100), c_uint(2 | 16), c_uint(16))
.
Here's a C version if you want to make a small binary instead of invoking Python. Compile with gcc -O -Wall -o caps_lock_off caps_lock_off.c -lX11
, with the packages build-essentials
and libx11-dev
installed.
#include <stdio.h>
#include <X11/X.h>
#include <X11/XKBlib.h>
int main()
{
Display *display = XOpenDisplay(NULL);
if (display == NULL) {
fprintf(stderr, "Couldn't open display\n");
return 2;
}
Bool sent = XkbLockModifiers(display, XkbUseCoreKbd, LockMask, 0);
if (!sent) {
fprintf(stderr, "Couldn't send LatchLockState\n");
return 1;
}
#ifdef REPORT_STATE
XkbStateRec xkb_state;
Status status = XkbGetState(display, XkbUseCoreKbd, &xkb_state);
if (status) {
fprintf(stderr, "XkbGetState returned %d\n", status);
return 1;
}
printf("state.group=%02x\n", xkb_state.group);
printf("state.locked_group=%02x\n", xkb_state.locked_group);
printf("state.base_group=%02x\n", xkb_state.base_group);
printf("state.latched_group=%02x\n", xkb_state.latched_group);
printf("state.mods=%02x\n", xkb_state.mods);
printf("state.base_mods=%02x\n", xkb_state.base_mods);
printf("state.latched_mods=%02x\n", xkb_state.latched_mods);
printf("state.locked_mods=%02x\n", xkb_state.locked_mods);
printf("state.compat_state=%02x\n", xkb_state.compat_state);
printf("state.grab_mods=%02x\n", xkb_state.grab_mods);
printf("state.compat_grab_mods=%02x\n", xkb_state.compat_grab_mods);
printf("state.lookup_mods=%02x\n", xkb_state.lookup_mods);
printf("state.compat_lookup_mods=%02x\n", xkb_state.compat_lookup_mods);
printf("state.ptr_buttons=%02x\n", xkb_state.ptr_buttons);
#endif
int err = XCloseDisplay(display);
if (err) {
fprintf(stderr, "XCloseDisplay returned %d\n", err);
return 1;
}
return 0;
}
Also possibly of interest is a way to temporarily ignore Caps Lock:
xkbset nullify lock
After this, Caps Lock will effectively be permanently off, until you reenable it with xkbset nullify -lock
.
Learn to use Shift instead, that is the common way to enter a capital letter.
If you want to hit Caps Lock anyway to produce a capital letter, do note that there are reports that the delay is a known issue ((Launchpad Bug #1376903). The Arch Linux wiki describes a method to remove this delay which seems to work.
For your convenience, this script performs the manual steps documented at that wiki page:
#!/bin/sh
xkbcomp -xkb "$DISPLAY" - | sed 's#key <CAPS>.*#key <CAPS> {\
repeat=no,\
type[group1]="ALPHABETIC",\
symbols[group1]=[ Caps_Lock, Caps_Lock],\
actions[group1]=[ LockMods(modifiers=Lock),\
Private(type=3,data[0]=1,data[1]=3,data[2]=3)]\
};\
#' | xkbcomp -w 0 - "$DISPLAY"
This needs to be started as part of your desktop session (do not use Upstart for example). See this post for a method, do not forget to enable the execute bit of the script.
Best Answer
Having my self this problem, I decided to make a script that I can run in the background, and keep the Caps Lock status the same, between language changes. It JUST WORKS!ΑΠΛΑ ΔΟΥΛΕΥΕΙ! (The previous example was typed by keeping the caps lock on while changing between English and Greek). You can find it on github, https://github.com/mechpanos/capskeeper .
It uses Python, wit the pyxhook module to listen to the key presses even in the background (included), and it also takes advantage of the pyautogui library to automatically press CAPS LOCK button, if needed (needs to be installed separately, with the command pip install -U pyautogui).
Thanks to all the folks for the examples of using Python, pyxhook and pyautogui, that helped make this script! Feel free to test it and make it better, if you want to...
You can run it on the background, using no hangup to run a program in background even if you close your terminal:
Do not forget to use & to put it in background.