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 Answers 4

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.

10
  • 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, 2013 at 8:51
  • I have the same problem with @Nur
    – eccstartup
    Jul 28, 2013 at 8:57
  • 1
    I'm testing that, will let you know shortly.
    – Mitch
    Jul 28, 2013 at 9:14
  • 1
    No. Just try it, it doesn't do anything. :)
    – Mitch
    Jul 28, 2013 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, 2013 at 10:31
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
2
  • How would one create .keyboard_status in the first place? Dec 18, 2020 at 13:35
  • Oh, I'm a moron. It's right there in the code. echo "enabled" > ~/.keyboard-status Dec 18, 2020 at 13:39
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

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 "$DEVICES_STRING" | cut -d" " -f 7)
        DEVICES_STRING="${DEVICES_STRING//id=/}"

        DEVICE_ID="$DEVICES_STRING"
    else
        DEVICE_ID=$1
    fi

    FILE_PATH=~/.keyboard-status-"$DEVICE_ID"

    FILE_STATUS=$(<"$FILE_PATH")

    if [ -z "$FILE_STATUS" ] || [ "$FILE_STATUS" = "enabled" ]; then
        xinput --disable "$DEVICE_ID"
        echo "disabled" >"$FILE_PATH"
        echo "keyboard $DEVICE_ID disabled"
    else
        xinput --enable "$DEVICE_ID"
        echo "enabled" >"$FILE_PATH"
        echo "keyboard $DEVICE_ID enabled"
    fi
}

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

alias handle_keyboard="handleKeyboard"

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .