16

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)]

4 Answers4

20

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.

Mitch
  • 107,631
  • 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 08:51
  • I have the same problem with @Nur – eccstartup Jul 28 '13 at 08:57
  • 1
    I'm testing that, will let you know shortly. – Mitch Jul 28 '13 at 09:14
  • Will xinput --enable 9 xinput --disable 9 work the same as your command? – eccstartup Jul 28 '13 at 10:26
  • 1
    No. Just try it, it doesn't do anything. :) – Mitch Jul 28 '13 at 10:28
  • I know sleep 5 means. It is one solution, but not very good as expected. I think your link for the perl script works, but it also locked some function of the mouse. – eccstartup Jul 28 '13 at 10:29
  • 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
  • It is true that nothing is perfect! I appreciate your help. I think I will accept the perl script. – eccstartup Jul 28 '13 at 10:38
  • This nice thing about the sleep 5 here is that, if you got the id wrong and disabled your working keyboard, it'll undo itself in a few seconds. My built-in laptop keyboard keeps spamming me with 'q', which causes any open app (usually Firefox) to quit if I press the ctrl key. So with this short script, I can disable the keyboard, then hit ctrl-c (on my working wireless keyboard) to stop it being re-enabled, but if I somehow used the wireless keyboard's id, the ctrl-c won't register, so the script will continue and re-enable the keyboard. – Desty Oct 28 '15 at 00:00
  • good script here: https://sourceforge.net/projects/lk4b/ – aorlando Jan 25 '20 at 12:00
2

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.

#!/bin/bash

if [[ $(<~/.keyboard-status) == "enabled" ]]; then
    xinput --disable 13
    echo "disabled" > ~/.keyboard-status
else
    xinput --enable 13
    echo "enabled" > ~/.keyboard-status
fi
jbrock
  • 3,307
1

To automate both enabling and disabling Internal keyboard of laptop from cmdline, a shell script was written for my personal use at https://github.com/anitaggu/ikbdop.

A brief youtube tutorial is also available here at https://youtu.be/LvoIwqFutlg

Ani
  • 11
0

I think this could be easier.

Add this function at end of .bashrc file (or .zshrc or .profile etc.).

handleKeyboard() {
    if [ -z "$1" ]; then
        DEVICES_STRING=$(xinput --list | grep 'AT Translated Set 2 keyboard' | tr "  " " " | tr "   " " " | tr "    " " ")
        while [[ $DEVICES_STRING = *"  "* ]]; do
            DEVICES_STRING="${DEVICES_STRING//  /}"
        done
    DEVICES_STRING=$(echo &quot;$DEVICES_STRING&quot; | cut -d&quot; &quot; -f 7)
    DEVICES_STRING=&quot;${DEVICES_STRING//id=/}&quot;

    DEVICE_ID=&quot;$DEVICES_STRING&quot;
else
    DEVICE_ID=$1
fi

FILE_PATH=~/.keyboard-status-&quot;$DEVICE_ID&quot;

FILE_STATUS=$(&lt;&quot;$FILE_PATH&quot;)

if [ -z &quot;$FILE_STATUS&quot; ] || [ &quot;$FILE_STATUS&quot; = &quot;enabled&quot; ]; then
    xinput --disable &quot;$DEVICE_ID&quot;
    echo &quot;disabled&quot; &gt;&quot;$FILE_PATH&quot;
    echo &quot;keyboard $DEVICE_ID disabled&quot;
else
    xinput --enable &quot;$DEVICE_ID&quot;
    echo &quot;enabled&quot; &gt;&quot;$FILE_PATH&quot;
    echo &quot;keyboard $DEVICE_ID enabled&quot;
fi

}

And after this function you need only create an Alias like:

alias handle_keyboard="handleKeyboard"