0

My eventual goal is to detect if a usb keyboard is unplugged - it has to work on mac, windows and linux, which is why I went for python. (basically I have a switching usb hub, like a kvm - but only the usb - and I want to do certain things like shut off the screen when you switch away from a box; I have mac, windows and linux boxes on the switch).

I saw the pyusb and thought, ok - I'll use that to poll usb devices every 30s or so. so

I started with windows - did the following

  • pip install pyusb
  • pip install libusb
  • copy C:\python39\lib\sitepackages\libusb\_platform_windows\x64\libusb-1.0.dll C:\python39\scripts

and then tried the following script in jupyter

import usb
for bus in usb.busses():
    for device in bus.devices():
            print( "Device: " + device.filename )

and it just dies on the first line with "entity not found".
any idea how to fix this?
Or otherwise how to get an event when a usb keyboard (or other device) is disconnected?

Marcello Romani
  • 2,967
  • 31
  • 40
Darren Oakey
  • 2,894
  • 3
  • 29
  • 55

1 Answers1

0

I edited your Python snippet to provide more information (tested on Mac OS):

import usb
for bus in usb.busses():
    print(f"bus: {bus.location}")
    print(f"Device count: {len(bus.devices)}")
    for device in bus.devices:
            print(f"Device: {device.__dict__}")
    print()

Whit this script I get an additional entry if I plug a keyboard into the USB Hub.

Marcello Romani
  • 2,967
  • 31
  • 40