24

I managed to install Ubuntu 16.04 on my asus ux501 vw, but some of my shortcut are not working. I'm not looking to fix them all, but just one : the shortcut which allow me to enable or disable touchpad, so i can write long documents with working about the touchpad.

I tried to find it on google but I got nothing.

Can someone explain me how to fix that please? Thanks !

1

10 Answers 10

36

I created this bash script from negusp's answer. It finds and toggles TouchPad device. You can configure a custom shortcut to it in system settings.

#!/bin/bash

read TPdevice <<< $( xinput | sed -nre '/TouchPad|Touchpad/s/.*id=([0-9]*).*/\1/p' )
state=$( xinput list-props "$TPdevice" | grep "Device Enabled" | grep -o "[01]$" )

if [ "$state" -eq '1' ];then
    xinput --disable "$TPdevice" && notify-send -i emblem-nowrite "Touchpad" "Disabled"
else
    xinput --enable "$TPdevice" && notify-send -i input-touchpad "Touchpad" "Enabled"
fi

I'm setting Ctrl+Shift+F9 for toggle touchpad enable and disable like this:

enter image description here

Update: You may need to make your script to executable with command chmod +x filename or input /bin/bash /filepath to Command field of Custom shortcut window.

8
  • 2
    There's rarely a need to chain grep and sed. You can easily consolidate the two to sed -nre '/TouchPad/s/.*id=([0-9]*).*/\1/p'. Jan 22, 2017 at 11:55
  • 3
    A small correction in script for Ubuntu 17.10 after getting error line 6: [: : integer expression expected unable to find device Change TouchPad to Touchpad
    – Greg G
    Nov 11, 2017 at 9:35
  • 1
    Awesome answer! My only additions are to add zenity --info --text "Touchpad DISABLED" --timeout=2 and zenity --info --text "Touchpad ENABLED" --timeout=2 to your if and else blocks, respectively, to pop up a window which announces the new state then autocloses in 2 sec. Apr 2, 2018 at 18:44
  • 1
    See my slight modification here: askubuntu.com/a/1109515/327339 Jan 14, 2019 at 3:53
  • 1
    Update: some systems show Touchpad instead of TouchPad in the output from xinput, so I've updated my answer to search for both. Also, I've made it properly fix the slow scroll wheel problem too, using imwheel. See full ans here: askubuntu.com/questions/844151/enable-disable-touchpad/… Sep 12, 2019 at 17:28
17

You want shortcut, but you can easily put 2 scripts on the desktop and execute them.

First, go to terminal. Type xinput. Output Example:

⎡ Virtual core pointer                          id=2    [master pointer  (3)]
⎜   ↳ Virtual core XTEST pointer                id=4    [slave  pointer  (2)]
⎜   ↳ SynPS/2 Synaptics TouchPad                id=12   [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)]
    ↳ Sleep Button                              id=9    [slave  keyboard (3)]
    ↳ Laptop_Integrated_Webcam_1.3M             id=10   [slave  keyboard (3)]
    ↳ AT Translated Set 2 keyboard              id=11   [slave  keyboard (3)]
    ↳ Dell WMI hotkeys 

Find the Touchpad. In this example, the touchpad is listed as id=12

Create the first script with this:

#!/bin/bash
xinput enable 12

Save it and name it touchpadenable.sh, and in terminal, mark it as executable with:

chmod +x touchpadenable.sh

Do the exact same thing again, but rename the file as touchpaddisable.sh (or whatever), and instead of

xinput enable 12

Use the command

xinput disable 12

Save, mark as executable, and you should be able to run the scripts from the desktop. Note: you may have to right-click the scripts, click properties, and allow it to be executed.

3
  • Hey thanks for the answer ! I will use something like this ! Just one thing do you think it would be possible if i make it on 1 script file so that i can use the same shortcut?
    – Rei
    Nov 2, 2016 at 13:47
  • @Thomas See this answer: askubuntu.com/questions/597395/…
    – negusp
    Nov 2, 2016 at 13:57
  • You can toggle between the two commands with one script associated to one key: askubuntu.com/a/1318615/925128
    – cipricus
    Feb 23, 2021 at 10:58
10

You must to do like negusp's answer, first check your touch device, and will shows something like this:

~$ xinput
⎡ Virtual core pointer                      id=2    [master pointer  (3)]
⎜   ↳ Virtual core XTEST pointer                id=4    [slave  pointer  (2)]
⎜   ↳ PixArt Dell MS116 USB Optical Mouse       id=10   [slave  pointer  (2)]
⎜   ↳ SynPS/2 Synaptics TouchPad                id=14   [slave  pointer  (2)]
⎜   ↳ DLLC6B2:00 06CB:75BF Touchpad             id=12   [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)]
    ↳ Sleep Button                              id=9    [slave  keyboard (3)]
    ↳ Integrated_Webcam_HD                      id=11   [slave  keyboard (3)]
    ↳ AT Translated Set 2 keyboard              id=13   [slave  keyboard (3)]
    ↳ Dell WMI hotkeys                          id=15   [slave  keyboard (3)]
    ↳ DELL Wireless hotkeys                     id=16   [slave  keyboard (3)]

In my case is the device with id=12, now create a bash script with name "toggle-touch" in you Desktop folder under a folder named "bin", would be "~/Desktop/bin" and copy next code, check the device variable is pointed to my touch id=12, fix it with your case:

#!/bin/bash

device=12
state=`xinput list-props "$device" | grep "Device Enabled" | grep -o "[01]$"`

if [ $state == '1' ];then
  xinput --disable $device
else
  xinput --enable $device
fi

Set to this script execution permission:

chmod 775 /Desktop/bin/toggle-touch

Finally you can add this path to the ".bashrc" from your home folder, just add this line to the end of file:

PATH=$PATH:~/Desktop/bin

update the path with:

. .bashrc

Then you can execute this script from any place, I hope this help.

4

In my Ubuntu 18.04 Almas Dusal's answer didn't work until I changed the 'p' to lower case in TouchPad in the following line of his code:

read TPdevice <<< $( xinput | sed -nre '/Touchpad/s/.*id=([0-9]*).*/\1/p' )

After that touchpad was succesfully disabled and enabled by the script but that was not reflected in the Mouse & Touchpad settings screen. I created the following new script which toggles the touchpad while updating the settings view.

#!/bin/bash

state=$( gsettings get org.gnome.desktop.peripherals.touchpad send-events )

if [ "$state" = "'enabled'" ];then
    gsettings set org.gnome.desktop.peripherals.touchpad send-events 'disabled' \
        && notify-send -i touchpad-disabled-symbolic "Touchpad" "Disabled"
else
    gsettings set org.gnome.desktop.peripherals.touchpad send-events 'enabled' \
        && notify-send -i input-touchpad-symbolic "Touchpad" "Enabled"
fi
1
  • Yes, on 18.04 this is the answer! Thx for this work of art - finally I can re-enable the trackpad without tracking down a mouse ;) Aug 27, 2020 at 16:50
3

In Ubuntu 22.04, you have to use gsettings now, for both the X11 and Wayland window servers, like this:

# disable the touchpad
gsettings set org.gnome.desktop.peripherals.touchpad send-events disabled
# enable the touchpad
gsettings set org.gnome.desktop.peripherals.touchpad send-events enabled
# enable the touchpad automatically if an external mouse is NOT plugged in, but
# disable it automatically if one is
gsettings set org.gnome.desktop.peripherals.touchpad send-events disabled-on-external-mouse

# get the current setting
gsettings get org.gnome.desktop.peripherals.touchpad send-events

See my full answer and more details here: How to enable/disable the touchpad/trackpad in Ubuntu 22.04 from the command-line

1
  • 1
    Turning this into a script like askubuntu.com/a/1072472/54643 answer is very helpful. I've then bound that script to a hotkey in the Gnome Settings so I can toggle it on/off really easily. Sep 20 at 9:26
2

Update 2020: use this script here: eRCaGuy_dotfiles...touchpad_toggle.sh. It handles X11 and Wayland window servers, and versions of Ubuntu including 14.04, 16.04, 18.04, 20.04, and even 22.04.

See also my Ubuntu 22.04 answer here.

Original answer:

Tested and works on Ubuntu 14.04, 16.04, and 18.04.

Do like @Almas Dusal's answer, which does like @negusp's answer, except use my modified script instead since it has 3 additional benefits (UPDATE: benefits 1 and 3 below were added 12 Sept. 2019):

  1. It searches the output of xinput for both Touchpad AND TouchPad, since some systems have the P capitalized while others do not. This makes my script compatible with more systems (hopefully ALL systems).
  2. It adds the 2-second auto-closing popup windows via zenity in order to show whether your touchpad is now ENABLED or DISABLED. See screenshots of these popup windows below.
  3. It properly fixes your scroll wheel speed, enabling imwheel (which fixes your slow scroll wheel problem in Ubuntu/Linux when using a mouse scroll wheel) when you are using a mouse, while disabling imwheel when using a touchpad, since imwheel otherwise makes the touchpad scrolling really glitchy when enabled. Be sure to uncomment the respective lines to make this work. Ex: # imwheel -b "4 5" and # killall imwheel.
#!/bin/bash

# GS_toggle_touchpad.sh
# - toggle the touchpad on and off

# Gabriel Staples
# Written: 2 Apr. 2018 
# Updated: 12 Sept. 2019 

# References:
# - https://askubuntu.com/a/874865/327339
# - https://askubuntu.com/questions/844151/enable-disable-touchpad/1109515#1109515 <-- my own ans!

# Search for id number of "TouchPad" OR "Touchpad" in `xinput` list; manually type `xinput` to see all your devices
read TouchPadDeviceId <<< $( xinput | sed -nre '/TouchPad|Touchpad/s/.*id=([0-9]*).*/\1/p' ) 

state=$( xinput list-props "$TouchPadDeviceId" | grep "Device Enabled" | grep -o "[01]$" )

echo "TouchPadDeviceId = $TouchPadDeviceId"
echo "state = $state"

if [ "$state" -eq '1' ];then
    xinput --disable "$TouchPadDeviceId"
    zenity --info --text "Touchpad DISABLED" --timeout=2
    # Next line required only if using imwheel to fix slow scroll speed in Chrome, for ex; see here: 
    # https://askubuntu.com/a/991680/327339
    # imwheel -b "4 5"
else
    xinput --enable "$TouchPadDeviceId"
    zenity --info --text "Touchpad ENABLED" --timeout=2
    # Next line required only if using imwheel to fix slow scroll speed in Chrome, for ex; see here: 
    # https://askubuntu.com/a/991680/327339
    # killall imwheel
fi

Note that the imwheel thing is only required if you are following this answer to fix your scroll wheel speed too: Permanently fix Chrome scroll speed

Now, assign it a shortcut key of Ctrl + Alt + P (or whatever you choose [the 'P' stands for touch'P'ad in my case]). My addition to the script above is the zenity self-closing popup window part, which I describe more in my answer here (https://superuser.com/a/1310142/425838).

Now, when I use the shortcut above, I see this popup window, which auto-closes after 2 seconds:

enter image description here

And if I press it again I get this popup window, which auto-closes after 2 seconds:

enter image description here

Perfect!

As an added bonus, if you are a heavy mouse user, just add the command which calls your script above to your startup programs in Ubuntu so it runs and disables your track pad every time your computer starts up! I have this set so I can quit bumping the track pad with the base of my hand and deleting stuff accidentally all the time! Now, when my hand bumps the track pad, nothing happens because it's disabled. :)

Note: this all works great in Ubuntu 18.04 as well as older versions. I've tested it in Ubuntu 18.04 and Ubuntu 14.04. Here's a screenshot from the shortcut settings window in 18.04:

enter image description here

Related:

  1. UPDATE: Get the latest version of this script from my eRCaGuy_dotfiles project here!
  2. Permanently fix Chrome scroll speed
2
  • Does not reenable on my HP Probook. There are 2 items in xinput, SynPS/2 Synaptics TouchPad and SYNA306A:00 06CB:CD0B Touchpad. The first execution disables one, the second disables the other. From there it will toggle the second, but never the first.
    – rtaft
    Oct 9, 2019 at 14:22
  • @rtaft, to fix it, all you have to do is change the search string. In the command near the top, change this part: /TouchPad|Touchpad/ to something like /SynPS/2 Synaptics TouchPad/ instead, to choose that device. Try that in see if that works. Also, you could simply use /TouchPad/ or /Touchpad/ to choose one device vs the other, since the P is capitalized in only one of those, and the match is case-sensitive. The /TouchPad|Touchpad/ string match I'm searching for in the ex. will match either. That's what the | part means: it means "or". So it matches TouchPad OR Touchpad. Sep 5, 2021 at 19:55
1

I using this command to set a shortcut in Ubuntu without script file.
It's simple and work fine!

bash -c "xinput set-prop $(xinput list --id-only 'Elan Touchpad') 'Device Enabled' $(expr 1 - $(xinput list-props $(xinput list --id-only 'Elan Touchpad') | grep 'Device Enabled' | grep -o '[01]$'))"

Where value Elan Touchpad is my device name.
To get name, using command xinput.

Ubuntu shortcut config

1
  • If you replace Name of Your Touchpad with yours (using xinput as suggested), you can generate the command with this: echo $'bash -c "xinput set-prop $(xinput list --id-only 'Elan Touchpad') 'Device Enabled' $(expr 1 - $(xinput list-props $(xinput list --id-only 'Elan Touchpad') | grep 'Device Enabled' | grep -o '[01]$'))"' | sed 's/Elan Touchpad/Name of Your Touchpad/g'
    – x0s
    Sep 22, 2022 at 12:04
0

Leaving my script here (more or less the same as above). Works out of the box, no need for prior lookup of the device ID.

#!/bin/bash

function query_device_id {
    xinput list | grep -i touchpad | sed 's/.*id=\([0-9]*\).*/\1/g'
}

let device_id=$(query_device_id)

let state=$(xinput list-props $device_id | grep Enabled | awk '{print $4;}')

let new_state=$((1 - $state))

xinput set-prop $device_id "Device Enabled" $new_state
0

Create a shell script and paste below code in to it and name it touchPadDisable.sh

#!/bin/bash
temp=$(xinput | grep -i "Elantech Touchpad" | cut -d"=" -f 2 | cut -d"[" -f 1)
xinput disable $temp

Create another shell script and paste below code in to it and name it touchPadEnable.sh

#!/bin/bash
temp=$(xinput | grep -i "Elantech Touchpad" | cut -d"=" -f 2 | cut -d"[" -f 1)
xinput enable $temp

First script will disable touchpad and second one will enable touchpad.

You can add this script to custom shortcuts in keyboards.

0

Another alternative is to download this gnome extension https://extensions.gnome.org/extension/935/toggle-touchpad/

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .