Disabling Touchpad when using a Mouse

After using this script for a while I found a problem when occasionally after a reboot the device numbers had changed. So I have changed the script to use the device name in place of the ID. This has solved the problem. I’ve also added icons to the notification output.

There is a common feature on current Linux desktop that allows the touchpad to be disabled while typing. This is a useful feature but it has its limitations. The delay before the touchpad starts working can be set to a long time to reduce accidental movement but then the delay becomes irritating. I find I still occasionally brush the touchpad and cause an issue with what I am typing particularly when I pause to review what I have written.

As I normally use a bluetooth mouse I would prefer that the touchpad is completely disabled or preferably disabled when the mouse is in use. There are several scripts around which do this using synclient. I have found a few of these and tested them and none worked for me. I could see that the setting was changed but the touchpad was still active.

After a fair bit of research I found mention of using xinput and the commands needed to use xinput to disable the touchpad. Xinput has an advantage over synclient in that it completely disables the touchpad, buttons and all whereas synclient just stops any cursor movement.

I have tested this on my Thinkpad running Korora 23 Xfce but it should work on any recent version of most distros and with most desktop environments. It isn’t needed in KDE which includes this as an option in the touchpad settings. The script should also work with a USB mouse, in fact any sort of mouse, as well as the Bluetooth mouse it was tested with. The script uses xinput to check if a mouse is present. If your mouse is listed there and the name includes the word “mouse” it will work. Type “xinput –list” (without the quotes of course) with your mouse attached and check it is there.

There are a couple of steps needed to get xinput to work. The first is to identify the id of the touchpad. To do this run the command “xinput –list”. It will show a list of all the devices xinput controls. One of these is clearly marked as the touchpad. Looking along that line there is an ‘id=’ field. Make a note of this number, mine is 11 but yours may well be different. Looking along that line also make a note of the exact title of the touchpad, mine was “SynPS/2 Synaptics TouchPad” which seems to be common.

Next you need the id of the touchpad off setting for that device. Run “xinput –list-props 11” substituting the device number you found earlier for 11. This gives a long list of properties for that device. Look for “Device Enabled”, for me this was first on the list and so I had to scroll up to see it. That line will have a number in brackets, the property id, mine is 139 but again yours will probably be different.

With both those numbers recorded we are ready to test the xinput command. There is no point making a script if the command doesn’t work. Test with the command “xinput set-int-prop 11 139 8 0” (replacing my 11 with the device number you found and the 139 with the property number you found). If the command works your touchpad will be disabled. Run the command “xinput set-int-prop 11 139 8 1” (yes replace my numbers with yours again) to enable it again.

Once that is working we can create a script. My script is copied from several I found that used synclient but with the appropriate command replaced. Don’t forget to put the correct numbers in your script. We will also use the exact name of the touchpad in place of the number.

First as root or using sudo open /usr/local/bin/touchpad in your favourite text editor. It should be a new file. Copy and paste the script below and then edit the lines with the xinput command.

#!/bin/bash
sleep 1
list=`xinput --list | grep -i 'mouse'`
if [ ${#list} -eq 0 ]; then
xinput set-int-prop 'SynPS/2 Synaptics TouchPad' 139 8 1
notify-send -i touchpad-indicator "No Mouse found" "Your touchpad is set to ON"
else
xinput set-int-prop 'SynPS/2 Synaptics TouchPad' 139 8 0
notify-send -i preferences-desktop-peripherals "Mouse connected" "Your touchpad is now turned OFF"
fi

Save the file. Make it executable, “chmod +x /usr/local/bin/touchpad”. Note the sleep on the first line isn’t strictly needed but occasionally the script fails to run without it.

Now it should work. Test by running the script as a user. If your mouse is connected you should see a notification and the touchpad should be disabled. Next disconnect the mouse and check the touchpad again. I use a bluetooth mouse and it takes some time for the system to recognise that the mouse is off. I’m guessing it thinks there is a temporary break in communication. But with a usb mouse it should react much faster. I normally don’t need to run the script again.

However the exception is after a reboot or very occasionally upon returning from a suspend I find I need to run it. I use a dropdown terminal so it isn’t a problem to open it and run the script. However you may prefer to create a keyboard shortcut so that you can run the script quickly and easily as needed. In Xfce go to settings – Keyboard – Application Shortcuts and put the full path to the command as the application, press the key(s) and it is set. Test it.

One interesting result of this script is I can still move the cursor on my Thinkpad using the Trackpoint but none of the buttons work. I could disable the Trackpoint too but it isn’t easy to accidentally bump the Trackpoint, at least I’ve never done it so I don’t think it is necessary.

Advertisements

One thought on “Disabling Touchpad when using a Mouse

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: