If you don't already know them, use this method to find your ids. Either the decimal or hexadecimal values are fine. The hexadecimal usually start with a 0x, or at least a 0, whereas the decimal is just a regular integer number.
This assumes PyUSB is installed and working, and that you have your vendor and product IDs. If not, go do those things first.
This code was written for Linux (I run Ubuntu), but it should work on Mac, and even Windows, with little to no tweaking. If anything needs to be tweaked, it's likely libUSB or Python related.
#!/usr/bin/python
import sys
import usb.core
import usb.util
# decimal vendor and product values
dev = usb.core.find(idVendor=1118, idProduct=1917)
# or, uncomment the next line to search instead by the hexidecimal equivalent
#dev = usb.core.find(idVendor=0x45e, idProduct=0x77d)
# first endpoint
interface = 0
endpoint = dev[0][(0,0)][0]
# if the OS kernel already claimed the device, which is most likely true
# thanks to http://stackoverflow.com/questions/8218683/pyusb-cannot-set-configuration
if dev.is_kernel_driver_active(interface) is True:
# tell the kernel to detach
dev.detach_kernel_driver(interface)
# claim the device
usb.util.claim_interface(dev, interface)
collected = 0
attempts = 50
while collected < attempts :
try:
data = dev.read(endpoint.bEndpointAddress,endpoint.wMaxPacketSize)
collected += 1
print data
except usb.core.USBError as e:
data = None
if e.args == ('Operation timed out',):
continue
# release the device
usb.util.release_interface(dev, interface)
# reattach the device to the OS kernel
dev.attach_kernel_driver(interface)
Here's what I figured out from a few tests. This may be particular to my Microsoft laser mouse, so results may vary. Also, when I refer to a position, I'm referring to the actual position in bold, not the array index notation.
Examples of mouse direction and velocity
Moving on a diagonal, like up and to the left generates array('B', [16, 0, 255, 255, 255, 255, 0, 0])
Here's how to read the direction and velocity, positions 3 through 6.
If the program terminates abnormally, or if you kill it with CTRL+C, your mouse will remain detached from the kernel. Fix any bugs or looping issues and re-run the program. If/when the program exits successfully it should return reattach the mouse to your operating system. Or, try unplugging and plugging the mouse back in.
Lovingly crafted by orangecoat with some rights reserved, and a promise not to spam you.
Back to top