How can I set a keyboard shortcut to toggle on/off e.g. the trackpad?

You can place a small script that checks the current state under a shortcut key, and make it subsequently switch to the other state.

This works in a lot of situations, eg toggling the trackpad, toggling visibility of (many) indicator icons etc. (see EDIT, down in the post)

The script below does that:

#!/usr/bin/env python3
import subprocess

key = "org.gnome.settings-daemon.peripherals.touchpad touchpad-enabled"

cmd1 = "/bin/bash", "-c", "gsettings get "+key
new_state = "false" if subprocess.check_output(cmd1).decode("utf-8").strip()  == "true" else "true"
cmd2 = "gsettings set "+key+" "+new_state
subprocess.Popen(["/bin/bash", "-c", cmd2])

How to use

  1. Copy the script into an empty file, save it as toggle_touchpad.py
  2. Test-run it by running (in a terminal) the command:

    python3 /path/to/toggle_touchpad.py
    
  3. If all works fine, add it to a shortcut key combination: choose: System Settings > "Keyboard" > "Shortcuts" > "Custom Shortcuts". Click the "+" and add the command:

    python3 /path/to/toggle_touchpad.py
    

Now the shortcut key should toggle the touchpad on/off

Explanation

The touchpad is enabled / disabled by the key:

org.gnome.settings-daemon.peripherals.touchpad touchpad-enabled

To get the current state:

gsettings get org.gnome.settings-daemon.peripherals.touchpad touchpad-enabled

To set to enabled

gsettings set org.gnome.settings-daemon.peripherals.touchpad touchpad-enabled true

To set to disabled:

gsettings set org.gnome.settings-daemon.peripherals.touchpad touchpad-enabled false

Notes

1. Using absolute paths in Custom keyboard shortcuts

Like in .desktop files, relative paths like ~ cannot be used in custom keyboard shortcuts. Also, just like in terminal commands, spaces need to be escaped:

Instead of:

python3 ~/Keyboard\ shortcuts/toggle_touchpad.py

use:

python3 /home/yourname/"Keyboard shortcuts"/toggle_touchpad.py

2. Using the command(s) in 15.04

Due to a bug (it seems) the command:

gsettings set org.gnome.settings-daemon.peripherals.touchpad touchpad-enabled true 

works fine, however the command:

gsettings set org.gnome.settings-daemon.peripherals.touchpad touchpad-enabled false

needs to run twice (!!) to disable the touchpad...

3. Bash version of the script

For those who prefer a bash version for some reason:

#!/bin/bash

key="org.gnome.settings-daemon.peripherals.touchpad touchpad-enabled"
current=$(gsettings get $key)

if [ "$current" == "true" ]; then
  gsettings set $key false
else
  gsettings set $key true
fi

Save it as toggle_touchpad.sh, run it with the command

/bin/bash /path/to/toggle_touchpad.sh

EDIT

The script(s) above can be used to toggle any setting, done by a gsettings command, of the type boolean (false/true). Simply change the line:

key = "org.gnome.settings-daemon.peripherals.touchpad touchpad-enabled"

By the corresponding key.

An example:

If I change the line to:

key = "com.canonical.indicator.sound visible"

It toggles visibility of the sound indicator icon.