Translated by ChatGPT.

    The touchscreen on my laptop has a crack from a drop. At first, it wasn’t a problem, but in recent weeks, it has worsened, and sometimes the cursor goes wild and doesn’t respond to commands. So, I needed to disable the touchscreen as an input device.

    In the Xorg desktop environment, you can use xinput list to display all input devices along with their corresponding ID numbers. Once you locate the device’s ID, you can disable it with xinput disable ##. Generally, the ID number is stable, so I directly added the disable command to ~/.bashrc.

    However, I recently took out my long-neglected Raspberry Pi and, for fun, bought a compact 60% mechanical keyboard. After plugging in the keyboard and logging in with my password, guess what? The new keyboard stopped working, giving me a real scare.

    So, now I need to check the input device IDs each time and pull out the IDs related to the touchscreen (there's more than one) from the xinput list output. I need to assign them to a variable and then input this variable into xinput disable #. This sequence of actions is beyond my Bash skills, so I considered using Python for the intermediate steps: that is, getting Python to read the Bash command results and write the Bash command statements.

    A Google search brought up this result: https://stackoverflow.com/questions/4256107/running-bash-commands-in-python. After a quick look at the official subprocess documentation, I wrote the following snippet, saved it as ~/disable_touchscreen.py, and added python ~/disable_touchscreen.py to my ~/.bashrc.

    import subprocess
    check = subprocess.run(["xinput","list"],capture_output=True)
    for line in check.stdout.decode("utf-8").split("\n"):
        if "touchscreen" in line:
            device = line.partition("id=")[2].partition("\t")[0]
            disable = subprocess.run(["xinput","disable",str(device)])
            if disable.returncode == 0:
                print(f"Successfully disabled touchscreen device {device}")
            else:
                print(f"Failed to disable touchscreen device {device}")
    • The core of this code is subprocess.run(). The first parameter is the command passed to Bash, which is a list where each element corresponds to each part of the Bash command split by spaces. To get the command’s output, you need to add the capture_output=True parameter.
    • The return value of the function is a special data structure. If the command executes successfully, the result is in .stdout, which is a binary string b'xxx...', so it needs to be converted to a string with .decode("utf-8").

    If only using Bash:

    • My Bash skills can get as far as xinput list | grep "touchscreen". Here, | is a pipe, which passes the output of the preceding command as input to the following command. To use a pipe in Python, you can refer to this question: https://stackoverflow.com/questions/13332268/how-to-use-subprocess-command-with-pipes.
    • The result of the pipe still needs to be trimmed to get the ID, i.e., line.partition("id=")[2].partition("\t")[0]. This would probably require awk, which is challenging but learnable, and at least I know what to learn.
    • But assigning the ID to a variable and then passing this variable into xinput disable—I don’t even know where to start with that.

    See also: