Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

For some reason my laptop is stuck in Caps Lock. I have the Caps Lock key mapped by xmodmap to hyper_l, so I don't have a caps_lock key to turn it off. Is there a way I can turn it off by command line? It would be nice if I could reset the lock state without resetting my computer, especially for future reference. This has happened before, but I would like to know now how to do it properly this time.

share|improve this question
6  
THANKS. IT'S DIFFICULT TO HOLD DOWN SHIFT. –  Echogene Nov 17 '11 at 18:01
    
[echo -e "$(dumpkeys | grep ^keymaps)\nkeycode 58 = Escape" | sudo loadkeys][1] [Adding the following line to .profile or .bash_profile should disable caps lock:][2] [xmodmap -e "remove lock = Caps_Lock"][3] [1]: askubuntu.com/questions/43986/… [2]: askubuntu.com/questions/43986/… [3]: askubuntu.com/questions/43986/… –  One Zero Nov 17 '11 at 18:02

1 Answer 1

I don't know of any command line tool for that in Ubuntu. (For Num Lock, there is numlockx Install 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)).

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.

share|improve this answer
    
I'll have a go when I'm next on my laptop. I'm on my desktop for now. –  Echogene Nov 17 '11 at 23:50
9  
The python script worked great for me. Thanks. –  djb Jan 4 '12 at 4:58
1  
Created an account just to +1 this. There was about to be LOTS OF UNINTENTIONAL TEXT-YELLING around here, but you saved me. –  Kyle Strand Jan 16 at 4:05

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.