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