Saturday, April 27, 2024
 Popular · Latest · Hot · Upcoming
11
rated 0 times [  11] [ 0]  / answers: 1 / hits: 3061  / 2 Years ago, sun, may 22, 2022, 9:56:52

I want to have my touchpad disabled automatically when an external mouse is connected and enabled when there is none. I have tried using touchpad-indicator but that fails in cases when the computer has been put to sleep with a mouse connected and awoken with the mouse disconnected.



I have tried to make the following script into a daemon to solve this issue but I can't get it to work:



#!/bin/bash

declare -i TID
declare -i MID
TID=`xinput list | grep -Eo 'Touchpads*id=[0-9]{1,2}' | grep -Eo '[0-9]{1,2}'`
MID=`xinput list | grep -Eo 'Mouses*id=[0-9]{1,2}' | grep -Eo '[0-9]{1,2}'`
if [ $MID -gt 0 ]
then
xinput disable $TID
else
xinput enable $TID
fi


I tried start-stop-daemon -S -x ./myscript.sh -b



and setsid ./myscript.sh >/dev/null 2>&1 < /dev/null &



and nohup ./myscript 0<&- &>/dev/null &
and even ./myscript.sh &



All of these return some 4-digit number, which, I guess, should be PID of the started process but when I launch lxtask there are no processes with this PID, even if I tick "view all processes". And, of course, it doesn't work!


More From » bash

 Answers
3

The basic script you need is simply:



#!/usr/bin/env bash

## Get the touchpad id. The -P means perl regular expressions (for K)
## the -i makes it case insensitive (better portability) and the -o
## means print only the matched portion. The K discards anything matched
## before it so this command will print the numeric id only.
TID=$(xinput list | grep -iPo 'touchpad.*id=Kd+')

## Run every second
while :
do
## Disable the touchpad if there is a mouse connected
## and enable it if there is none.
xinput list | grep -iq mouse && xinput disable "$TID" || xinput enable "$TID"
## wait one second to avoind spamming your CPU
sleep 1
done


The script above will toggle the trackpad depending on whether a mouse is connected. When launched, it will run for ever and will check for a mouse every second, disabling or enabling the touchpad accordingly.



Now, save the script as ~/touchpad.sh, make it executable (chmod +x ~/touchpad.sh) and add it to your GUI session startup programs. You have not specified which desktop environment you are using but since you mentioned lxtask, I will assume you are using LXDE. In any case, here are instructions for both LXDE and Unity:




  1. Add the script to LXDE's autostart files



    echo "@$HOME/touchpad.sh" >> ~/.config/lxsession/PROFILE/autostart file


    Make sure you replace "PROFILE" with the actual name of your LXDE profile, you can find out what it is by running ls ~/.config/lxsession/.


  2. Add the script to Unity's autostart files



    Open Startup Applications (search in the dashboard for "Startup")



    enter image description here



    Click on "Add" and then paste the path to your script in the command field:



    enter image description here



[#26381] Tuesday, May 24, 2022, 2 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
irripri

Total Points: 164
Total Questions: 111
Total Answers: 107

Location: South Georgia
Member since Sun, Aug 8, 2021
3 Years ago
;