Skip to content

Instantly share code, notes, and snippets.

@michaelfm1211
Last active September 25, 2022 23:15
Show Gist options
  • Save michaelfm1211/e3d94b2a29d99a2befa76d9708282d36 to your computer and use it in GitHub Desktop.
Save michaelfm1211/e3d94b2a29d99a2befa76d9708282d36 to your computer and use it in GitHub Desktop.
pyusb script to use an extra USB mouse with my totp program
# adapted from https://www.orangecoat.com/how-to/read-and-decode-data-from-your-mouse-using-this-pyusb-hack
# requirements:
# - macOS (although it may be adapted for linux)
# - pyusb (https://pypi.org/project/pyusb/)
# - my totp program (https://github.com/michaelfm1211/totp). you might be able to use another totp program
# - the accompanying totp-copy.sh (https://gist.github.com/michaelfm1211/32336940e1d27c4b2a44c9e3f665f26c). again, may be adapted for linux
# - terminal-notifier (https://github.com/julienXX/terminal-notifier). not stricly needed, but nice to have
import atexit
import os
import usb.core
import usb.util
# change this function to suit your needs and your system
def left_click():
os.system('totp-copy.sh github')
os.system('totp -r github | terminal-notifier -title "GitHub"')
print('copied github totp')
# change this function to suit your needs and your system
def right_click():
os.system('totp-copy.sh google')
os.system('totp -r google | terminal-notifier -title "Google"')
print('copied google totp')
# change this to get your second USB mouse
dev = usb.core.find(idProduct=0x0538)
interface = 0
endpoint = dev[0][(0, 0)][0]
@atexit.register
def release_mouse():
usb.util.release_interface(dev, interface)
dev.attach_kernel_driver(interface)
if dev.is_kernel_driver_active(interface) is True:
dev.detach_kernel_driver(interface)
usb.util.claim_interface(dev, interface)
while True:
data = None
try:
data = dev.read(endpoint.bEndpointAddress, endpoint.wMaxPacketSize)
except usb.core.USBError as e:
if e.args == ('Operation timed out',):
continue
else:
print(e)
break
if data[1] == 0 or set(data[2:]) != set([0, 0, 0, 0]):
continue
if data[1] == 1:
left_click()
elif data[1] == 2:
right_click()
else:
print(data)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment