Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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
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]:… [2]:… [3]:… – One Zero Nov 17 '11 at 18:02

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(""); 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("")
display = X11.XOpenDisplay(None)
X11.XkbLockModifiers(display, c_uint(0x0100), c_uint(2), c_uint(0))

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
The python script worked great for me. Thanks. – djb Jan 4 '12 at 4:58
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 '14 at 4:05
THANKS YOU SAVED MY DAY – Jesse Hallett Feb 26 '15 at 22:06
saved my day! thanks a lot – Nek Mar 31 '15 at 12:31

X automation tools could be used for sending the required key events.

  1. Install xdotool

    sudo apt-get install xdotool
  2. Send a CAPS down/up event

    xdotool key Caps_Lock

Another tool is xte

  1. Install it

    sudo apt-get install xautomation
  2. Send a CAPS lock down/up event

    xte "key Caps_Lock"


share|improve this answer
What if this doesn't work? – quant_dev Jun 16 '15 at 16:24
This is the only one that worked for me. nullify and others didn't work. – lnostdal Jul 8 '15 at 10:15
@quant_dev, I've added another tool xte could you give it a try. – Sneetsher Jul 10 '15 at 20:39
CAN'T INSTALL, PASSWORD NOT ACCEPTED. :( – Gringo Suave Dec 19 '15 at 1:22
Yes, thanks, py script worked. ;) – Gringo Suave Dec 21 '15 at 0:46

Your Answer


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.