Skip to content

Instantly share code, notes, and snippets.

@oprypin
Created July 8, 2015 20:10
Show Gist options
  • Save oprypin/ff0db70bf231d45836b4 to your computer and use it in GitHub Desktop.
Save oprypin/ff0db70bf231d45836b4 to your computer and use it in GitHub Desktop.
#!/usr/bin/python3
import subprocess
import re
#repl = {6:2, 7:2} # Mouse buttons to be replaced
repl = {6:8, 7:9} # Mouse buttons to be replaced
out = subprocess.check_output(['xinput', 'list'])
for s in out.decode('utf-8').lower().split('\n'):
try:
s.index('mouse')
s.index('pointer')
r = re.search('id=([0-9]+)', s)
if r:
dev = r.group(1)
buttons = subprocess.check_output(('xinput', 'get-button-map', dev)).split()
def replacer(x):
try: return str(repl[int(x)])
except: return x
buttons = list(map(replacer, buttons))
subprocess.call(['xinput', 'set-button-map', dev]+buttons)
# break
except ValueError: # One of the `index` calls failed -> not a mouse pointer
pass # Just move on, this is not an error
#else:
# pass # Could not find an appropriate device
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment