Script to turn off laptop touchpad

About writing shell scripts and making the most of your shell
Forum rules
Topics in this forum are automatically closed 6 months after creation.
Locked
User avatar
Pepi
Level 6
Level 6
Posts: 1305
Joined: Wed Nov 18, 2009 7:47 pm

Script to turn off laptop touchpad

Post by Pepi »

Hi all! Not good at doing any writing code an was hoping someone could help me with this. If I want to turn off my laptop touchpad I do this at the terminal.

xinput --list | grep GlidePoint (To get the ID number of my touchpad)

⎜ ↳ AlpsPS/2 ALPS GlidePoint id=13 [slave pointer (2)]


export DISPLAY=:0
xinput set-int-prop 14 "Device Enabled" 8 0

(I have to edit the ID number because it will change on a shutdown/startup)

How can a script be done to find the correct ID of my touchpad and run this from a command file?

Thanks
Last edited by LockBot on Wed Dec 28, 2022 7:16 am, edited 1 time in total.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
User avatar
xenopeek
Level 25
Level 25
Posts: 29597
Joined: Wed Jul 06, 2011 3:58 am

Re: Script to turn off laptop touchpad

Post by xenopeek »

Create a file as follows (you can just use gedit / Text Editor from the menu):

Code: Select all

#!/bin/bash

# Check if GlidePoint can be found
MATCHES=$(xinput --list | grep GlidePoint | wc -l)

# Check exactly one match was found
if [[ $MATCHES != 1 ]]; then
	zenity --error --text="xinput lists no GlidePoint" --title=${0}
	exit 1
fi

# Get the ID of the GlidePoint
ID=$(xinput --list | grep GlidePoint | sed 's/^.*id=//g' | sed 's/ .*$//g')

# Check the match is not zero length
if [[ ${#ID[0]} = 0 ]]; then
	zenity --error --text="couldn't find id of GlidePoint" --title=${0}
	exit 1
fi

# Disable the GlidePoint
export DISPLAY=:0
xinput set-int-prop $ID "Device Enabled" 8 0

# Check if xinput had an error
if [[ $? != 0 ]]; then
	zenity --error --text="xinput failed disabling GlidePoint" --title=${0}
	exit 1
fi
For example, call it "Disable-GlidePoint" or something. Then right-click it, properties, permissions, and set to allow it for executing.
Image
User avatar
Pepi
Level 6
Level 6
Posts: 1305
Joined: Wed Nov 18, 2009 7:47 pm

Re: Script to turn off laptop touchpad

Post by Pepi »

Thanks Vincent! I can't get the code to run. It bombs out at ...

bill@bill-TOSHIBA-NB205 ~ $ xinput set-int-prop $ID "Device Enabled" 8 0
Invalid format 0


All the rest of the code seems to run without any errors,
User avatar
xenopeek
Level 25
Level 25
Posts: 29597
Joined: Wed Jul 06, 2011 3:58 am

Re: Script to turn off laptop touchpad

Post by xenopeek »

Well, I had to take a few guesses from the output you shared :wink: Somehow the ID is not getting grepped correctly.

Please do the following:

Code: Select all

xinput --list | grep GlidePoint >> ~/Desktop/output
xinput --list | grep GlidePoint | sed 's/^.*id=//g' | sed 's/ .*$//g' >> ~/Desktop/output
And then open the file "output" on your desktop, and share the contents here in a Code block (use the Code button located at the top right of the text field when you reply, and paste the contents of the output file between the code tags).
Image
User avatar
Pepi
Level 6
Level 6
Posts: 1305
Joined: Wed Nov 18, 2009 7:47 pm

Re: Script to turn off laptop touchpad

Post by Pepi »

Code: Select all

⎜   ↳ AlpsPS/2 ALPS GlidePoint                	id=13	[slave  pointer  (2)]
13	[slave
User avatar
xenopeek
Level 25
Level 25
Posts: 29597
Joined: Wed Jul 06, 2011 3:58 am

Re: Script to turn off laptop touchpad

Post by xenopeek »

Okay, then this should fix it. Replace the line:

Code: Select all

ID=$(xinput --list | grep GlidePoint | sed 's/^.*id=//g' | sed 's/ .*$//g')
With:

Code: Select all

ID=$(xinput --list | grep GlidePoint | sed 's/^.*id=//g' | cut -f1)
Only the last bit is changed. Without the Code block around it, in your first post the tab character in that line you shared had collapsed to a single space. Should work with this change.

So the entire script becomes:

Code: Select all

#!/bin/bash

# Check if GlidePoint can be found
MATCHES=$(xinput --list | grep GlidePoint | wc -l)

# Check exactly one match was found
if [[ $MATCHES != 1 ]]; then
	zenity --error --text="xinput lists no GlidePoint" --title=${0}
	exit 1
fi

# Get the ID of the GlidePoint
ID=$(xinput --list | grep GlidePoint | sed 's/^.*id=//g' | cut -f1)

# Check the match is not zero length
if [[ ${#ID[0]} = 0 ]]; then
	zenity --error --text="couldn't find id of GlidePoint" --title=${0}
	exit 1
fi

# Disable the GlidePoint
export DISPLAY=:0
xinput set-int-prop $ID "Device Enabled" 8 0

# Check if xinput had an error
if [[ $? != 0 ]]; then
	zenity --error --text="xinput failed disabling GlidePoint" --title=${0}
	exit 1
fi
Image
User avatar
Pepi
Level 6
Level 6
Posts: 1305
Joined: Wed Nov 18, 2009 7:47 pm

Re: Script to turn off laptop touchpad

Post by Pepi »

Thank you Vincent! Works like a charm. I hope this may help other out also. I sure wish I knew how to script like this 8) I'm not the sharpest tool in the shed :wink:
User avatar
xenopeek
Level 25
Level 25
Posts: 29597
Joined: Wed Jul 06, 2011 3:58 am

Re: Script to turn off laptop touchpad

Post by xenopeek »

Pepi wrote:Thank you Vincent! Works like a charm. I hope this may help other out also. I sure wish I knew how to script like this 8) I'm not the sharpest tool in the shed :wink:
Ah well, I need to automate something from time to time :wink: I keep old scripts around, for later re-use. There are a few good books on shell scripting, but I picked up everything from examples from others & Google 8)
Image
Locked

Return to “Scripts & Bash”