1

I'm new to python but I will try my best to explain what's happening. So i want to write a python script which detects VID,PID and NAME of a keyboard and mouse using pywin32. I already wrote a script for detecting a mouse and it works. Here is the script:

def list_mice_devices():
    wmi = win32com.client.GetObject("winmgmts:")
    devices = wmi.InstancesOf("Win32_PointingDevice")
    mice_list = []
 
    for device in devices:
        name = device.Name
        match = re.search(r'VID_(\w+)&PID_(\w+)', device.PNPDeviceID)
        
        vid, pid = match.groups() if match else (None, None)
        mice_list.append((name, vid, pid))
 
    return mice_list
 
def select_mouse_and_configure():
    print(Fore.CYAN + "\nDetecting mice devices...")
    mice = list_mice_devices()
 
    if not mice:
        print(Fore.RED + "No mouse devices found. Exiting...")
        time.sleep(5)
        exit()

Output: Detecting mice devices... 1 → Synaptics SMBus TouchPad VID: Not found, PID: Not found 2 → USB Input Device VID: 1532, PID: 008A Select your mouse number:

Now i tried it with "Win32_Keyboard" and it is giving error Here's the script for keyboard:

def list_keyboard_devices():
    wmi = win32com.client.GetObject("winmgmts:")
    devices = wmi.InstancesOf("Win32_Keyboards")
    keyboard_list = []
    
    for device in devices:
        name = device.Name
        match = re.search(r'VID_(\w+)&PID_(\w+)', device.PNPDeviceID)
        
        vid, pid = match.groups() if match else (None, None)
        keyboard_list.append((name, vid, pid))
 
    return keyboard_list

def select_keyboard_and_configure():
    print(Fore.CYAN + "\nDetecting keyboard devices...")
    keyboards = list_keyboard_devices()
 
    if not keyboards:
        print(Fore.RED + "No keyboard devices found. Exiting...")
        time.sleep(5)
        exit()

Output:

Detecting keyboard devices...
Traceback (most recent call last):
  File "C:\Project 101\python\keyboard.py", line 102, in <module>
    select_keyboard_and_configure()
  File "C:\Project 101\python\keyboard.py", line 69, in select_keyboard_and_configure
    keyboards = list_keyboard_devices()
                ^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Project 101\python\keyboard.py", line 58, in list_keyboard_devices
    for device in devices:
pywintypes.com_error: (-2147217392, 'OLE error 0x80041010', None, None)

Basically they are same and they should work. If anyone can help it will be very appreciated. THANKS.

I didn't try to solve it yet but i have looked into (https://learn.microsoft.com/en-us/windows/win32/cimwin32prov/win32-keyboard) to understand how "win32_keyboard class works

2 Answers 2

0

If the mouse is working, but the keyboard isn't, try using pywinusb to get the IDs for just the keyboard. I found this on a great website (just a quick search on google brings it up), I put the link below the code.

import pywinusb.hid as hid

def get_keyboard_pid_vid():
    keyboards = hid.find_all_hid_devices()
    for keyboard in keyboards:
        if keyboard.product_name == 'Keyboard':
            return keyboard.vendor_id, keyboard.product_id
    return None

pid, vid = get_keyboard_pid_vid()
print(f"PID: {pid}, VID: {vid}")

I suspect this also works for the mouse, so if that stops working as well, try this. Note that you need to install pywinusb on your system before you can run this code.

Source

Sign up to request clarification or add additional context in comments.

Comments

0

For your list_keyboard_devices you will need to change devices = wmi.InstancesOf("Win32_Keyboards") to devices = wmi.InstancesOf("Win32_Keyboard"). Note that you have use signular form Win32_Keyboard. This should give you a list of keyboards.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.