Saturday, March 3, 2018

Raspberry Pi USB Input Device Data Monitor (Part 2)

USB Mouse/Keyboard Data Monitoring Using Raspberry Pi

In the previous post here, I was using python evdev module to capture the USB data.
This time I used python pyusb module since I found a great post by Gourmet Web Design

Install pip:
$ sudo apt-get update$ sudo apt-get install python-pip
Install puusb module:
$ sudo pip install pyusb
Check input events:
$ ls /dev/input
If you do not know which event is mouse or keyboard, follow this.
Install input-utils:
$ sudo apt-get install input-utils
$ lsinput

If you want to use pyusb to check the input devices, you can refer to this post by Gourmet Web Design


Python Code
This is the python example code.

import usb.coreimport usb.util
dev = usb.core.find(idVendor=0x093a, idProduct=0x2510)
# was it found?if dev is None:    print usb.core.find()    raise ValueError('Device not found')
# first endpointinterface = 0endpoint = dev[0][(0,0)][0]print(endpoint)
# tell the kernel to detachdev.detach_kernel_driver(interface)
while True:    try:        data =,endpoint.wMaxPacketSize)        print data    except usb.core.USBError as e:        data = None        if e.args == ('Operation timed out',):            continue
# release the deviceusb.util.release_interface(dev, interface)# reattach the device to the OS kerneldev.attach_kernel_driver(interface)

Crate a python script file as below and copy&paste the above code.
Run the python script.
Press any buttons on the keyboard or mouse and you will see the outputs in the terminal.

No comments:

Post a Comment