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:
The Script Explained
Let’s go through the script step by step:
The script starts with the necessary shebang line (#!/bin/bash
) and a short description of its purpose and usage.
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.
The script stores the command-line argument provided by the user in the variable action
.
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.
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.
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:
or
Code
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.