Detecting keyboard type/device name

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
knn
Posts: 30
Joined: 30 Mar 2020, 20:35

Detecting keyboard type/device name

Post by knn » 28 May 2020, 12:27

Is there any way programmatically to detect the Keyboard Type/Keyboard name? The reason is, I am often switching keyboards and computers, yet I would like to have only 1 .ahk script.

Can AHK fetch the keyboard name from the device manager or so?

Albireo
Posts: 1747
Joined: 16 Oct 2013, 13:53

Re: Detecting keyboard type/device name

Post by Albireo » 28 May 2020, 17:54

Do you see the name of the keyboard somewhere? (in the device manager?)
Maybe this give you all devices?

Code: Select all

; Enumerate Query
for device in ComObjGet("winmgmts:").ExecQuery("Select * from Win32_PnPEntity")
   list .= device.name "`n"
MsgBox, %list%
For this you need AHK + COM Standard Library:

Code: Select all

COM_Init()

; Create Query
wmi := COM_GetObject("winmgmts:")
Query := COM_Invoke(wmi, "ExecQuery", "Select * from Win32_PnPEntity")
enumQuery := COM_Invoke(Query, "_NewEnum")

; Enumerate Query
while COM_Enumerate(enumQuery, device)=0
   list .= COM_Invoke(device, "Name") "`n"
   
MsgBox, %list%

COM_Release(wmi), COM_Release(Query), COM_Release(enumQuery), COM_Term()

knn
Posts: 30
Joined: 30 Mar 2020, 20:35

Re: Detecting keyboard type/device name

Post by knn » 29 May 2020, 11:05

Albireo wrote:
28 May 2020, 17:54
Do you see the name of the keyboard somewhere? (in the device manager?)
Maybe this give you all devices?

Code: Select all

; Enumerate Query
for device in ComObjGet("winmgmts:").ExecQuery("Select * from Win32_PnPEntity")
   list .= device.name "`n"
MsgBox, %list%
Yes, this works :bravo: Is there a way to list Keyboards only instead of all PnP devices?

Albireo
Posts: 1747
Joined: 16 Oct 2013, 13:53

Re: Detecting keyboard type/device name

Post by Albireo » 29 May 2020, 17:08

Maybe this code is better for you? (change USB to something you search for)

Code: Select all

for device in ComObjGet("winmgmts:").ExecQuery("Select * from Win32_PnPEntity")
{	
	If InStr(device.manufacturer, "USB" )
		list .= device.name "`t`t`t" device.manufacturer  "`n"
	
}
MsgBox, %list%
The choices for Win32_PnPEntity I could find are on this page Device Management Reference and more specific Win32_PnPEntity class From this I have tried ".name" and ".manufacturer" (Have no idea what other of these choices can provide something.)

EDIT:
(Have no idea what this is - maybe something for you?)
DeviceInterfaces.ahk

knn
Posts: 30
Joined: 30 Mar 2020, 20:35

Re: Detecting keyboard type/device name

Post by knn » 29 May 2020, 18:46

Albireo wrote:
29 May 2020, 17:08
(Have no idea what this is - maybe something for you?)
DeviceInterfaces.ahk
Thank you very much for your help. That's a huge script and I am not sure it would run on a non-elevated account. The script you posted works fine :dance:

User avatar
lmstearn
Posts: 688
Joined: 11 Aug 2016, 02:32
Contact:

Re: Detecting keyboard type/device name

Post by lmstearn » 19 Oct 2021, 09:22

FYI DeviceInterfaces.ahk url changed, also requires EnumDeviceInterfaceClasses which is currently AWOL.
:arrow: itros "ylbbub eht tuO kaerB" a ni kcuts m'I pleH

Post Reply

Return to “Ask for Help (v1)”