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
  • 109,787
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,417
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"