Skip to content

Instantly share code, notes, and snippets.

@foucault
Last active August 12, 2017 00:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save foucault/d3e6f66968e5e046d4c57c12351b3964 to your computer and use it in GitHub Desktop.
Save foucault/d3e6f66968e5e046d4c57c12351b3964 to your computer and use it in GitHub Desktop.
Enable middle click emulation with xinput
#!/usr/bin/python
# This small script will enable middle click emulation for all pointing
# devices available. For some reason GNOME does not expose any option at all
# for middle click emulation so the only way to enable it is through the
# `xinput` program, which must be installed for the script to work
from subprocess import check_output, check_call
import re
MID_EMU_ENABLED_RE = "Middle Emulation Enabled"
POINTER_RE = ".*slave\s+pointer.*"
if __name__ == "__main__":
# Get all the XInput devices
raw_list = check_output('/usr/bin/xinput').decode()
pid = []
for p in raw_list.split("\n"):
dev = re.split("\t+", p)
if len(dev) > 2:
# Check if device is a pointer but not the XTEST device
# if it's a pointer add it to the list
if re.match(POINTER_RE, dev[2]) and not "XTEST" in dev[0]:
pid.append(int(dev[1].split("=")[1]))
for pointer in pid:
# Request all available properties for the pointer
raw_props = check_output(['/usr/bin/xinput',
'list-props',
str(pointer)]).decode()
# Traverse through the properties
for prop in raw_props.split("\n"):
# Check if this property is the Middle Click Emulation but not
# the default one, i.e.
# THIS
# libinput Middle Emulation Enabled (300): 0
# NOT THIS
# libinput Middle Emulation Enabled Default (301): 0
if (MID_EMU_ENABLED_RE in prop) and not ("Default" in prop):
id_match = re.match('.*\((\d+)\).*', prop)
prop_id = int(id_match.group(1))
try:
# Set the middle click emulation property to 1
# thus enabling it!
check_call(['/usr/bin/xinput',
'set-int-prop',
str(pointer),
str(prop_id), '8', '1'])
except CalledProcessError as exc:
print("Failed to enable middle click emulation",
"for pointing device id=%d" % pointer)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment