Attaching a USB keyboard to a laptop is common when using a laptop because a normal keyboard may provide a more convenient typing experience. The laptop keyboard is not used in these situations. However, the laptop keyboard may still be touched by accident. In this post, we will discuss how to disable and enable the laptop keyboard in Linux. I will also provide scripts ready for you to directly use them for disabling/enabling the laptop keyboard.
Scripts for disabling/enabling laptop keyboard
For those in hurry to have try, you can download the scripts from the code repository.
The scripts are in a git repository, you may clone the whole repository and copy these scripts out only by
$ git clone https://github.com/zma/usefulscripts $ cp -rv usefulscripts/script/laptopkb ./
laptopkb-disable.sh will disable the keyboard and the
laptopkb-enable.sh will enable the keyboard.
Before disabling your keyboard on your laptop, make sure you have an external USB keyboard connected to your laptop. Otherwise, you will have no way to input commands to enable your keyboard back unless you restart Linux using the power button or run the commands through a SSH session to your laptop (if you enabled the SSH service).
The mechanisms behind the scripts
xinput is a utility to configure and test X input devices including the keyboard and we use it to control the laptop keyboard.
float slave command of
xinput removes slave from its current master device, and the
--reattach slave master command reattachs slave to master. So these two commands can be used to disable/enable a keyboard (and other devices).
The remaining problem is to find out the “slave” and “master”.
xinput list shows a list of devices as follows.
⎡ Virtual core pointer id=2 [master pointer (3)] ⎜ ↳ Virtual core XTEST pointer id=4 [slave pointer (2)] ⎜ ↳ Logitech Optical USB Mouse id=9 [slave pointer (2)] ⎜ ↳ SynPS/2 Synaptics TouchPad id=11 [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)] ↳ Video Bus id=7 [slave keyboard (3)] ↳ Power Button id=8 [slave keyboard (3)] ↳ HP WMI hotkeys id=12 [slave keyboard (3)] ↳ AT Translated Set 2 keyboard id=10 [slave keyboard (3)] ⎡ mouse pointer id=13 [master pointer (14)] ⎜ ↳ mouse XTEST pointer id=15 [slave pointer (13)] ⎣ mouse keyboard id=14 [master keyboard (13)] ↳ mouse XTEST keyboard id=16 [slave keyboard (14)]
Usually the “AT Translated Set 2 keyboard” is the laptop keyboard. “Virtual core keyboard” is the keyboard master (which is 3 from my observations and 3 is used directly in the script. Yes, it’s a dirty trick. Let me know if you find it is not 3 for you by leaving a comment). From the output of
xinput list, we can find out the ids. To find out the laptop keyboard id, we can use
$ xinput list | grep 'AT Translated Set' | cut -f2 | cut -d'=' -f2
So, adter combining them together, we have the scripts I put in the repository.