Skip to content

Disabling Your Keyboard with Bash

Published: at 02:16 PM

Have you ever wanted a quick and easy way to enable or disable your keyboard on a Linux system?

Maybe you want to temporarily disable your laptop’s built-in keyboard while using an external one, or perhaps you need to enable it again after a specific task. Whatever the reason, we’ve got you covered with a simple and efficient Bash script that does the job effortlessly.

Keyboard Control Script

Below is a Bash script that leverages the power of xinput to enable or disable your keyboard based on the command-line argument you provide. Before we delve into the script, make sure you have xinput installed on your system. If you don’t have it, you can typically install it using your package manager. For instance, on Debian-based systems, you can use:

sudo apt-get install xinput

The Script Explained

Let’s go through the script step by step:

#!/bin/bash
 
# This script allows you to enable or disable your keyboard using xinput.
# Usage: ./keyboard.sh [enable|disable]

The script starts with the necessary shebang line (#!/bin/bash) and a short description of its purpose and usage.

# Check if the user provided the correct number of arguments
if [ $# -ne 1 ]; then
    echo "Usage: ./keyboard.sh [enable|disable]"
    exit 1
fi

The script checks if the user provided the correct number of arguments. It expects exactly one argument, which should be either ‘enable’ or ‘disable’. If the number of arguments is incorrect, it displays the usage instructions and exits with an error code.

# Get the command-line argument (should be either 'enable' or 'disable')
action=$1

The script stores the command-line argument provided by the user in the variable action.

# Get the ID of your keyboard using xinput list
# Use the name of the keyboard to filter devices and extract the ID
keyboard_name="<YOUR KEYBOARD NAME>"
keyboard_id=$(xinput list | grep "$keyboard_name" | grep -o "id=[0-9]*" | awk -F "=" '{print $2}')

Here, the script sets the keyboard_name variable to the name of the keyboard device you want to enable or disable. In this case, it is set to “AT Translated Set 2 keyboard”. The script then uses xinput list to retrieve a list of all input devices, grep to filter out the line containing the keyboard name, and awk to extract and store the keyboard ID in the keyboard_id variable.

# Check if the keyboard ID is valid
if [ -z "$keyboard_id" ]; then
    echo "Keyboard not found."
    exit 1
fi

The script checks if the keyboard_id variable is empty, indicating that the keyboard was not found in the list of input devices. If the keyboard is not found, it displays an error message and exits with an error code.

# Check if the action is valid
if [ "$action" = "enable" ]; then
    # Enable the keyboard using xinput
    xinput --set-prop "$keyboard_id" "Device Enabled" 1
    echo "Keyboard enabled."
elif [ "$action" = "disable" ]; then
    # Disable the keyboard using xinput
    xinput --set-prop "$keyboard_id" "Device Enabled" 0
    echo "Keyboard disabled."
else
    echo "Invalid action. Please use 'enable' or 'disable'."
    exit 1
fi

Finally, the script checks the value of the action variable. If it is equal to “enable”, it enables the keyboard using xinput by setting the “Device Enabled” property to 1. If the value is “disable”, it disables the keyboard by setting the property to 0. If the action variable does not match “enable” or “disable”, the script displays an error message and exits with an error code.

Usage

To use the script, simply open a terminal, navigate to the directory where the script is located, and execute it with the desired action as an argument:

./keyboard.sh enable

or

./keyboard.sh disable

Code

#!/bin/bash
 
# This script allows you to enable or disable your keyboard using xinput.
# Usage: ./keyboard.sh [enable|disable]
 
# Check if the user provided the correct number of arguments
if [ $# -ne 1 ]; then
    echo "Usage: ./keyboard.sh [enable|disable]"
    exit 1
fi
 
# Get the command-line argument (should be either 'enable' or 'disable')
action=$1
 
# Get the ID of your keyboard using xinput list
# Use the name of the keyboard to filter devices and extract the ID
keyboard_name="YOUR KEYBOARD NAME"
keyboard_id=$(xinput list | grep "$keyboard_name" | grep -o "id=[0-9]*" | awk -F "=" '{print $2}')
 
# Check if the keyboard ID is valid
if [ -z "$keyboard_id" ]; then
    echo "Keyboard not found."
    exit 1
fi
 
# Check if the action is valid
if [ "$action" = "enable" ]; then
    # Enable the keyboard using xinput
    xinput --set-prop "$keyboard_id" "Device Enabled" 1
    echo "Keyboard enabled."
elif [ "$action" = "disable" ]; then
    # Disable the keyboard using xinput
    xinput --set-prop "$keyboard_id" "Device Enabled" 0
    echo "Keyboard disabled."
else
    echo "Invalid action. Please use 'enable' or 'disable'."
    exit 1
fi

Conclusion

With this nifty Bash script, managing your keyboard’s state on a Linux system becomes a breeze. Whether you need to enable or disable your keyboard, you now have a straightforward solution at your fingertips. Enjoy the convenience of this script and take full control of your keyboard with ease.