How to disable and enable keyboard in ubuntu?

I have a great trouble because I don't have enough space to put both my keyboard and some other things on my desk. And accidentally pressing some key may cause trouble to the system. So I am looking for one way to lock my keyboard temporarily. Of course I don't want to plug out the keyboard from the computer because it is so inconvenient.

How can I do with this?

xinput -list

⎡ Virtual core pointer                          id=2    [master pointer  (3)]
⎜   ↳ Virtual core XTEST pointer                id=4    [slave  pointer  (2)]
⎜   ↳ Logitech USB Optical Mouse                id=9    [slave  pointer  (2)]
⎣ Virtual core keyboard                         id=3    [master keyboard (2)]
    ↳ Virtual core XTEST keyboard               id=5    [slave  keyboard (3)]
    ↳ Power Button                              id=6    [slave  keyboard (3)]
    ↳ Power Button                              id=7    [slave  keyboard (3)]
    ↳ CHICONY HP Basic USB Keyboard             id=8    [slave  keyboard (3)]
    ↳ HP WMI hotkeys                            id=10   [slave  keyboard (3)]

To Disable/Enable the keyboard, just press Ctrl+Alt+T on your keyboard to open Terminal. When it opens, run the command(s) below:

xinput -list

enter image description here

Once you find your ID, then

sleep 0.1 ; xinput set-prop 9 'Device Enabled' 0 ; sleep 5 ; xinput set-prop 9 'Device Enabled' 1

sleep 5 is the number of seconds (I guess) you want the keyboard to be disabled.

For more information on the xinput command see the ManPage.

Or you can use Lock keyboard utility.

  • 4
    How to enable keyboard again with mouse only? Is there any keyboard free solution to disable and enable keyboard back by indicator applet or just double clicking a script? – Nur Jul 28 '13 at 8:51
  • I have the same problem with @Nur – eccstartup Jul 28 '13 at 8:57
  • 1
    I'm testing that, will let you know shortly. – Mitch Jul 28 '13 at 9:14
  • 1
    No. Just try it, it doesn't do anything. :) – Mitch Jul 28 '13 at 10:28
  • 1
    I had no issues with the mouse. It worked fine while the keyboard was disabled. Plus I strongly believe that nothing is a 100%. – Mitch Jul 28 '13 at 10:31

Run xinput -list and find the id for AT Translated Set 2 keyboard as mentioned in the accepted answer. You may need to install xinput first.

I created the following script, and I run this via a Launcher I can click on with my mouse. Each time you click it, it toggles on or off the keyboard. Replace 13 with the ID number of your keyboard.


if [[ $(<~/.keyboard-status) == "enabled" ]]; then
    xinput --disable 13
    echo "disabled" > ~/.keyboard-status
    xinput --enable 13
    echo "enabled" > ~/.keyboard-status
  • How would one create .keyboard_status in the first place? – Mike Lawrence Dec 18 '20 at 13:35
  • Oh, I'm a moron. It's right there in the code. echo "enabled" > ~/.keyboard-status – Mike Lawrence Dec 18 '20 at 13:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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