How to Disable and Enable Laptop Keyboard for X.org Server in Linux

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.

These scripts use the xinput tool for controlling the keyboard. You will need to install it on your Linux if your Linux hasn’t it installed by default.

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 ./

The 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.

The 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 grep + cut:

$ 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.

Eric Ma

Eric is a systems guy. Eric is interested in building high-performance and scalable distributed systems and related technologies. The views or opinions expressed here are solely Eric's own and do not necessarily represent those of any third parties.

15 comments:

  1. Thats nice,but not working for me, i use kali linux 2018.2 and i get this error

    /root/laptopkb/laptopkb-get-id.sh: line 5: xinput: command not found
    ./laptopkb-disable.sh: line 7: xinput: command not found

    1. The `xinput` may not work for Wayland which ubuntu 19.04 may use. This may be the cause.

      You may try `evtest` to grab the events for the device, which “disables” the device for other processes.

  2. Thank you! Because of soda spill, my laptop keyboard became troublesome. This script saved my work :-) Thank you for sharing!

  3. Hi, I’m on Linux Mint 20.1
    I don’t see see “AT Translated Set 2 keyboard id=……”
    Trying since 2w to find a solution, till now unsuccessful.
    Can you help?
    Thx in advance!!
    This is my output:

    xinput –list
    ⎡ Virtual core pointer id=2 [master pointer (3)]
    ⎜ ↳ Virtual core XTEST pointer id=4 [slave pointer (2)]
    ⎜ ↳ FTCS1000:00 2808:0101 Touchpad id=10 [slave pointer (2)]
    ⎜ ↳ FTCS1000:00 2808:0101 Mouse id=11 [slave pointer (2)]
    ⎣ Virtual core keyboard id=3 [master keyboard (2)]
    ↳ Virtual core XTEST keyboard id=5 [slave keyboard (3)]
    ↳ Video Bus id=6 [slave keyboard (3)]
    ↳ Power Button id=7 [slave keyboard (3)]
    ↳ Sleep Button id=8 [slave keyboard (3)]
    ↳ USB2.0 HD UVC WebCam: USB2.0 HD

Leave a Reply

Your email address will not be published. Required fields are marked *